home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 1.ddi / MWHC.001 / 7 < prev    next >
Encoding:
Text File  |  1992-03-02  |  14.7 KB  |  612 lines

  1. /*  Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13  */
  2.  
  3. /**** name=_offsetof ****/
  4. #define main() TEST__offsetof()
  5.  
  6. #include <stdio.h>
  7. void main() {
  8.    struct s{
  9.       char x, y;
  10.       int a[10];
  11.       } x;
  12.    printf("%d\n",_offsetof(struct s,x)); /* Is zero.                     */
  13.    printf("%d\n",_offsetof(struct s,y)); /* Is one.                      */
  14.    printf("%d\n",_offsetof(x       ,a)); /* Is four.                     */
  15.    }
  16. /* End _offsetof  */
  17. #undef main
  18. /**** name=_open ****/
  19. #define main() TEST__open()
  20.  
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #include <stdio.h>
  25.  
  26. void main() {
  27.    int pan;
  28.    pan = _open("test.dat",O_RDONLY | O_TEXT);
  29.    if (pan != -1)
  30.       printf("file test.dat exists, handle=%d\n",pan);
  31.    else
  32.       printf("file test.dat cannot be opened,"
  33.              " error code=%d\n",pan);
  34.  
  35.    pan=_open("open.tmp",
  36.              O_CREAT | O_BINARY | O_EXCL,
  37.              S_IREAD | S_IWRITE);
  38.    if (pan == -1)
  39.       printf("cannot create file open.tmp,"
  40.              " error code=%d\n",pan);
  41.    else
  42.       printf("file open.tmp has been created,"
  43.              " handle=%d\n",pan);
  44.  
  45.    pan=_open("open.tmp",
  46.              O_CREAT | O_BINARY | O_TRUNC,
  47.              S_IREAD | S_IWRITE);
  48.    if (pan == -1)
  49.       printf("cannot truncate file open.tmp,"
  50.              " error code=%d\n",pan);
  51.    else
  52.       printf("file open.tmp has been truncated,"
  53.              " handle=%d\n",pan);
  54.    }
  55.  
  56. /* End _open  */
  57. #undef main
  58. /**** name=_outpX ****/
  59. #define main() TEST__outpX()
  60.  
  61. #include <conio.h>
  62. #define MHZ 20
  63.  
  64. void main() {
  65.    volatile int j;
  66.    int k, n;
  67.  
  68.    for (n = 0; n < 500; n++) {
  69.       k = inp(0x61) & 0xfc;
  70.       outp(0x61, k | 2);
  71.       for (j = 0; j < 5 * MHZ; j++);                           /* Listen */
  72.       outp(0x61, k);
  73.       for (j = 0; j < 10 * MHZ; j++);                              /* to */
  74.       outp(0x61, k | 2);
  75.       for (j = 0; j < 10 * MHZ; j++);                             /* the */
  76.       outp(0x61, k);
  77.       for (j = 0; j < 20 * MHZ; j++);                            /* tone */
  78.       }
  79.    }
  80.  
  81. /* End _outp*  */
  82. #undef main
  83. #undef  MHZ
  84. /**** name=perror ****/
  85. #define main() TEST_perror()
  86.  
  87. /*
  88.    The program fragment below attempts to open exists.not, and complains if
  89.    the open fails.
  90. */
  91.  
  92. #include <stdio.h>
  93. #define FAILURE (-1)
  94. #define OKAY    (1)
  95.  
  96. int main() {
  97.    FILE *FP1;
  98.    if ((FP1 = fopen("exists.not", "r")) == NULL) {
  99.       perror("exists.not");
  100.       return(FAILURE);
  101.       }
  102.    else {
  103.       printf("File exists, cannot test perror.\n");
  104.       return(OKAY);
  105.       }
  106.    }
  107.  
  108. /* End perror  */
  109. #undef main
  110. #undef  FAILURE
  111. #undef  OKAY
  112. /**** name=printf ****/
  113. #define main() TEST_printf()
  114.  
  115. /*
  116.    A similar example for fprintf shows the similarity of the two functions.
  117. */
  118.  
  119. #include <stdio.h>
  120.  
  121. void main() {
  122.    char s[14] = "like this one", c = '*';
  123.    int    i = 10, x = 255, o = 45;
  124.    double d = 3.1415927, nf = -3.449123;
  125.  
  126.    printf("printf prints strings (%s),\n", s);
  127.    printf("decimal numbers (%d),", i);
  128.    printf(" hex numbers (%04X),\n", x);
  129.    printf("floating-point numbers (%+.4e)\n", d);
  130.    printf("percent signs (%%),\n");
  131.    printf("characters (%-5c),\n", c);
  132.    printf("negative floating-points (% 010.2e),\n", nf);
  133.    printf("and even octal numbers (%-+#5o).", o);
  134.    }
  135.  
  136. /* End printf  */
  137. #undef main
  138. /**** name=putc ****/
  139. #define main() TEST_putc()
  140. /* End putc  */
  141. #undef main
  142. /**** name=_putch ****/
  143. #define main() TEST__putch()
  144.  
  145. /*
  146.    This program tests  functions _putch(), isalpha(), isupper(), toupper(),
  147.    tolower(), and isdigit().  It reads input from standard input and prints
  148.    output to the screen.    The program prints all alphabetic characters in
  149.    uppercase.
  150. */
  151.  
  152. #include <stdio.h>
  153. #include <conio.h>
  154. #include <ctype.h>
  155.  
  156. void main() {
  157.    int c, i=0;
  158.    puts("Testing _putch...\n");
  159.    puts("Enter characters, (';' to terminate).");
  160.  
  161.    while ((c = getche()) != ';') {                          /* Terminate at
  162.                                                               semicolon. */
  163.       if (isalpha(c)) {
  164.          if (isupper(c))
  165.             _putch(c);
  166.          else if (islower(c))
  167.             _putch(toupper(c));
  168.          else printf("\nERROR in isalpha().\n");
  169.          }
  170.       else if (isdigit(c))
  171.          _putch(c);
  172.       else _putch(toupper(tolower(c)));
  173.       i++;
  174.       }
  175.    printf("\nProgram terminated at semicolon.\n");
  176.    printf("Number of characters read: %d.\n",i);
  177.    }
  178.  
  179. /* End _putch  */
  180. #undef main
  181. /**** name=putchar ****/
  182. #define main() TEST_putchar()
  183. /* End putchar  */
  184. #undef main
  185. /**** name=_putenv ****/
  186. #define main() TEST__putenv()
  187.  
  188. /*
  189.    This program displays the  environment  variables,  changes the value of
  190.    the PATH variable, adds a new variable, and redisplays the values.
  191. */
  192.  
  193. #include <stdio.h>
  194. #include <stdlib.h>
  195.  
  196. void printenviron() {
  197.    int i = 0;
  198.    while (_environ[i])
  199.       printf("%s\n\n", _environ[i++]);
  200.    }
  201.  
  202. void main() {
  203.    /* Print status of all environment variables. */
  204.    printf("Current environment variables:\n");
  205.    printenviron();
  206.    /* Change one and add a new one. */
  207.    _putenv("PATH=c:\\highc\\small");
  208.    _putenv("NEWENVIRONVAR=SOME\\DIR\\SPECIFICATION");
  209.    /* Print status again. */
  210.    printf("New environment variables:\n");
  211.    printenviron();
  212.    }
  213.  
  214. /* End _putenv  */
  215. #undef main
  216. /**** name=puts ****/
  217. #define main() TEST_puts()
  218. /* End puts  */
  219. #undef main
  220. /**** name=qsort ****/
  221. #define main() TEST_qsort()
  222.  
  223. #include <stdio.h>
  224. #include <stdlib.h>
  225. #include <string.h>
  226.  
  227. struct info {
  228. char name[8];
  229.    int  age;
  230. char phone[9];
  231.    };
  232.  
  233. /* Unsorted info array: */
  234. struct info data[] =  {
  235.    { "Anne",  33, "555-5552" },
  236.    { "Fred",  27, "555-1221" },
  237.    { "Sonya", 36, "555-1976" },
  238.    { "Frank", 30, "555-1965" },
  239.    { "Carol", 32, "555-4299" },
  240.    { "Alice", 19, "555-7979" },
  241.    { "Larry", 33, "555-8235" },
  242.    };
  243.  
  244. static int
  245. compare_function(const struct info *item1,
  246.                  const struct info *item2) {
  247.    if (item1->age < item2->age) /* Compare by age. */
  248.       return (-1);
  249.    else if (item1->age > item2->age)
  250.       return (1);
  251.    else  /* Then compare by name. */
  252.       return (strcmp(item1->name, item2->name));
  253.    }
  254.  
  255. void main() {
  256.    int i;
  257.    qsort(data, sizeof(data)/sizeof(*                                 data),
  258.                sizeof(*data), compare_function);
  259.    for (i = 0; i < sizeof(data)/sizeof(* data); i++)
  260.       printf("%-8s %d, %s\n", data[i].name,
  261.                               data[i].age,
  262.                               data[i].phone);
  263.    }
  264.  
  265. /* End qsort  */
  266. #undef main
  267. /**** name=rand ****/
  268. #define main() TEST_rand()
  269.  
  270. /*
  271.    This  program checks the robustness of the  rand()  function.  It  calls
  272.    rand() LOOPS times. The  program  computes the modulus MOD of the number
  273.    returned  from  the  rand() function and  keeps  a  frequency  count  of
  274.    numbers.
  275. */
  276. #include <stdio.h>
  277. #include <stdlib.h>
  278. #define LOOPS 1000
  279. #define MOD   10
  280.  
  281. void main() {
  282.    int i, k;
  283.    int freq[MOD];                      /* Used for occurrence frequency. */
  284.  
  285.    puts("Testing rand...\n");
  286.  
  287.    for (i=0; i < MOD; i++)
  288.         freq[i] = 0;
  289.  
  290.    for (i = 0; i <LOOPS; i++) {
  291.         k = rand() % MOD;
  292.         freq[k]++;
  293.         }
  294.  
  295.    for (i = 0; i < MOD; i++)
  296.         printf("freq[%d] => %d\n",
  297.                i, freq[i]);
  298.    }
  299.  
  300. /* End rand  */
  301. #undef main
  302. #undef  LOOPS
  303. #undef  MOD
  304. /**** name=_read ****/
  305. #define main() TEST__read()
  306.  
  307. #include <io.h>
  308. #include <fcntl.h>
  309. #include <sys/stat.h>
  310. #include <stdio.h>
  311. #include <conio.h>
  312.  
  313. void main() {
  314.    int  pan;
  315.    char c;
  316.  
  317.    puts("Testing _read...\n");
  318.    pan = open("test.dat", O_RDONLY | O_BINARY);
  319.    if (pan != -1) {
  320.       printf("File test.dat exists, "
  321.              "handle = %d.\n",pan);
  322.       while(_read(pan, &c, 1) > 0)
  323.          putch(c);
  324.       }
  325.    else
  326.       printf("File test.dat cannot"
  327.              " be opened, error code = %d.\n",pan);
  328.    }
  329.  
  330. /* End _read  */
  331. #undef main
  332. /**** name=realloc ****/
  333. #define main() TEST_realloc()
  334.  
  335. #include <stdlib.h>
  336. #include <stdio.h>
  337.  
  338. void main() {
  339.    typedef long grade;
  340.    grade *grade_ptr;
  341.    
  342.    puts("Testing realloc...\n");
  343.    grade_ptr = (long *) calloc (1, sizeof(grade));
  344.    *grade_ptr = 2;
  345.    grade_ptr = NULL;
  346.  
  347.    if((grade_ptr = (long *)
  348.       realloc(grade_ptr, sizeof(long))) == NULL)
  349.       puts("Failed to reallocate a Null pointer.");
  350.    else
  351.       printf("Grade after reallocation of NULL is"
  352.              " %d.\n", *grade_ptr);
  353.    *grade_ptr = 3;
  354.    printf("Grade is now %d.\n", *grade_ptr);
  355.    }
  356.  
  357. /* End realloc  */
  358. #undef main
  359. /**** name=remove ****/
  360. #define main() TEST_remove()
  361.  
  362. /*
  363.    This program opens a temporary file and renames it to input.tmp.  Later,
  364.    it renames the file back to temporary file and removes it.  This example
  365.    illustrates the use of remove, tmpnam, and rename.
  366. */
  367. #include <stdio.h>
  368. #define FAILURE  (-1)
  369.  
  370. int main() {
  371.    FILE *FP1;
  372.    int flag;
  373.    char tmp[L_tmpnam], new[L_tmpnam]="input.tmp";
  374.    puts("Testing remove...\n");
  375.    if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
  376.       perror("Cannot open temporary file.");
  377.       return FAILURE;
  378.       }
  379.  
  380.    else
  381.       puts("Temporary file is now open.");
  382.  
  383.    flag = rename(tmp,new);
  384.    if (!flag)                                  /* Rename was successful. */
  385.       printf("File renamed as ==>%s. \n",new);
  386.    else
  387.       printf("File is not renamed to %s.\n",new);
  388.  
  389.    flag = rename(new,tmp);
  390.    fclose(FP1);
  391.  
  392.    if (remove(tmp))
  393.       perror("Cannot remove temporary file.");
  394.    else
  395.       puts("Temporary file is now removed.");
  396.    }
  397.  
  398. /* End remove  */
  399. #undef main
  400. #undef  FAILURE
  401. /**** name=rename ****/
  402. #define main() TEST_rename()
  403. /* End rename  */
  404. #undef main
  405. /**** name=rewind ****/
  406. #define main() TEST_rewind()
  407.  
  408. /*
  409.    The program below copies test.dat, a block at a  time,  to  rewind1.tmp,
  410.    then to rewind2.tmp.
  411. */
  412. #include <stdio.h>
  413. #define FAILURE   (-1)
  414. #define BLOCKSIZE (512)
  415.  
  416. int main() {
  417.    FILE *FP1, *FP2, *FP3;
  418.    char buf[BLOCKSIZE];   int i;
  419.    FILE *open(char *f,char *mode) {
  420.       FILE *FP;
  421.       if ((FP = fopen(f,mode)) == NULL) perror(f);
  422.       return FP;
  423.       }
  424.    if (NULL == (FP1 = open("test.dat","r"))    ||
  425.        NULL == (FP2 = open("rewind1.tmp","w")) ||
  426.        NULL == (FP3 = open("rewind2.tmp","w"))
  427.        ) return FAILURE;
  428.    while ((i = fread(buf, 1, BLOCKSIZE, FP1)) != 0)
  429.       fwrite(buf, 1, i, FP2);
  430.    rewind(FP1);
  431.    while ((i = fread(buf, 1, BLOCKSIZE, FP1)) != 0)
  432.       fwrite(buf, 1, i, FP3);
  433.    }
  434.  
  435. /* End rewind  */
  436. #undef main
  437. #undef  FAILURE
  438. #undef  BLOCKSIZE
  439. /**** name=_rmdir ****/
  440. #define main() TEST__rmdir()
  441.  
  442. /*
  443.    This program creates a new directory and then deletes it.
  444. */
  445.  
  446. #include <stdio.h>
  447. #include <direct.h>
  448.  
  449. void main() {
  450.    /* Create a new directory. */
  451.    if (mkdir("c:\\rmdir") == -1)
  452.       perror("Error in mkdir.");
  453.    else {
  454.       printf("Directory c:\\rmdir "
  455.              "successfully created.\n");
  456.  
  457.       /* Remove newly created directory. */
  458.       if (_rmdir("c:\\rmdir") == -1)
  459.          perror("Error in _rmdir.");
  460.       else
  461.          printf("Directory c:\\rmdir removed.\n");
  462.       }
  463.    }
  464.  
  465. /* End _rmdir  */
  466. #undef main
  467. /**** name=_rmemcpy ****/
  468. #define main() TEST__rmemcpy()
  469.  
  470. #include <stdio.h>
  471. #include <string.h>
  472.  
  473. void main() {
  474.    char dest[100];
  475.    char *source = "This is the string to be copied.";
  476.  
  477.    strcpy(dest, "This will be overwritten.\n");
  478.  
  479.    printf("source before: %s\n", source);
  480.    printf("dest   before: %s\n", dest);
  481.  
  482.    _rmemcpy(dest, source, strlen(source));
  483.  
  484.    printf("source after: %s\n", source);
  485.    printf("dest   after: %s\n", dest);
  486.    }
  487.  
  488. /* End _rmemcpy  */
  489. #undef main
  490. /**** name=_rotX ****/
  491. #define main() TEST__rotX()
  492.  
  493. #include <stdlib.h>
  494. #include <stdio.h>
  495.  
  496. void main() {
  497.    int k=1;
  498.    int j=1;
  499.  
  500.    printf("Rotate left and right by "
  501.           "one bit at a time:\n");
  502.    do {
  503.        printf("%8x ... %8x\n", k, j);
  504.        k = _rotl(k, 1);
  505.        j = _rotr(j, 1);
  506.        } while (k != 1);
  507.    }
  508.  
  509. /* End _rot*  */
  510. #undef main
  511. /**** name=_rstrcpy ****/
  512. #define main() TEST__rstrcpy()
  513.  
  514. #include <string.h>
  515. #include <stdio.h>
  516.  
  517. void main() {
  518.    char string1[50] = "abcdefghijklmnopqrstuvwxyz";
  519.    char string2[50] = "abcdefghijklmnopqrstuvwxyz";
  520.    char *source1 = string1, *dest1 = &string1[9];
  521.    char *source2 = string2, *dest2 = &string2[9];
  522.  
  523.    _rstrcpy(dest1, source1);
  524.    puts(string1);
  525.    _fill_char(&string2[27], 23, '\0');                      /* For puts. */
  526.    strcpy(dest2, source2);
  527.    puts(string2);
  528.    }
  529.  
  530. /* End _rstrcpy  */
  531. #undef main
  532. /**** name=_rstrncpy ****/
  533. #define main() TEST__rstrncpy()
  534.  
  535. #include <string.h>
  536. #include <stdio.h>
  537.  
  538. void main() {
  539.    char string1[50] = "abcdefghijklmnopqrstuvwxyz";
  540.    char string2[50] = "abcdefghijklmnopqrstuvwxyz";
  541.    char *source1 = string1, *dest1 = &string1[9];
  542.    char *source2 = string2, *dest2 = &string2[9];
  543.  
  544.    _rstrncpy(dest1, source1, 14);
  545.    puts(string1);
  546.    strncpy(dest2, source2, 14);
  547.    puts(string2);
  548.    }
  549.  
  550. /* End _rstrncpy  */
  551. #undef main
  552.  
  553. /*****names*****/
  554.  
  555. char * names[]={
  556.    "_offsetof",
  557.    "_open",
  558.    "_outpX",
  559.    "perror",
  560.    "printf",
  561.    "_putch",
  562.    "_putenv",
  563.    "qsort",
  564.    "rand",
  565.    "_read",
  566.    "realloc",
  567.    "remove",
  568.    "rewind",
  569.    "_rmdir",
  570.    "_rmemcpy",
  571.    "_rotX",
  572.    "_rstrcpy",
  573.    "_rstrncpy",
  574.    "",""};
  575.    int nextfunum;
  576. void main() {
  577.    char ans[90];
  578.    for (;;) {
  579.       for (int j=0;j< 18;j++)
  580.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  581.       else printf("%4d %-21s",j+1,names[j]);
  582.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  583.       gets(ans);
  584.       if (ans[0] != 0) nextfunum=atoi(ans);
  585.       printf("\n\n\n");
  586.       switch(nextfunum) {
  587.          case 0:exit(0);
  588.          case 1:TEST__offsetof();break;
  589.          case 2:TEST__open();break;
  590.          case 3:TEST__outpX();break;
  591.          case 4:TEST_perror();break;
  592.          case 5:TEST_printf();break;
  593.          case 6:TEST__putch();break;
  594.          case 7:TEST__putenv();break;
  595.          case 8:TEST_qsort();break;
  596.          case 9:TEST_rand();break;
  597.          case 10:TEST__read();break;
  598.          case 11:TEST_realloc();break;
  599.          case 12:TEST_remove();break;
  600.          case 13:TEST_rewind();break;
  601.          case 14:TEST__rmdir();break;
  602.          case 15:TEST__rmemcpy();break;
  603.          case 16:TEST__rotX();break;
  604.          case 17:TEST__rstrcpy();break;
  605.          case 18:TEST__rstrncpy();break;
  606.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  607.          }
  608.       printf("\n\npress enter to select another function\n");
  609.       gets(ans);
  610.       }
  611.    }
  612.