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

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