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

  1. /*  Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13  */
  2.  
  3. /**** name=scanf ****/
  4. #define main() TEST_scanf()
  5.  
  6. /*
  7.    A similar example is  given  for  fscanf,  showing the slight difference
  8.    between the two functions.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. void main() {
  14.    char s1[11], s2[19] = "but missed eleven.";
  15.    int i, j;
  16.  
  17.    printf("Please type:\nscanf read 10 %11 and then "
  18.           "quit.z and garbage?\n");
  19.    scanf("%11c%d %%%d %[^z]z", s1, &i, &j, s2);
  20.    printf("%11s%d, %s%s",
  21.           s1, i, j == 11 ? "11, " : "", s2);
  22.    }
  23.  
  24. /* End scanf  */
  25. #undef main
  26. /**** name=_searchenv ****/
  27. #define main() TEST__searchenv()
  28.  
  29. /*
  30.    This program searches for  the  location of file stdio.h using the IPATH
  31.    environment variable.
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37.  
  38. void main() {
  39.    char buffer[70];
  40.  
  41.    _searchenv("stdio.h", "IPATH", buffer);
  42.    if (strcmp(buffer, "") == 0)
  43.       puts("File not found.\n");
  44.    else
  45.       printf("The file is in directory: %s.\n",
  46.              buffer);
  47.    }
  48.  
  49. /* End _searchenv  */
  50. #undef main
  51. /**** name=_segread ****/
  52. #define main() TEST__segread()
  53.  
  54. /*
  55.    This program displays the current values of the segment registers.
  56. */
  57.  
  58. #include <stdio.h>
  59. #include <dos.h>
  60.  
  61. void main() {
  62.    struct SREGS segs;
  63.    unsigned cs, ds, es, ss;
  64.  
  65.    _segread(&segs);
  66.    cs = segs.cs;
  67.    ds = segs.ds;
  68.    es = segs.es;
  69.    ss = segs.ss;
  70.    printf("Current values of segment registers:\n");
  71.    printf("code  segment: %x\ndata  segment:"
  72.           " %x\nextra segment: %x\n"
  73.           "stack segment: %x\n", cs, ds, es, ss);
  74.    }
  75.  
  76. /* End _segread  */
  77. #undef main
  78. /**** name=setjmp ****/
  79. #define main() TEST_setjmp()
  80. /* End setjmp  */
  81. #undef main
  82. /**** name=_setmode ****/
  83. #define main() TEST__setmode()
  84.  
  85. /*
  86.    The program below copies a line from standard
  87.    input to standard output without doing any text
  88.    conversions.
  89. */
  90.  
  91. #include <stdio.h>
  92.  
  93. void TEST__setmode() {
  94.    char line[256];
  95.  
  96.    _setmode(stdin,  _BINARY);
  97.    _setmode(stdout, _BINARY);
  98.    
  99.    printf("\nWaiting for input: ");
  100.    if (gets(line))
  101.       puts(line);
  102.    }
  103.  
  104. /* End _setmode  */
  105. #undef main
  106. /**** name=setvbuf ****/
  107. #define main() TEST_setvbuf()
  108.  
  109. #include <stdio.h>
  110. #include <stdlib.h>
  111.  
  112. void main() {
  113.    char buffer[256];
  114.    size_t size;
  115.    FILE *FP1, *FP2;
  116.  
  117.    size = sizeof(buffer);
  118.  
  119.    if ((FP1 = fopen("setvbuf1.tmp","a")) == NULL)
  120.       printf("Unable to open setvbuf1.tmp.\n");
  121.    if ((FP2 = fopen("setvbuf2.tmp","w")) == NULL)
  122.       printf("Unable to open setvbuf2.tmp.\n");
  123.    if (setvbuf(FP1,buffer,_IOFBF,size) != 0)
  124.       printf("setvbuf failed on setvbuf1.tmp\n");
  125.    else
  126.       printf("setvbuf was successful.\n");
  127.    if (setvbuf(FP2,NULL,_IONBF,0) != 0)
  128.       printf("setvbuf failed on setvbuf2.tmp\n");
  129.    else
  130.       printf("setvbuf was successful.\n");
  131.    fclose(FP1);
  132.    fclose(FP2);
  133.    }
  134.  
  135. /* End setvbuf  */
  136. #undef main
  137. /**** name=_skip_char ****/
  138. #define main() TEST__skip_char()
  139.  
  140. #include <stdio.h>
  141.  
  142. void main() {
  143.    char p[10] = "TESTING.";
  144.    char q[10] = "NNNNNNT.";
  145.    unsigned sl = 10, n, m;
  146.    char c = 'T';
  147.    char d = 'N';
  148.  
  149.    n=_skip_char(p, sl, c);
  150.    m=_skip_char(q, sl, d);
  151.    printf("n = %u, m = %u\n", n, m);
  152.    }
  153.  
  154. /* End _skip_char  */
  155. #undef main
  156. /**** name=_sopen ****/
  157. #define main() TEST__sopen()
  158.  
  159. #include <io.h>
  160. #include <fcntl.h>
  161. #include <share.h>
  162. #include <sys/stat.h>
  163. #include <stdio.h>
  164.  
  165. void main() {
  166.    int pan;
  167.    pan = _sopen("test.dat",O_RDONLY | O_TEXT,SH_DENYWR);
  168.    if (pan != -1)
  169.       printf("File test.dat exists, handle=%d\n", pan);
  170.    else
  171.       printf("File test.dat cannot be sopened,"
  172.              " error code=%d\n",pan);
  173.  
  174.    pan = _sopen("sopen.tmp",
  175.                 O_CREAT | O_BINARY | O_EXCL,SH_DENYWR,
  176.                 S_IREAD | S_IWRITE);
  177.    if (pan == -1)
  178.       printf("Cannot create file sopen.tmp,"
  179.              " error code=%d\n",pan);
  180.    else
  181.       printf("File sopen.tmp has been created,"
  182.              " handle=%d\n",pan);
  183.    close(pan);
  184.  
  185.    pan = _sopen("sopen.tmp",
  186.                 O_CREAT | O_BINARY | O_TRUNC,SH_DENYWR,
  187.                 S_IREAD | S_IWRITE);
  188.    if (pan == -1)
  189.       printf("Cannot truncate file sopen.tmp,"
  190.              " error code=%d\n",pan);
  191.    else
  192.       printf("File sopen.tmp has been truncated,"
  193.              " handle=%d\n",pan);
  194.    }
  195.  
  196. /* End _sopen  */
  197. #undef main
  198. /**** name=_spawnX ****/
  199. #define main() TEST__spawnX()
  200.  
  201. /*
  202.    Spawn a child process chosen by the user.
  203. */
  204.  
  205. #include <stdio.h>
  206. #include <process.h>
  207. #include <string.h>
  208.  
  209. void main() {
  210.    char path[30];
  211.    int  retcode, i;
  212.    char *args[10];
  213.  
  214.    printf("Enter name of program to spawn"
  215.           " (with arguments): ");
  216.    scanf("%[^'\n']", path);
  217.    args[0] = strtok(path, "  -\\/,");
  218.    i=1;
  219.    while ((args[i] = strtok(NULL, "  -\\/,")) != NULL)
  220.       i++;
  221.    printf("Spawning subprocess %s...\n", path);
  222.    if ((retcode = _spawnv(P_WAIT, path, args)) == 0)
  223.       puts("The child process terminated normally.");
  224.    else if (retcode == -1)
  225.       puts("The child process was not started.");
  226.    else
  227.       puts("The child process had an abnormal exit.");
  228.    }
  229.  
  230. /* End _spawn*  */
  231. #undef main
  232. /**** name=_splitpath ****/
  233. #define main() TEST__splitpath()
  234.  
  235. /*
  236.    Create a full pathname in a buffer, then call _splitpath to break it
  237.    into its components.
  238. */
  239.  
  240. #include <stdio.h>
  241. #include <stdlib.h>
  242.  
  243. void main() {
  244.    char path_buffer[_MAX_PATH];
  245.    char drive[_MAX_DRIVE];
  246.    char dir[_MAX_DIR];
  247.    char fname[_MAX_FNAME];
  248.    char ext[_MAX_EXT];
  249.  
  250.    /* Create a full pathname in path_buffer. */
  251.    _makepath(path_buffer, "d", "highc\\inc\\",
  252.              "makpath", "c");
  253.    printf("Path created with _makepath: %s\n\n",
  254.            path_buffer);
  255.  
  256.    /* Now split into parts. */
  257.    _splitpath(path_buffer, drive, dir, fname, ext);
  258.    printf("Path extracted with _splitpath:\n");
  259.    printf("   drive: %s\n", drive);
  260.    printf("   dir  : %s\n", dir);
  261.    printf("   fname: %s\n", fname);
  262.    printf("   ext  : %s\n", ext);
  263.    }
  264.  
  265. /* End _splitpath  */
  266. #undef main
  267. /**** name=sprintf ****/
  268. #define main() TEST_sprintf()
  269.  
  270. /*
  271.    A  similar  example  for fprintf shows the slight difference between the
  272.    two functions.
  273. */
  274.  
  275. #include <stdio.h>
  276.  
  277. void main() {
  278.    char String[200];
  279.    char *S = String;
  280.    char s[14] = "like this one";
  281.    int i = 10, x = 255, n;
  282.    double d = 3.1415927;
  283.  
  284.    sprintf(S, "sprintf prints strings (%s),%n",s,&n);
  285.    S += n; /*  The next call to sprintf must be                          */
  286.            /*   passed the address of the NUL                            */
  287.            /*   written by the last call.                                */
  288.    sprintf(S, "\ndecimal numbers (%d),%n", i, &n);
  289.    S += n;
  290.    sprintf(S, " hex numbers (%04X),\n%n", x, &n);
  291.    S += n;
  292.    sprintf(S, "percent signs (%%), and\n%n", &n);
  293.    S += n;
  294.    sprintf(S, "floating-point numbers (%+.4e).", d);
  295.    puts(S);
  296.    }
  297.  
  298. /* End sprintf  */
  299. #undef main
  300. /**** name=_srotX ****/
  301. #define main() TEST__srotX()
  302.  
  303. #include <stdlib.h>
  304. #include <stdio.h>
  305. void main() {
  306.    unsigned short int k=1, m=0x8000;
  307.    printf("Rotate left and right by"
  308.           " one bit at a time:\n");
  309.    do {
  310.       printf("\t%4.4x\t\t%4.4x\n", k, m);
  311.       k = _srotl(k, 1);
  312.       m = _srotr(m, 1);
  313.       } while (k != 1);
  314.    }
  315.  
  316. /* End _srot*  */
  317. #undef main
  318. /**** name=sscanf ****/
  319. #define main() TEST_sscanf()
  320.  
  321. /*
  322.    A similar example for fscanf shows the slight difference between the two
  323.    functions.
  324. */
  325.  
  326. #include <stdio.h>
  327.  
  328. void main() {
  329.    char s1[11];
  330.    char s2[19] = "but missed eleven.";
  331.    char s3[19] = "but missed eleven.";
  332.    char input1[39] = "scanf read 10 %11 and quit.za";
  333.    char input2[39] = "It read 9, 10 11 and quit.za";
  334.    int i, j = 2, k = 2;
  335.  
  336.    sscanf(input1,"%11c%d %%%d %[^z]z",s1,&i,&j,s2);
  337.    printf("%11s%d, %s%s\n", s1, i,
  338.            j == 11 ? "11, " : "", s2);
  339.    sscanf(input2,"%11c%d %%%d %[^z]z",s1,&i, &k,s3);
  340.    printf("%11s%d, %s%s\n", s1, i,
  341.            k == 11 ? "11, " : "", s3);
  342.    }
  343.  
  344. /* End sscanf  */
  345. #undef main
  346. /**** name=_stat ****/
  347. #define main() TEST__stat()
  348.  
  349. /*
  350.    Display information about a user-specified file.
  351. */
  352.  
  353. #include <time.h>
  354. #include <sys/types.h>
  355. #include <sys/stat.h>
  356. #include <stdio.h>
  357. #include <dos.h>
  358. #include <string.h>
  359. #include <stdlib.h>
  360.  
  361. void main() {
  362.    struct _stat buf;
  363.    char fname[25];
  364.    printf("Stats on file name: ");
  365.    gets(fname);
  366.    if (_stat(fname, &buf) != 0)
  367.       perror("Problem getting info.");
  368.    else {
  369.       (buf.st_mode & S_IFDIR) ? puts("directory")
  370.       : puts("not a directory");
  371.       (buf.st_mode & S_IFREG) ? puts("ordinary file")
  372.       : puts("not an ordinary file");
  373.       (buf.st_mode & S_IWRITE)? puts("read/write file")
  374.       : puts("read-only file");
  375.       (buf.st_mode & S_IEXEC) ? puts("executable file")
  376.       : puts("not an executable file");
  377.       printf("File size    : %ld\n", buf.st_size);
  378.       printf("Drive number : %d\n", buf.st_dev);
  379.       printf("Time modified: %s", ctime(&buf.st_atime));
  380.       }
  381.    }
  382.  
  383. /* End _stat  */
  384. #undef main
  385. /**** name=_status87 ****/
  386. #define main() TEST__status87()
  387.  
  388. /*
  389.    Program to test _status87 and _stat387.
  390. */
  391.  
  392. #include <stdio.h>
  393. #include <float.h>
  394. #include <math.h>
  395.  
  396. void main() {
  397. #ifdef _I387
  398.  
  399.    double a = 1e-40, b;
  400.    float x, y;
  401.  
  402.    /*
  403.       Exceptions are sticky, what do they look like starting out?
  404.    */
  405.    printf("Status = %.4x - start\n",_status87());
  406.    printf("Stat387= %.4x - start\n",_stat387());
  407.  
  408.    _clear87();  /* Clear whatever is there.                              */
  409.  
  410.    printf("Status = %.4x - clear\n",_status87());
  411.    printf("Stat387= %.4x - clear\n",_stat387());
  412.  
  413.    /* Force underflow by assigning a double to                           */
  414.  
  415.    /*  a float.                                                          */
  416.    y = a;
  417.    printf("Status = %.4x - inexact,"
  418.           " underflow\n",_status87());
  419.    printf("Stat387= %.4x - inexact,"
  420.           " underflow\n",_stat387());
  421.  
  422.    /* y became denormal on underflow, assign it                          */
  423.    /*  to a double.                                                      */
  424.    /* Notice that the previous exceptions stuck.                         */
  425.  
  426.    b = y;
  427.    printf("Status = %.4x - inexact,"
  428.           " underflow, denormal\n",_status87());
  429.    printf("Stat387= %.4x - inexact,"
  430.           " underflow, denormal\n",_stat387());
  431.  
  432.    _clear87(); /* Clean up the mess.                                     */
  433.    printf("Status = %.4x - clear\n",_status87());
  434.    printf("Stat387= %.4x - clear\n",_stat387());
  435. #endif
  436.    }
  437.  
  438. /* End _status87  */
  439. #undef main
  440. /**** name=_stkdmp ****/
  441. #define main() TEST__stkdmp()
  442.  
  443. #include <stdio.h>
  444. /*
  445.    Call a few functions  which  will call _stkdmp and display their calling
  446.    information.  Note that the program will write to the file  handle given
  447.    in main, though this is not obvious.
  448. */
  449. void f1(int n) { _stkdmp(n); }
  450. void f2(int n, int m) { f1(n+m); }
  451. void f3(int j) { f2(j-3, 3); }
  452.  
  453. void main() {
  454.    /*
  455.       Write to file handle 1: stdout by default.
  456.    */
  457.    f3(1);
  458.    }
  459.  
  460. /* End _stkdmp  */
  461. #undef main
  462. /**** name=strcat ****/
  463. #define main() TEST_strcat()
  464.  
  465. /*
  466.    Collect  up  to  ten lines from test.dat into a buffer lines; then print
  467.    the buffer.  This example illustrates the use of strcat and strncat.
  468. */
  469.  
  470. #include <string.h>
  471. #include <stdio.h>
  472. #define  LINESIZE    (256)
  473. #define  OUTPUTLIMIT (3000)
  474.  
  475. int main() {
  476.    int  line_count = 0;
  477.    FILE *F1;
  478.    char lines[OUTPUTLIMIT] = "", line[LINESIZE], *s;
  479.    int  i = OUTPUTLIMIT, j = 0;
  480.    if ((F1 = fopen("test.dat","r"))==NULL) {
  481.       perror("test.dat");
  482.       return 2;
  483.       }
  484.    while (line_count < 10) {
  485.       line_count++;
  486.                                          /* Read a line and point at it. */
  487.       s = fgets(line, LINESIZE , F1);
  488.                                      /* If no line was read, we're done. */
  489.       if (s == NULL) break;
  490.                                           /* Append to lines as much of  */
  491.                                           /*  the line as will fit.      */
  492.       if (i < (j = strlen(line))) {
  493.          strncat(lines, line, i);
  494.          break;
  495.          }
  496.       else {
  497.          strcat(lines, line);
  498.          i -= j;
  499.          }
  500.       }
  501.    puts(lines);                                      /* Print the lines. */
  502.    }
  503.  
  504. /* End strcat  */
  505. #undef main
  506. #undef   LINESIZE
  507. #undef   OUTPUTLIMIT
  508. /**** name=_strcats ****/
  509. #define main() TEST__strcats()
  510.  
  511. #include <string.h>
  512. #include <stdio.h>
  513.  
  514. void main() {
  515.    char s1[65] = "This ";
  516.    char s2[] = "string ";
  517.    char s3[] = "contains ";
  518.  
  519.    char s4[] = "fifty ";
  520.    char s5[] = "characters ";
  521.    char s6[] = "more ";
  522.    char s7[] = "or ";
  523.    char s8[] = "less";
  524.    char s9[] = ".\n";
  525.    char s10[] = "More in fact than can be printed.";
  526.    _strcats(65, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, NULL);
  527.    puts(s1);
  528.    }
  529.  
  530. /* End _strcats  */
  531. #undef main
  532. /**** name=strchr ****/
  533. #define main() TEST_strchr()
  534.  
  535. #include <string.h>
  536. #include <stdio.h>
  537.  
  538. void main() {
  539.    char *s1 = "Testing for strchr.";
  540.    char *s2 = "This is correctly tested.";
  541.    char *s;
  542.    
  543.    s = strchr(s1, (int)'t');
  544.    puts(s);
  545.    s = strchr(s2, (int)'c');
  546.    puts(s);
  547.    }
  548.  
  549. /* End strchr  */
  550. #undef main
  551. /**** name=_strXcmpX ****/
  552. #define main() TEST__strXcmpX()
  553.  
  554. #include <string.h>
  555. #include <stdio.h>
  556.  
  557. void main() {
  558.    char a[] = "tHIS iS a tEST Z";
  559.    char b[] = "This Is A Test z";
  560.    char c[] = "[]";
  561.    char d[] = "{}";
  562.    char e[] = "1234";
  563.    char f[] = "5678";
  564.  
  565.    if (_strcmpi(a,b) == 0)
  566.       puts("_strcmpi equal test ok.");
  567.    else puts("_strcmpi equal test failed.");
  568.    if (_strcmpi(c,d) == 0)
  569.       puts("_strcmpi unequal test failed.");
  570.    else puts("_strcmpi unequal test ok.");
  571.    if (_strcmpi(e,e) == 0)
  572.       puts("_strcmpi equal2 test ok.");
  573.    else puts("_strcmpi equal2 test failed.");
  574.    if (_strcmpi(e,f) == 0)
  575.       puts("_strcmpi unequal test failed.");
  576.    else puts("_strcmpi unequal test ok.");
  577.    }
  578.  
  579. /* End _str*cmp*  */
  580. #undef main
  581. /**** name=strcmp ****/
  582. #define main() TEST_strcmp()
  583.  
  584. /*
  585.    The program below compares test.dat and test2.dat line by line.
  586. */
  587.  
  588. #include <string.h>
  589. #include <stdio.h>
  590.  
  591. #define EQUAL    (1)
  592. #define UNEQUAL  (0)
  593. #define FAILURE  (-1)
  594. #define TRUE     (1)
  595. #define LINESIZE (256)
  596.  
  597. int main() {
  598.    FILE *F1, *F2;
  599.    char line1[LINESIZE], line2[LINESIZE], *s1, *s2;
  600.    int i = 1;
  601.       /* Open the files to be compared.  If the                          */
  602.       /*  files cannot be opened, return an error                        */
  603.       /*  code to the calling environment.                               */
  604.    if ((F1 = fopen("test.dat","r")) == NULL) {
  605.       perror("Cannot open test.dat");
  606.       return FAILURE;
  607.       }
  608.    if ((F2 = fopen("test2.dat","r")) == NULL) {
  609.       perror("Cannot open test2.dat");
  610.       return FAILURE;
  611.       }
  612.    while (TRUE) {
  613.          /* Read a line from each file.                                  */
  614.       s1 = fgets(line1, LINESIZE, F1);
  615.       s2 = fgets(line2, LINESIZE, F2);
  616.          /* A NULL return from fgets means end-of-                       */
  617.          /*  file was found.  If both strings are                        */
  618.          /*  NULL, the files are equal, if only one                      */
  619.          /*  is NULL, they are not equal.                                */
  620.       if ((s1 == NULL) || (s2 == NULL)) {
  621.          if (s1 == s2) break;
  622.          printf("Files diverge at line %d.", i);
  623.          return UNEQUAL;
  624.          }
  625.          /* Compare the strings.                                         */
  626.  
  627.       if (strcmp(line1,line2)) {
  628.          printf("Files diverge at line %d.", i);
  629.          return UNEQUAL;
  630.          }
  631.       else i++;
  632.       }
  633.    printf("Files are identical.");
  634.    return EQUAL;
  635.    }
  636.  
  637. /* End strcmp  */
  638. #undef main
  639. #undef  EQUAL
  640. #undef  UNEQUAL
  641. #undef  FAILURE
  642. #undef  TRUE
  643. #undef  LINESIZE
  644. /**** name=strcpy ****/
  645. #define main() TEST_strcpy()
  646.  
  647. /*
  648.    The  function  below  returns the last word of the string  line  in  the
  649.    string word.  This example illustrates the use of strcpy and strrchr.
  650. */
  651.  
  652. #include <string.h>
  653.  
  654. char *last_word (char *line, char *word) {
  655.    char *s;
  656.  
  657.    /* Find the last space in line.                                       */
  658.    s = strrchr(line,' ');
  659.    /* If not found, line itself is the last word                         */
  660.    if (s == NULL) s = line;
  661.    /* Else the last word starts after the space.                         */
  662.    else s++;
  663.    /* Copy the word into word.                                           */
  664.    return strcpy(word, s);
  665.    }
  666.  
  667. void main() {
  668.    char word[50];
  669.    puts(last_word("ignore all words until strcpy-ok",word));
  670.    }
  671. /* End strcpy  */
  672. #undef main
  673. /**** name=strcspn ****/
  674. #define main() TEST_strcspn()
  675. /* End strcspn  */
  676. #undef main
  677. /**** name=_strdup ****/
  678. #define main() TEST__strdup()
  679.  
  680. #include <string.h>
  681. #include <stdio.h>
  682.  
  683. void main() {
  684.    char a[100];
  685.    int j;
  686.  
  687.    for (j = 0; j < 99; j++)
  688.       a[j] = 1;
  689.    a[99] = 0;
  690.    j = 0;
  691.    do j++;
  692.    while (NULL != _strdup(a));
  693.    printf("_strdup %d00 bytes before running"
  694.           " out of heap.\n",j);
  695.    }
  696.  
  697. /* End _strdup  */
  698. #undef main
  699. /**** name=Xstrerror ****/
  700. #define main() TEST_Xstrerror()
  701.  
  702. /*
  703.    This program prints a few system error messages.
  704. */
  705.  
  706. #include <stdio.h>
  707. #include <string.h>
  708. #include <stdlib.h>
  709.  
  710. void main() {
  711.    int i;
  712.  
  713.    /* Print error messages in strerror format. */
  714.    puts("A few error messages:\n");
  715.    for(i = 0; i <= 10; i++)
  716.        printf(strerror(i));
  717.  
  718.    /* Now use _strerror format. */
  719.    puts("\n\nA few more with a different format:\n");
  720.    for(i = 0; i <= 10; i++) {
  721.       errno = i;
  722.       printf(_strerror("This is an error message"));
  723.       }
  724.    }
  725.  
  726. /* End *strerror  */
  727. #undef main
  728. /**** name=strlen ****/
  729. #define main() TEST_strlen()
  730.  
  731. /*
  732.    The function below copies the individual words of  the  line  line  to a
  733.    buffer, NUL-terminating each copy.  The words are indexed  by  an  array
  734.    pword of pointers.  The number of words found is returned.  This example
  735.    illustrates the use of strlen, strncpy, and strpbrk.
  736. */
  737.  
  738. #include <string.h>
  739.  
  740. int wordify (char *line, char *word, char **pword) {
  741.    char *s = line;
  742.    int i = 0, j;
  743.    /* While not at the end of the string:                                */
  744.    while (s != NULL) {
  745.       pword[i++] = word;
  746.          /* Find the end of a word.                                      */
  747.       s = strpbrk(line, " ");
  748.          /* Find the length of the word.                                 */
  749.       j = (s == NULL ? strlen(line) : s - line);
  750.          /* Copy the word.                                               */
  751.       strncpy(word,line,j);
  752.       word[j] = 0;
  753.       word += j + 1;
  754.       line += j + 1;
  755.       }
  756.    return (i);
  757.    }
  758. void main() {
  759.    char *pword[30];
  760.    char wrd[50];
  761.    printf("number of words is: %d\n",
  762.       wordify("these are some words",wrd,pword));
  763.    }
  764. /* End strlen  */
  765. #undef main
  766. /**** name=_strlwr ****/
  767. #define main() TEST__strlwr()
  768.  
  769. #include <string.h>
  770. #include <stdio.h>
  771.  
  772. void main() {
  773.    char a[97];
  774.    char *c;
  775.    int j;
  776.  
  777.    for (j = 0; j < 64; j++)
  778.       a[j] = j + 33;
  779.    a[64] = '\n';
  780.    for (j = 65; j < 95; j++)
  781.       a[j] = j + 32;
  782.    a[96] = 0;
  783.    printf("The original string:\n\n%s\n\n",a);
  784.    c = _strlwr(a);
  785.    printf("  The _strlwr string:\n\n%s\n\n",a);
  786.    printf("  The result string:\n\n%s\n",c);
  787.    }
  788.  
  789. /* End _strlwr  */
  790. #undef main
  791. /**** name=strncat ****/
  792. #define main() TEST_strncat()
  793.  
  794. #include <stdio.h>
  795. #include <string.h>
  796. void main() {
  797.    char a[50] = "Testing for ";
  798.    char b[30] = "strncat successfully failed";
  799.    char c[10] = ".";
  800.    char buf[50];
  801.  
  802.    strcpy (buf,"Testing for strncat successful");
  803.    if (strcmp(buf, strncat(a, b, (size_t)18)) == 0)
  804.       puts(a);
  805.    else puts(strncat(a, b, (size_t)30));
  806.    strncat(a, c, (size_t)30);
  807.    puts (a);
  808.    strncat(buf, a, 50 - strlen(buf));
  809.    puts(buf);
  810.    }
  811.  
  812. /* End strncat  */
  813. #undef main
  814. /**** name=_strncat ****/
  815. #define main() TEST__strncat()
  816.  
  817. #include <string.h>
  818. #include <stdio.h>
  819.  
  820. void main() {
  821.    char string1[50] =
  822.       "This string contains forty characters.  ";
  823.    char string2[50] =
  824.       "Plus ten. This sentence will be ignored.";
  825.    _strncat(string1, string2, 50);
  826.    puts(string1);
  827.    }
  828.  
  829. /* End _strncat  */
  830. #undef main
  831. /**** name=strncmp ****/
  832. #define main() TEST_strncmp()
  833. /* End strncmp  */
  834. #undef main
  835. /**** name=strncpy ****/
  836. #define main() TEST_strncpy()
  837. /* End strncpy  */
  838. #undef main
  839. /**** name=_strnicmp ****/
  840. #define main() TEST__strnicmp()
  841.  
  842. #include <string.h>
  843. #include <stdio.h>
  844.  
  845. void main() {
  846.    char a[] = "tHIS iS a tEST Z1";
  847.    char b[] = "This Is A Test z2";
  848.    char aa[] = "tHIS iS a tEST ZA1";
  849.    char bb[] = "This Is A Test za2";
  850.    char c[] = "[]", d[] = "{}";
  851.    char e[] = "1234", f[] = "5678";
  852.  
  853.    if (_strnicmp(a,b,(size_t)16) == 0)
  854.       puts("_strnicmp equal test ok");
  855.    else puts("_strnicmp equal test failed");
  856.    if (_strnicmp(c,d,(size_t)16) == 0)
  857.       puts("_strnicmp unequal test failed");
  858.    else puts("_strnicmp unequal test ok");
  859.    if (_strnicmp(e,e,(size_t)16) == 0)
  860.       puts("_strnicmp equal2 test ok");
  861.    else puts("_strnicmp equal2 test failed");
  862.    if (_strnicmp(e,f,(size_t)16) == 0)
  863.       puts("_strnicmp unequal test failed");
  864.    else puts("_strnicmp unequal test ok");
  865.    if (_strnicmp(a,b,(size_t)17) == 0)
  866.       puts("_strnicmp equal3 test failed");
  867.    else puts("_strnicmp equal3 test ok");
  868.    if (_strnicmp(a,b,(size_t)18) == 0)
  869.       puts("_strnicmp equal4 test failed");
  870.    else puts("_strnicmp equal4 test ok");
  871.    if (_strnicmp(aa,bb,(size_t)17) == 0)
  872.       puts("_strnicmp equal5 test ok");
  873.    else puts("_strnicmp equal5 test failed");
  874.    }
  875.  
  876. /* End _strnicmp  */
  877. #undef main
  878. /**** name=_strnset ****/
  879. #define main() TEST__strnset()
  880.  
  881. #include <string.h>
  882. #include <stdio.h>
  883.  
  884. void main() {
  885.    char a[] = "**failed** _strnset is working";
  886.    char *b;
  887.  
  888.    b = _strnset(a, 32, 10);
  889.    puts(a);
  890.    puts(b);
  891.    b = _strnset(a, '-', 55);
  892.    b = _strnset(a, 32, 11);
  893.    puts(a);
  894.    }
  895.  
  896. /* End _strnset  */
  897. #undef main
  898. /**** name=strpbrk ****/
  899. #define main() TEST_strpbrk()
  900. /* End strpbrk  */
  901. #undef main
  902. /**** name=strrchr ****/
  903. #define main() TEST_strrchr()
  904.  
  905. #include <stdio.h>
  906. #include <string.h>
  907.  
  908. void main() {
  909.    char *s = "This is a test for strrchr.";
  910.    char *p = "                        ";
  911.    int c = ' ';
  912.  
  913.    p = strrchr(s, c);
  914.    puts(p);
  915.    
  916.    c = 's';
  917.    p = strrchr(s, c);
  918.    puts(p);
  919.  
  920.    c = 'i';
  921.    p = strrchr(s, c);
  922.    puts(p);
  923.    
  924.    c = 'r';
  925.    p = strrchr(s, c);
  926.    puts(p);
  927.    }
  928.  
  929. /* End strrchr  */
  930. #undef main
  931. /**** name=_strrev ****/
  932. #define main() TEST__strrev()
  933.  
  934. #include <string.h>
  935. #include <stdio.h>
  936.  
  937. void main() {
  938.    char a[] = "abcdefghijklmnopqrstuvwxyz";
  939.    char b[] = "123456789";
  940.  
  941.    puts("_strrev - string reversal");
  942.    puts(a); puts(_strrev(a));
  943.    puts(b); puts(_strrev(b));
  944.    }
  945.  
  946. /* End _strrev  */
  947. #undef main
  948. /**** name=_strset ****/
  949. #define main() TEST__strset()
  950.  
  951. #include <string.h>
  952. #include <stdio.h>
  953.  
  954. void main() {
  955.    char a[] = "_strset is working";
  956.    char *b;
  957.  
  958.    puts(a);
  959.    b = _strset(a, '-');
  960.    puts(b);
  961.    b = _strset(a, '+');
  962.    puts(a);
  963.    }
  964.  
  965. /* End _strset  */
  966. #undef main
  967. /**** name=strstr ****/
  968. #define main() TEST_strstr()
  969.  
  970. #include <stdio.h>
  971. #include <string.h>
  972.  
  973. void main() {
  974.    char *s1 = "This is testing";
  975.    char *s2 = "This is testing for";
  976.    char *s3 = "This is testing for strstr.";
  977.    char *s4 = " is";
  978.    char *s5 = " for";
  979.    char *s6 = " strstr";
  980.    char *nul = "";
  981.    char *buf = "                                   ";
  982.    char *sp;
  983.  
  984.    puts("Testing strstr...");
  985.    printf("1:%s.\n", strstr(s1, s4));
  986.  
  987.    if ((sp = strstr(s1, s5)) != NULL)
  988.       printf("2:%s.\n", sp);
  989.    printf("3:%s.\n", strstr(s2, s5));
  990.  
  991.    printf("4:%s.\n", strstr(s3, s6));
  992.  
  993.    printf("5:%s.\n", strstr(s1, nul));
  994.  
  995.    if ((sp = strstr(s2, s6)) != NULL)
  996.       printf("6:%s.\n", sp);
  997.    }
  998.  
  999. /* End strstr  */
  1000. #undef main
  1001. /**** name=_strtime ****/
  1002. #define main() TEST__strtime()
  1003.  
  1004. #include <time.h>
  1005. #include <stdio.h>
  1006.  
  1007. void main() {
  1008.    char a[15];
  1009.  
  1010.    _strtime(&a[0]);
  1011.    puts("The time is --");
  1012.    puts(a);
  1013.    }
  1014.  
  1015. /* End _strtime  */
  1016. #undef main
  1017. /**** name=strtod ****/
  1018. #define main() TEST_strtod()
  1019.  
  1020. /*
  1021.    The program below computes the areas of a series of circles, given their
  1022.    diameters.   A  similar  example  for  atof  shows how the two functions
  1023.    differ.
  1024. */
  1025.  
  1026. #include <stdio.h>
  1027. #include <stdlib.h>
  1028. #define   INPUTS (5)
  1029.  
  1030. void main() {
  1031.    char   diameters[] = "1.03 67.94 9.2032e27 4 8e-32";
  1032.    char   *diameter = diameters;
  1033.    double pi = 3.141593;
  1034.    double diam;
  1035.    double area[INPUTS];
  1036.    int    i = 0;
  1037.  
  1038.    /* While not at end of string.                                        */
  1039.    while (*diameter) {
  1040.       diam = strtod(diameter, &diameter);
  1041.       area[i] = pi * diam * diam / 4.0;
  1042.       printf("\ndiam=%-20.5g \tarea=%g",
  1043.               diam, area[i++]);
  1044.       }
  1045.    }
  1046.  
  1047. /* End strtod  */
  1048. #undef main
  1049. #undef    INPUTS
  1050. /**** name=strtok ****/
  1051. #define main() TEST_strtok()
  1052.  
  1053. #include <string.h>
  1054. #include <stdio.h>
  1055.  
  1056. void main() {
  1057.    char separator[12] = "fwy\n 0ru b", sentence[80] =
  1058. "fwThisfruisb frabucomplete0sentence.0\n by itself.\n";
  1059.    printf("%s ", strtok(sentence,separator));
  1060.    printf("%s ", strtok(NULL,separator));
  1061.    printf("%s ", strtok(NULL,separator));
  1062.    printf("%s ", strtok(NULL,separator));
  1063.    printf("%s",  strtok(NULL,separator));
  1064.    printf("%s",  strtok(NULL,"bits of icy leaf\n"));
  1065.    }
  1066.  
  1067. /* End strtok  */
  1068. #undef main
  1069. /**** name=strtol ****/
  1070. #define main() TEST_strtol()
  1071.  
  1072. /*
  1073.    The program below computes the  number of miles travelled during various
  1074.    time units at a speed of  60  miles  per hour.  This example illustrates
  1075.    the use of strtol and atol.  A similar example appears for atoi, showing
  1076.    how these functions differ.
  1077. */
  1078.  
  1079. #include <stdlib.h>
  1080. #include <stdio.h>
  1081. #define INPUTS      5
  1082. #define WEEK        "604800"
  1083. #define DAY         "86400"
  1084. #define HOUR        "07020"                               /* Octal       */
  1085. #define MINUTE      "0x3c"                                /* Hexadecimal */
  1086. #define SECOND      "1"
  1087.  
  1088. void main() {
  1089.    char *sec_list[] = {WEEK, DAY, HOUR, MINUTE, SECOND};
  1090.    char *stopstring;
  1091.    unsigned long seconds = 0;
  1092.    int  i = 0;
  1093.  
  1094.    while (sec_list[i] && *sec_list[i]) {
  1095.       seconds += strtol(sec_list[i], &stopstring, 0);
  1096.       i++;
  1097.       }
  1098.    printf("Total number of seconds in a week, day, hour"
  1099.           ", minute, and second = %ld.\n", seconds);
  1100.    }
  1101.  
  1102. /* End strtol  */
  1103. #undef main
  1104. #undef  INPUTS
  1105. #undef  WEEK
  1106. #undef  DAY
  1107. #undef  HOUR
  1108. #undef  MINUTE
  1109. #undef  SECOND
  1110. /**** name=_strupr ****/
  1111. #define main() TEST__strupr()
  1112.  
  1113. #include <string.h>
  1114. #include <stdio.h>
  1115.  
  1116. void main() {
  1117.    char a[97];
  1118.    char *c;
  1119.    int j;
  1120.  
  1121.    for (j = 0; j < 64; j++)
  1122.         a[j] = j + 33;
  1123.    a[64] = '\n';
  1124.    for (j = 65;j < 95; j++)
  1125.         a[j] = j + 32;
  1126.    a[96] = 0;
  1127.    printf("The original string:\n\n%s\n\n",a);
  1128.    c = _strupr(a);
  1129.    printf("  The _strupr string:\n\n%s\n\n",a);
  1130.    printf("  The result string:\n\n%s\n",c);
  1131.    }
  1132.  
  1133. /* End _strupr  */
  1134. #undef main
  1135. /**** name=_swab ****/
  1136. #define main() TEST__swab()
  1137.  
  1138. #include <stdio.h>
  1139. #include <stdlib.h>
  1140.  
  1141. void main() {
  1142.    char a[] = "abcdefghijkl";
  1143.    char b[13];
  1144.  
  1145.    b[12] = 0;
  1146.    _swab(a, b, 12);
  1147.    puts("_swab - swap bytes");
  1148.    puts(a);
  1149.    puts(b);
  1150.    }
  1151.  
  1152. /* End _swab  */
  1153. #undef main
  1154. /**** name=system ****/
  1155. #define main() TEST_system()
  1156.  
  1157. /*
  1158.    Compile and run this example with the following commands:
  1159.  
  1160.     hc386 system.c -maxreal 0ffffh
  1161.     run386 system
  1162.  
  1163. -- or --
  1164.  
  1165.     hc386 system.c
  1166.     run386 -maxreal 0ffffh system
  1167. */
  1168.  
  1169. #include <stdlib.h>
  1170. void main() {
  1171.    system("dir /w");
  1172.    }
  1173.  
  1174. /* End system  */
  1175. #undef main
  1176.  
  1177. /*****names*****/
  1178.  
  1179. char * names[]={
  1180.    "scanf",
  1181.    "_searchenv",
  1182.    "_segread",
  1183.    "_setmode",
  1184.    "setvbuf",
  1185.    "_skip_char",
  1186.    "_sopen",
  1187.    "_spawnX",
  1188.    "_splitpath",
  1189.    "sprintf",
  1190.    "_srotX",
  1191.    "sscanf",
  1192.    "_stat",
  1193.    "_status87",
  1194.    "_stkdmp",
  1195.    "strcat",
  1196.    "_strcats",
  1197.    "strchr",
  1198.    "_strXcmpX",
  1199.    "strcmp",
  1200.    "strcpy",
  1201.    "_strdup",
  1202.    "Xstrerror",
  1203.    "strlen",
  1204.    "_strlwr",
  1205.    "strncat",
  1206.    "_strncat",
  1207.    "_strnicmp",
  1208.    "_strnset",
  1209.    "strrchr",
  1210.    "_strrev",
  1211.    "_strset",
  1212.    "strstr",
  1213.    "_strtime",
  1214.    "strtod",
  1215.    "strtok",
  1216.    "strtol",
  1217.    "_strupr",
  1218.    "_swab",
  1219.    "system",
  1220.    "",""};
  1221.    int nextfunum;
  1222. void main() {
  1223.    char ans[90];
  1224.    for (;;) {
  1225.       for (int j=0;j< 40;j++)
  1226.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  1227.       else printf("%4d %-21s",j+1,names[j]);
  1228.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  1229.       gets(ans);
  1230.       if (ans[0] != 0) nextfunum=atoi(ans);
  1231.       printf("\n\n\n");
  1232.       switch(nextfunum) {
  1233.          case 0:exit(0);
  1234.          case 1:TEST_scanf();break;
  1235.          case 2:TEST__searchenv();break;
  1236.          case 3:TEST__segread();break;
  1237.          case 4:TEST__setmode();break;
  1238.          case 5:TEST_setvbuf();break;
  1239.          case 6:TEST__skip_char();break;
  1240.          case 7:TEST__sopen();break;
  1241.          case 8:TEST__spawnX();break;
  1242.          case 9:TEST__splitpath();break;
  1243.          case 10:TEST_sprintf();break;
  1244.          case 11:TEST__srotX();break;
  1245.          case 12:TEST_sscanf();break;
  1246.          case 13:TEST__stat();break;
  1247.          case 14:TEST__status87();break;
  1248.          case 15:TEST__stkdmp();break;
  1249.          case 16:TEST_strcat();break;
  1250.          case 17:TEST__strcats();break;
  1251.          case 18:TEST_strchr();break;
  1252.          case 19:TEST__strXcmpX();break;
  1253.          case 20:TEST_strcmp();break;
  1254.          case 21:TEST_strcpy();break;
  1255.          case 22:TEST__strdup();break;
  1256.          case 23:TEST_Xstrerror();break;
  1257.          case 24:TEST_strlen();break;
  1258.          case 25:TEST__strlwr();break;
  1259.          case 26:TEST_strncat();break;
  1260.          case 27:TEST__strncat();break;
  1261.          case 28:TEST__strnicmp();break;
  1262.          case 29:TEST__strnset();break;
  1263.          case 30:TEST_strrchr();break;
  1264.          case 31:TEST__strrev();break;
  1265.          case 32:TEST__strset();break;
  1266.          case 33:TEST_strstr();break;
  1267.          case 34:TEST__strtime();break;
  1268.          case 35:TEST_strtod();break;
  1269.          case 36:TEST_strtok();break;
  1270.          case 37:TEST_strtol();break;
  1271.          case 38:TEST__strupr();break;
  1272.          case 39:TEST__swab();break;
  1273.          case 40:TEST_system();break;
  1274.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  1275.          }
  1276.       printf("\n\npress enter to select another function\n");
  1277.       gets(ans);
  1278.       }
  1279.    }
  1280.