home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c220 / 4.ddi / EXAMPLES / OR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-16  |  13.3 KB  |  578 lines

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