home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / 84 < prev    next >
Encoding:
Text File  |  1992-04-16  |  26.3 KB  |  1,201 lines

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