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

  1. /*  Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13  */
  2.  
  3. /**** name=fabs ****/
  4. #define main() TEST_fabs()
  5.  
  6. /*
  7.    This programs demonstrate the behavior of the functions  fabs,  abs, and
  8.    cabs, labs.
  9. */
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. void main() {
  15.    struct complex ab;
  16.  
  17.    ab.x = 3.0;
  18.    ab.y = -4.0;
  19.  
  20.    double x=-12.00,  y=3.0,  z=-2.0;
  21.    double result;
  22.    int k=-20, j=-15, i=2;
  23.    long a=50, b=10;
  24.  
  25.    puts("Testing fabs...\n");
  26.    result = fabs(x+z*fabs(z-y*fabs(x)));
  27.    printf("Result of fabs(x+z*fabs(z-y*fabs(x)))"
  28.           " ==> %+.2e.\n",result);
  29.  
  30.    result = fabs(k-fabs((double)j));
  31.    printf("Result of fabs(k-fabs(j))"
  32.           " ==> %+.2e.\n",result);
  33.  
  34.    result = _cabs(ab);
  35.    printf("Result of _cabs(ab)"
  36.           " ==> %.2f.\n",result);
  37.  
  38.    ab.y=abs(x);
  39.    result = _cabs(ab);
  40.    printf("Result of _cabs(ab) when ab.y=abs(x)"
  41.           " ==> %.2lf.\n",result);
  42.  
  43.    printf("Result of abs(i+j*abs(j-k))"
  44.           " ==> %d.\n",abs(i+j*abs(j-k)) );
  45.    printf("Result of labs(a-labs(a*b))"
  46.           " ==> %d.\n", labs(a-labs(a*b)));
  47.    }
  48. /* End fabs  */
  49. #undef main
  50. /**** name=fclose ****/
  51. #define main() TEST_fclose()
  52.  
  53. /*
  54.    The program fragment below attempts to open test.dat for  reading.    It
  55.    later attempts to close test.dat prior to opening fclose.tmp for writing
  56.    (using the same FILE variable).
  57. */
  58.  
  59. #include <stdio.h>
  60. #define  FAILURE (-1)
  61. int main() {
  62.    FILE *FP;
  63.  
  64.    if ((FP = fopen("test.dat", "r")) == NULL) {
  65.       perror("fopen of test.dat");
  66.       return FAILURE;
  67.       }
  68.    if (fclose(FP) != 0)
  69.       perror("fclose of test.dat");
  70.    if ((FP = fopen("fclose.tmp", "w")) == NULL) {
  71.       perror("fopen of fclose.tmp");
  72.       return FAILURE;
  73.       }
  74.    puts("fopen and fclose were successful.");
  75.    }
  76. /* End fclose  */
  77. #undef main
  78. #undef   FAILURE
  79. /**** name=_fcvt ****/
  80. #define main() TEST__fcvt()
  81.  
  82. /*
  83.    This program uses _fcvt to convert the number "E" to a string.
  84. */
  85.  
  86. #include <stdlib.h>
  87. #define E 2.718281828459045
  88.  
  89. void main() {
  90.    char  *result; /* _fcvt return string.                                */
  91.    int    dpos;   /* Stored decimal-point position.                      */
  92.    int    sign;   /* Sign of converted number.                           */
  93.  
  94.    result = _fcvt(E, 5, &dpos, &sign);
  95.    printf("------- _fcvt test --------\n");
  96.    printf(" value: %0.15lf\n", E);
  97.    printf("--------------------------\n");
  98.    printf("resulting string: %s\n decimal point @: %d\n"
  99.           "            sign: %d\n", result, dpos, sign);
  100.    }
  101.  
  102. /* End _fcvt  */
  103. #undef main
  104. #undef  E
  105. /**** name=ferror ****/
  106. #define main() TEST_ferror()
  107.  
  108.  
  109. /*
  110.     The program below tests ferror by calling fseek with an invalid value.
  111. */
  112.  
  113. #include <stdlib.h>
  114. #include <stdio.h>
  115. #define  FAILURE   (-1)
  116.  
  117. int main() {
  118.    FILE *FP1;
  119.  
  120.    puts("Testing ferror...\n");
  121.    if((FP1 = fopen("test.dat", "r")) == NULL) {
  122.       perror("fopen of test.dat failed.\n");
  123.       return FAILURE;
  124.       }
  125.  
  126.    /* call fseek with invalid whence */
  127.    fseek (FP1, 0, -1);
  128.    if(ferror(FP1))
  129.       perror("ERROR seeking test.dat");
  130.  
  131.    fclose (FP1);
  132.    }
  133. /* End ferror  */
  134.  
  135. #undef main
  136. #undef   SIZE
  137. #undef   FAILURE
  138. /**** name=fgetc ****/
  139. #define main() TEST_fgetc()
  140.  
  141. /*
  142.    This program copies test.dat character  by character to fgetc.tmp and to
  143.    standard  output.    This  illustrates  the  use  of fgetc,  fputc,  and
  144.    fputchar.
  145. */
  146. #include <stdio.h>
  147.  
  148. void main() {
  149.    FILE *FP1, *FP2;
  150.  
  151.    if ((FP1 = fopen("test.dat", "r")) == NULL)
  152.       perror("fopen of test.dat");
  153.    if ((FP2 = fopen("fgetc.tmp", "w")) == NULL)
  154.       perror("fopen of fgetc.tmp");
  155.    while (!feof(FP1))
  156.       fputc(fputchar(fgetc(FP1)), FP2);
  157.    }
  158.  
  159. /* End fgetc  */
  160. #undef main
  161. /**** name=fgetpos ****/
  162. #define main() TEST_fgetpos()
  163.  
  164. /*
  165.    This program opens test.dat for read and fgetpos.tmp for  writing.    It
  166.    copies  data  from  test.dat to fgetpos.tmp until four lines are copied.
  167.    Then the file position is set.  Next, a line is  read  and  printed, the
  168.    file position is reset, and data copying continues.
  169. */
  170.  
  171. #include <stdio.h>
  172. #define SIZE    (15)
  173. #define FAILURE (-1)
  174.  
  175. int main() {
  176.    FILE *FP1, *FP2;
  177.    char s[SIZE];
  178.    int i = 0;
  179.    fpos_t *pos;
  180.  
  181.    puts("Testing fgetpos...\n");
  182.  
  183.    if ((FP1 = fopen("test.dat", "r")) == NULL) {
  184.       perror("fopen of test.dat fails.");
  185.       return FAILURE;
  186.       }
  187.    else puts ("test.dat is open.");
  188.    if ((FP2 = fopen("fgetpos.tmp", "w")) == NULL) {
  189.       perror("fopen of fgetpos.tmp fails.");
  190.       return FAILURE;
  191.       }
  192.    else puts ("fgetpos.tmp is open for writing.");
  193.    if (FP1 && FP2) {                         /* Check for file pointers. */
  194.       while (i < 5) {
  195.          /* Print if something was read. */
  196.          if(fgets(s,SIZE,FP1)) fputs(s,FP2);
  197.          else fprintf(FP2,"** EOF IS DETECTED. **\n");
  198.          i++;
  199.          }
  200.       fgetpos(FP1,pos);
  201.       while (i < 7) {
  202.          /* Print if something was read. */
  203.          if(fgets(s,SIZE,FP1)) fputs(s,FP2);
  204.          else fprintf(FP2,"** EOF IS DETECTED. **\n");
  205.          i++;
  206.          }
  207.       fsetpos(FP1,pos);
  208.       while (!feof(FP1)) {
  209.          /* Print if something was read. */
  210.          if(fgets(s,SIZE,FP1)) fputs(s,FP2);
  211.          else fprintf(FP2,"** EOF IS DETECTED. **\n");
  212.          }
  213.       }
  214.    fclose(FP1);
  215.    fclose(FP2);
  216.    puts("fgetpos test complete.");
  217.    }
  218. /* End fgetpos  */
  219. #undef main
  220. #undef  SIZE
  221. #undef  FAILURE
  222. /**** name=fgets ****/
  223. #define main() TEST_fgets()
  224.  
  225. /*
  226.    The program  below  copies  test.dat  line  by  line to fgets.tmp.  This
  227.    example illustrates the use of fgets, fputs, and fopen.
  228. */
  229.  
  230. #include <stdio.h>
  231. #define  LINESIZE (80)
  232.  
  233. void main() {
  234.    FILE *FP1, *FP2;
  235.    char line[LINESIZE];
  236.  
  237.    if ((FP1 = fopen("test.dat", "r")) == NULL)
  238.       perror("cannot open test.dat");
  239.    if ((FP2 = fopen("fgets.tmp", "w")) == NULL)
  240.       perror("cannot open fgets.tmp");
  241.    while (fgets(line, LINESIZE, FP1)) {
  242.          /* fgets returns NULL (FALSE) when it                           */
  243.          /* cannot get any more characters.                              */
  244.       fputs(line, stdout);
  245.       fputs(line, FP2);
  246.       }
  247.    }
  248. /* End fgets  */
  249. #undef main
  250. #undef   LINESIZE
  251. /**** name=_fieeetomsbin ****/
  252. #define main() TEST__fieeetomsbin()
  253. /* End _fieeetomsbin  */
  254. #undef main
  255. /**** name=__FILE__ ****/
  256. #define main() TEST___FILE__()
  257. /* End __FILE__  */
  258. #undef main
  259. /**** name=_filelength ****/
  260. #define main() TEST__filelength()
  261.  
  262. /*
  263.    This program determines the length of a file.
  264. */
  265.  
  266. #include <stdio.h>
  267. #include <io.h>
  268.  
  269. FILE *stream;
  270. long length;
  271.  
  272. void main() {
  273.    /* Must open file first. */
  274.    stream = fopen("test.dat", "r");
  275.    length = _filelength (fileno(stream));
  276.    if (length == -1L)
  277.       puts("Could not find file test.dat.");
  278.    else
  279.       printf("test.dat is %ld bytes long.\n", length);
  280.    }
  281.  
  282. /* End _filelength  */
  283. #undef main
  284. /**** name=_fileno ****/
  285. #define main() TEST__fileno()
  286.  
  287. /*
  288.    Program to obtain the predefined file handles.
  289. */
  290. #include <stdio.h>
  291.  
  292. void TEST__fileno() {
  293.  
  294.    puts("Predefined file handles:");
  295.    puts("\nDevice           handle");
  296.    printf("Standard input     %d\n", _fileno(stdin));
  297.    printf("Standard output    %d\n", _fileno(stdout));
  298.    printf("Standard error     %d\n", _fileno(stderr));
  299.    }
  300. /* End _fileno  */
  301. #undef main
  302. /**** name=_fmalloc ****/
  303. #define main() TEST__fmalloc()
  304.  
  305. #include <stdio.h>
  306. #include <malloc.h>
  307.  
  308. void main() {
  309.    _Far char *new_array;
  310.  
  311.    new_array = _fmalloc(15000 * sizeof(long));
  312.  
  313.    if (new_array == NULL) {
  314.       printf("_fmalloc: Not enough memory"
  315.              " for 15,000 longs\n");
  316.       new_array = _fmalloc(5000 * sizeof(long));
  317.       if (new_array == NULL)
  318.          printf("_fmalloc: Not enough memory"
  319.                 " for 5,000 longs\n");
  320.       else
  321.          printf("_fmalloc: Allocated 5,000 longs"
  322.                 " at %x:%x\n",
  323.                 (unsigned long) (_Near) new_array >> 16,
  324.                 (unsigned long) (_Near) new_array);
  325.       }
  326.    else
  327.       printf("_fmalloc: Allocated 15,000 longs"
  328.              " at %x:%x\n",
  329.              (unsigned long) (_Near) new_array >> 16,
  330.              (unsigned long) (_Near) new_array);
  331.    }
  332. /* End _fmalloc  */
  333. #undef main
  334. /**** name=_fmsbintoieee ****/
  335. #define main() TEST__fmsbintoieee()
  336. /* End _fmsbintoieee  */
  337. #undef main
  338. /**** name=fopen ****/
  339. #define main() TEST_fopen()
  340. /* End fopen  */
  341. #undef main
  342. /**** name=_FP_X ****/
  343. #define main() TEST__FP_X()
  344.  
  345. /*
  346.    This program displays the segment and offset of a far pointer.
  347. */
  348. #include <dos.h>
  349. #include <stdio.h>
  350.  
  351. void main() {
  352.    struct ovly {
  353.       int offs;
  354.       short segs;
  355.       };
  356.    union {
  357.       _Far char *a;
  358.       struct ovly q;
  359.       } x;
  360.    unsigned int seg, off;
  361.  
  362.    x.q.offs = 320;     /* Line two, column one. */
  363.    x.q.segs = 0x1C;    /* Phar Lap screen segment. */
  364.    puts("\nchar *a is stored at: ");
  365.  
  366.    seg = _FP_SEG(x.a);
  367.    off = _FP_OFF(x.a);
  368.    printf("Segment: 0x%.4x\n"
  369.           "Offset : 0x%.4x\n", seg, off);
  370.    }
  371. /* End _FP_*  */
  372. #undef main
  373. /**** name=fprintf ****/
  374. #define main() TEST_fprintf()
  375.  
  376. #include <stdio.h>
  377. #define  FAILURE  (-1)
  378.  
  379. int main() {
  380.    FILE   *FP;
  381.    char    s[14] = "like this one", c = '*';
  382.    int     i = 10, x = 255, o = 45;
  383.    double  d = 3.1415927, nf = -3.449123;
  384.  
  385.    if ((FP = fopen("fprintf.tmp", "w")) == NULL) {
  386.       perror("Cannot open fprintf.tmp.");
  387.       return FAILURE;
  388.       }
  389.  
  390.    fprintf(FP,"fprintf prints strings (%s),\n", s);
  391.    fprintf(FP,"decimal numbers(%d),", i);
  392.    fprintf(FP," hex numbers(%04X),\n", x);
  393.    fprintf(FP,"floating-point numbers (%+.4e)\n", d);
  394.    fprintf(FP,"percent signs(%%),\n");
  395.    fprintf(FP,"characters (%-5c),\n", c);
  396.    fprintf(FP,"negative floating-points"
  397.            " (% 010.2e)\n", nf);
  398.    fprintf(FP,"and even octal numbers (%-+#5o).", o);
  399.    printf("fprintf test complete.");
  400.    }
  401.  
  402. /* End fprintf  */
  403. #undef main
  404. #undef   FAILURE
  405. /**** name=fputc ****/
  406. #define main() TEST_fputc()
  407. /* End fputc  */
  408. #undef main
  409. /**** name=_fputchar ****/
  410. #define main() TEST__fputchar()
  411. /* End _fputchar  */
  412. #undef main
  413. /**** name=fputs ****/
  414. #define main() TEST_fputs()
  415. /* End fputs  */
  416. #undef main
  417. /**** name=fread ****/
  418. #define main() TEST_fread()
  419.  
  420. /*
  421.    The  program below copies test.dat, a block at  a  time,  to  fread.tmp.
  422.    This example illustrates the use of fread and fwrite.
  423. */
  424. #include <stdio.h>
  425. #define  FAILURE   (-1)
  426. #define  BLOCKSIZE (128)
  427. #define  MAXBUF (256)
  428.  
  429. int main() {
  430.    char  buf[MAXBUF];
  431.    FILE *FP1, *FP2;
  432.    size_t i,j;
  433.  
  434.    puts("Testing fread...\n");
  435.    if ((FP1 = fopen("test.dat", "r")) == NULL) {
  436.       perror("Cannot open test.dat");
  437.       return FAILURE;
  438.       }
  439.    if ((FP2 = fopen("fread.tmp", "w")) == NULL) {
  440.       perror("Cannot open fread.tmp.");
  441.       return FAILURE;
  442.       }
  443.    i = fread(buf, sizeof(char), BLOCKSIZE, FP1); 
  444.    j = fwrite(buf, sizeof(char), i, FP2);
  445.    printf("i = %d, and j = %d.\n",i,j);
  446.    if (fclose(FP1) == 0)
  447.       puts("File test.dat is now closed.");
  448.    else puts("Unable to close file test.dat.");
  449.    if (fclose(FP2) == 0)
  450.       puts("File fread.tmp is now closed.");
  451.    else puts("Unable to close file fread.tmp.");
  452.    }
  453.  
  454. /* End fread  */
  455. #undef main
  456. #undef   FAILURE
  457. #undef   BLOCKSIZE
  458. #undef   MAXBUF
  459. /**** name=freopen ****/
  460. #define main() TEST_freopen()
  461.  
  462. /*
  463.    The program below first  attempts  to open freopen1.tmp for writing.  If
  464.    the  open  succeeds,  it  associates  a  number  of file  pointers  with
  465.    freopen1.tmp.    The  subsequent  call  to  freopen  attempts  to  close
  466.    freopen1.tmp and open freopen2.tmp using the same FILE  variable.   That
  467.    causes all file  pointers  previously  associated  with  freopen1.tmp to
  468.    reference freopen2.tmp.  If the pair of calls fclose/fopen were  used in
  469.    the place of freopen, each file pointer would have  to  be  individually
  470.    updated.
  471. */
  472.  
  473. #include <stdio.h>
  474. #define  FAILURE (-1)
  475. int main() {
  476.    FILE *Current_file, *Log_file, *Out_file, *Err_file;
  477.  
  478.    if((Current_file=fopen("freopen1.tmp","w"))==NULL) {
  479.      perror("Cannot open freopen1.tmp");
  480.      return FAILURE;
  481.      }
  482.    Log_file = Out_file = Err_file = Current_file;
  483.    printf("Log_file     =>%x\n",Log_file);
  484.    printf("Out_file     =>%x\n",Out_file);
  485.    printf("Err_file     =>%x\n",Err_file);
  486.    printf("Current_file =>%x.\n\n",Current_file);
  487.    if((Current_file =
  488.      freopen("freopen2.tmp","w",Current_file))==NULL) {
  489.       perror("freopen of freopen2.tmp");
  490.       return FAILURE;
  491.       }
  492.    else puts("freopen2.tmp is open.");
  493.    }
  494. /* End freopen  */
  495. #undef main
  496. #undef   FAILURE
  497. /**** name=frexp ****/
  498. #define main() TEST_frexp()
  499.  
  500. #include <stdio.h>
  501. #include <math.h>
  502.  
  503. void main() {
  504.    int exp;
  505.    double x, num=73.0;
  506.  
  507.    puts("Testing frexp...\n");
  508.    x = frexp(num, &exp);
  509.    printf("For The Number ==> %e.\n",num); 
  510.    printf("Fractional Part => %e,\n"
  511.           "Exponent Part   => %d.\n", x, exp);
  512.    }
  513.  
  514. /* End frexp  */
  515. #undef main
  516. /**** name=fscanf ****/
  517. #define main() TEST_fscanf()
  518.  
  519. #include <stdio.h>
  520. #define  FAILURE  (-1)
  521.  
  522. int main() {
  523.    FILE *FP;
  524.    char  s1[11], s2[19] = "but missed eleven.";
  525.    int   i, j;
  526.  
  527.    puts("Testing fscanf...\n");
  528.    if ((FP = fopen("test.dat", "r")) == NULL) {
  529.       perror("Cannot open test.dat.");
  530.       return FAILURE;
  531.       }
  532.    fscanf(FP,"%11c%d %%%d %[^z]z", s1, &i, &j, s2);
  533.    printf("%11s%d, %s%s", s1, i,
  534.           j == 11 ? "11, " : "",s2);
  535.    printf("\n");
  536.    }
  537.  
  538. /* End fscanf  */
  539. #undef main
  540. #undef   FAILURE
  541. /**** name=fseek ****/
  542. #define main() TEST_fseek()
  543.  
  544. /*
  545.    This program copies the  second  line of test.dat to fseek1.tmp, then to
  546.    fseek2.tmp.  This example illustrates the use of fseek and ftell.
  547. */
  548.  
  549. #include <stdio.h>
  550. #define  FAILURE    (-1)
  551. #define  BLOCKSIZE  (512)
  552.  
  553. int main() {
  554.    FILE *FP1, *FP2, *FP3;
  555.    char  line[BLOCKSIZE];
  556.    long  place_mark;
  557.  
  558.    FILE *topen(char *f,char *mode) {
  559.       FILE *FP;
  560.       if ((FP = fopen(f,mode)) == NULL)
  561.          perror("Cannot open any files");
  562.       return FP;
  563.       }
  564.  
  565.    if (((FP1 = topen("test.dat","r"))   == NULL) ||
  566.        ((FP2 = topen("fseek1.tmp","w")) == NULL) ||
  567.        ((FP3 = topen("fseek2.tmp","w")) == NULL)   )
  568.       return FAILURE;
  569.  
  570.    fgets(line, BLOCKSIZE, FP1);
  571.    place_mark = ftell(FP1);
  572.    if (fgets(line, BLOCKSIZE, FP1)) {
  573.       fputs(line, FP2);
  574.       fputs(line, stdout);
  575.       }
  576.    fseek(FP1, place_mark, SEEK_SET);
  577.    if (fgets(line, BLOCKSIZE, FP1)) {
  578.       fputs(line, FP3);
  579.       fputs(line, stdout);
  580.       }
  581.    }
  582. /* End fseek  */
  583. #undef main
  584. #undef   FAILURE
  585. #undef   BLOCKSIZE
  586. /**** name=fsetpos ****/
  587. #define main() TEST_fsetpos()
  588. /* End fsetpos  */
  589. #undef main
  590. /**** name=ftell ****/
  591. #define main() TEST_ftell()
  592. /* End ftell  */
  593. #undef main
  594. /**** name=_ftime ****/
  595. #define main() TEST__ftime()
  596.  
  597. #include <types.h>
  598. #include <timeb.h>
  599. #include <time.h>
  600. #undef   timezone
  601. #include <stdio.h>
  602. #include <stdlib.h>
  603.  
  604. void main() {
  605.    struct timeb x;
  606.  
  607.    puts("Testing _ftime...\n");
  608.    _ftime(&x);
  609.    printf("daylight is:%d\n"
  610.           "timezone is:%d\n"
  611.           "time is:%ld\n",
  612.           x.dstflag,x.timezone,x.time);
  613.    printf("time is:%ld\n",time(NULL));
  614.    tzset();
  615.    _ftime(&x);
  616.    printf("daylight is:%d\n"
  617.           "timezone is:%d\n"
  618.           "time is:%ld\n",
  619.           x.dstflag,x.timezone,x.time);
  620.    printf("time is:%ld\n",time(NULL));
  621.    }
  622.  
  623. /* End _ftime  */
  624. #undef main
  625. /**** name=fwrite ****/
  626. #define main() TEST_fwrite()
  627. /* End fwrite  */
  628. #undef main
  629. /**** name=_gcvt ****/
  630. #define main() TEST__gcvt()
  631.  
  632. /*
  633.    This program converts a number to a string.
  634. */
  635.  
  636. #include <stdio.h>
  637. #include <stdlib.h>
  638. #define BIG -2273734834873.2222
  639.  
  640. void main() {
  641.    char  *result;     /* _gcvt return string.                            */
  642.    char  buffer[100]; /* Buffer for result.                              */
  643.  
  644.    result = _gcvt(BIG, 10, buffer);
  645.    printf("------- _gcvt test --------\n");
  646.    printf(" value: %lf\n", BIG);
  647.    printf(" resulting string: %s\n ", result);
  648.    }
  649.  
  650. /* End _gcvt  */
  651. #undef main
  652. #undef  BIG
  653. /**** name=getc ****/
  654. #define main() TEST_getc()
  655.  
  656. /*
  657.    This program copies test.dat character by character to  getc.tmp.   This
  658.    example illustrates the use  of  getc and putc.  Similar examples appear
  659.    for fgetc and getchar, showing how the functions differ.
  660. */
  661.  
  662. #include <stdio.h>
  663.  
  664. void main() {
  665.    FILE *FP1, *FP2;
  666.  
  667.    if ((FP1 = fopen("test.dat", "r")) == NULL)
  668.       perror("Cannot open test.dat.");
  669.    if ((FP2 = fopen("getc.tmp", "w")) == NULL)
  670.       perror("Cannot open getc.tmp.");
  671.    while (feof(FP1) == 0)
  672.       fputc(fputchar(getc(FP1)), FP2);
  673.    }
  674.  
  675. /* End getc  */
  676. #undef main
  677. /**** name=_getchX ****/
  678. #define main() TEST__getchX()
  679.  
  680. #include <conio.h>
  681. #include <stdio.h>
  682.  
  683. void main() {
  684.    int c;
  685.  
  686.    puts("Every other character you key"
  687.         " will be displayed.");
  688.    do {
  689.       c = _getche();
  690.       if (c != '\r')
  691.           c = _getch();
  692.       } while (c != '\r');
  693.    }
  694.  
  695. /* End _getch*  */
  696. #undef main
  697. /**** name=getchar ****/
  698. #define main() TEST_getchar()
  699.  
  700. /*
  701.    This program copies standard input character by character to getchar.tmp
  702.    and to standard output.  Similar examples are given for fgetc  and getc,
  703.    pointing out the differences among the functions.
  704. */
  705. #include <stdio.h>
  706. #undef   getchar /* Undefine macros and access                           */
  707. #undef   putchar /*  the named functions.                                */
  708. void main() {
  709.    FILE *FP1;
  710.  
  711.    printf("Enter characters; end with a period.\n");
  712.    if ((FP1 = fopen("getchar.tmp", "w")) == NULL)
  713.       perror("Cannot open getchar.tmp.");
  714.    while ('.' != fputc(putchar(getchar()), FP1));
  715.    }
  716. /* End getchar  */
  717. #undef main
  718. /**** name=_getcwd ****/
  719. #define main() TEST__getcwd()
  720.  
  721. /*
  722.    This program gets the current working directory.
  723. */
  724.  
  725. #include <stdio.h>
  726. #include <direct.h>
  727.  
  728. void main() {
  729.    char *cwd = NULL;
  730.    int  maxlen = 66;
  731.  
  732.    if ((cwd = _getcwd(cwd, maxlen)) != NULL)
  733.       printf("Current working directory: %s\n", cwd);
  734.    else
  735.       perror("Error in _getcwd.");
  736.    }
  737.  
  738. /* End _getcwd  */
  739. #undef main
  740. /**** name=getenv ****/
  741. #define main() TEST_getenv()
  742.  
  743. /*
  744.    This program displays the value of the IPATH environment variable.
  745. */
  746. #include <stdio.h>
  747. #include <stdlib.h>
  748. void main() {
  749.    char *ev;
  750.    if ((ev = getenv("IPATH")) != NULL)
  751.       printf("The value of IPATH is:\n%s", ev);
  752.    else puts("The IPATH variable is not set.\n");
  753.    }
  754.  
  755. /* End getenv  */
  756. #undef main
  757. /**** name=_getpid ****/
  758. #define main() TEST__getpid()
  759.  
  760. /*
  761.    This program displays the caller's process ID.
  762. */
  763.  
  764. #include <process.h>
  765. #include <stdio.h>
  766.  
  767. void main() {
  768.    printf("The process ID is: %d\n", _getpid());
  769.    }
  770.  
  771. /* End _getpid  */
  772. #undef main
  773. /**** name=gets ****/
  774. #define main() TEST_gets()
  775.  
  776. /*
  777.    The program below copies a line from standard input to  standard output.
  778.    It illustrates the use of gets and puts.
  779. */
  780.  
  781. #include <stdio.h>
  782. #define  LINESIZE (256)
  783.  
  784. void main() {
  785.    char line[LINESIZE];
  786.  
  787.    printf("\nWaiting for keyboard input: ");
  788.    if (gets(line))
  789.       puts(line);
  790.    }
  791.  
  792. /* End gets  */
  793. #undef main
  794. #undef   LINESIZE
  795. /**** name=gmtime ****/
  796. #define main() TEST_gmtime()
  797. /* End gmtime  */
  798. #undef main
  799. /**** name=HUGE_VAL ****/
  800. #define main() TEST_HUGE_VAL()
  801.  
  802. #include <math.h>
  803. #include <stdio.h>
  804. #include <stdlib.h>
  805.  
  806. void main() {
  807.    double x = 1.0;
  808.    int counter;
  809.  
  810.    for (counter = 1; counter < 10000; (counter += 20)) {
  811.       printf("\n%d\t%g", counter, exp(x));
  812.       if (exp(x) >= HUGE_VAL) {
  813.          printf(".....Number too big.  Bye.\n");
  814.          break;
  815.          }
  816.       x += counter;
  817.       }
  818.    printf("\nDone.");
  819.    }
  820.  
  821. /* End HUGE_VAL  */
  822. #undef main
  823. /**** name=_hypot ****/
  824. #define main() TEST__hypot()
  825.  
  826. #include <math.h>
  827. #include <stdio.h>
  828.  
  829. void main() {
  830.    double base=3.0, height=4.0;
  831.  
  832.    puts("Testing _hypot...\n");
  833.    printf("Base => %.2f, Height => %.2f,"
  834.           " Hypotenuse => %.2f\n",
  835.           base,height,_hypot(base,height));
  836.    base = 1.0, height = 1.0;
  837.    printf("Base => %.2f, Height => %.2f,"
  838.           " Hypotenuse => %.2f\n",
  839.           base,height,_hypot(base,height));
  840.    base = 8.0, height = -6.0;
  841.    printf("Base => %.2f, Height => %.2f,"
  842.           " Hypotenuse => %.2f\n",
  843.           base, height, _hypot(base, height));
  844.    }
  845.  
  846. /* End _hypot  */
  847. #undef main
  848.  
  849. /*****names*****/
  850.  
  851. char * names[]={
  852.    "fabs",
  853.    "fclose",
  854.    "_fcvt",
  855.    "ferror",
  856.    "fgetc",
  857.    "fgetpos",
  858.    "fgets",
  859.    "_filelength",
  860.    "_fileno",
  861.    "_fmalloc",
  862.    "_FP_X",
  863.    "fprintf",
  864.    "fread",
  865.    "freopen",
  866.    "frexp",
  867.    "fscanf",
  868.    "fseek",
  869.    "_ftime",
  870.    "_gcvt",
  871.    "getc",
  872.    "_getchX",
  873.    "getchar",
  874.    "_getcwd",
  875.    "getenv",
  876.    "_getpid",
  877.    "gets",
  878.    "HUGE_VAL",
  879.    "_hypot",
  880.    "",""};
  881.    int nextfunum;
  882. void main() {
  883.    char ans[90];
  884.    for (;;) {
  885.       for (int j=0;j< 28;j++)
  886.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  887.       else printf("%4d %-21s",j+1,names[j]);
  888.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  889.       gets(ans);
  890.       if (ans[0] != 0) nextfunum=atoi(ans);
  891.       printf("\n\n\n");
  892.       switch(nextfunum) {
  893.          case 0:exit(0);
  894.          case 1:TEST_fabs();break;
  895.          case 2:TEST_fclose();break;
  896.          case 3:TEST__fcvt();break;
  897.          case 4:TEST_ferror();break;
  898.          case 5:TEST_fgetc();break;
  899.          case 6:TEST_fgetpos();break;
  900.          case 7:TEST_fgets();break;
  901.          case 8:TEST__filelength();break;
  902.          case 9:TEST__fileno();break;
  903.          case 10:TEST__fmalloc();break;
  904.          case 11:TEST__FP_X();break;
  905.          case 12:TEST_fprintf();break;
  906.          case 13:TEST_fread();break;
  907.          case 14:TEST_freopen();break;
  908.          case 15:TEST_frexp();break;
  909.          case 16:TEST_fscanf();break;
  910.          case 17:TEST_fseek();break;
  911.          case 18:TEST__ftime();break;
  912.          case 19:TEST__gcvt();break;
  913.          case 20:TEST_getc();break;
  914.          case 21:TEST__getchX();break;
  915.          case 22:TEST_getchar();break;
  916.          case 23:TEST__getcwd();break;
  917.          case 24:TEST_getenv();break;
  918.          case 25:TEST__getpid();break;
  919.          case 26:TEST_gets();break;
  920.          case 27:TEST_HUGE_VAL();break;
  921.          case 28:TEST__hypot();break;
  922.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  923.          }
  924.       printf("\n\npress enter to select another function\n");
  925.       gets(ans);
  926.       }
  927.    }
  928.