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

  1. /*         Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved  */
  2.  
  3. /**** name=abort ****/
  4. #define main TEST_abort
  5.  
  6. /*
  7.    The program fragment below aborts the  program  with a message if all is
  8.    not okay.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. void main() {
  14.    int i = 0;
  15.  
  16.    if (i != 1) {
  17.       fprintf(stderr, "Terminate right now!");
  18.       abort();
  19.       }
  20.    printf("\n This line will never print.\n");
  21.    }
  22. /* End abort  */
  23. #undef main
  24. /**** name=abs ****/
  25. #define main TEST_abs
  26.  
  27. #include <stdio.h>
  28. #include <math.h>
  29.  
  30. void main() {
  31.    int absolut, posneg = -32;
  32.  
  33.    absolut = abs(posneg);
  34.    printf("The absolute value of %d is: %d\n",
  35.          posneg, absolut);
  36.    }
  37. /* End abs  */
  38. #undef main
  39. /**** name=_access ****/
  40. #define main TEST__access
  41.  
  42. /*
  43.    This program reports the access rights of file test.dat.
  44. */
  45.  
  46. #include <io.h>
  47. #include <fcntl.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50.  
  51. void main() {
  52.    char *name = "test.dat";
  53.    int exists   = 0;
  54.    int readonly = 0;
  55.  
  56.    exists   = _access(name, 0) == 0;
  57.    readonly = _access(name, 2) == -1;
  58.  
  59.    if (exists && readonly)
  60.       puts("test.dat has read-only access.");
  61.    else if (exists && !readonly)
  62.       puts("test.dat has read/write access.");
  63.    else
  64.       puts("File test.dat not found.");
  65.    }
  66.  
  67. /* End _access  */
  68. #undef main
  69. /**** name=_alloca ****/
  70. #define main TEST__alloca
  71.  
  72. /*
  73.    alloca must never be used as an argument to a function. It is inherently
  74.    unreliable in concept. The function is provided only for compatibility.
  75. */
  76.  
  77. #include <malloc.h>
  78. #include <stdio.h>
  79.  
  80. pragma off(Postpone_arg_pops);                              /* Required. */
  81. pragma off(Optimize_FP);                                    /* Required. */
  82. void main() {
  83.    int m, j[15], *k;
  84.  
  85.    k = alloca(sizeof(j) * sizeof(j[0]));
  86.    printf("Allocated space on stack.\n");
  87.    for (m = 0; m <= 15; m++) {
  88.       if ((m==0) || (m==1))
  89.          k[m]=1;
  90.       else
  91.          k[m] = k[m-1] + k[m-2];                   /* Fibonacci sequence */
  92.       }
  93.    printf("Space is allocated and "
  94.           "contained the Fibonacci sequence.\n");
  95.    for (m = 0; m <= 13; m++) {
  96.       if (k[m] == 0)
  97.          printf("Error, element k[%d] is zero.\n", m);
  98.       else
  99.          printf("%d, ",k[m]);
  100.       }
  101.    printf("\nDone.\n");
  102.    }
  103. /* End _alloca  */
  104. #undef main
  105. /**** name=asctime ****/
  106. #define main TEST_asctime
  107.  
  108. /*
  109.    This  example  illustrates   the   use  of  asctime()  with  time()  and
  110.    localtime().
  111. */
  112. #include <time.h>
  113. #include <stdio.h>
  114.  
  115. void main() {
  116.    time_t t;
  117.  
  118.    time(&t);
  119.    printf("Local time: %s", asctime(localtime(&t)));
  120.    }
  121. /* End asctime  */
  122. #undef main
  123. /**** name=asin ****/
  124. #define main TEST_asin
  125.  
  126. /*
  127.    This  function  shows  the  behavior  of  asin  and sin.  Note that  the
  128.    argument to asin is a constant and its return is a radian value.
  129. */
  130.  
  131. #include <errno.h>
  132. #include <math.h>
  133. #include <stdio.h>
  134. #include <stdlib.h>
  135.  
  136. void main() {
  137.    double x, value = 0;
  138.  
  139.    for (;;) {  /* Go until error is created. */
  140.       x = asin(value);
  141.       printf("\nasin(%lf)", value);
  142.       if (errno == EDOM) {
  143.          printf("\nDomain error.  value = %lf", value);
  144.          return;
  145.          }
  146.       else
  147.          printf("\nasin returns angle:"
  148.                 " %lf radian\t %lf degrees",
  149.                  x, 180.0/_PI * x );
  150.       value = value + 0.6;
  151.       }
  152.    }
  153. /* End asin  */
  154. #undef main
  155. /**** name=assert ****/
  156. #define main TEST_assert
  157.  
  158. #include <assert.h>
  159. #include <stdio.h>
  160. #include <stdlib.h>                          /* Get abort() declaration. */
  161.  
  162. void main() {
  163.    int i = 0;
  164.    
  165.    assert(!i);
  166.    printf("\nProgram is running, but"
  167.           " will fail soon.\n");
  168.    assert(i == 1);
  169.    printf("\nProgram still running.\n");
  170.    }
  171. /* End assert  */
  172. #undef main
  173. /**** name=atan ****/
  174. #define main TEST_atan
  175.  
  176. #include <errno.h>
  177. #include <math.h>
  178. #include <stdio.h>
  179. #include <stdlib.h>
  180.  
  181. void main() {
  182.    double x = 0.0;
  183.    
  184.    for(;;) {
  185.       printf("\natan(%f) = %f radians\t%f degrees",
  186.              x, atan(x), 180.0/_PI * atan(x));
  187.       x = exp(x);
  188.       if (errno) {  /* Check for error in exp(). */
  189.          printf("\n An error has occurred.  Bye ...");
  190.          return;
  191.          }
  192.       }
  193.    }
  194. /* End atan  */
  195. #undef main
  196. /**** name=atan2 ****/
  197. #define main TEST_atan2
  198.  
  199. #include <stdio.h>
  200. #include <math.h>
  201. void main() {
  202.    double x = 1.0, y = 1.55741;
  203.    printf("%e  %e\n", atan2(y,x), atan2(-y,x));
  204.    printf("%e  %e\n", atan2(y,-x), atan2(-y,-x));
  205.    }
  206. /* End atan2  */
  207. #undef main
  208. /**** name=atexit ****/
  209. #define main TEST_atexit
  210.  
  211. #include <stdio.h>
  212. #include <stdlib.h>
  213. void func(void);
  214.  
  215. void main() {
  216.  
  217.    atexit(func);
  218.    printf("Watch for the atexit message"
  219.           " when the program ends.\n");
  220.    }
  221.  
  222. void func(void) {
  223.    printf("\nThe atexit test is now finished.\n");
  224.    }
  225. /* End atexit  */
  226. #undef main
  227. /**** name=atof ****/
  228. #define main TEST_atof
  229.  
  230. /*
  231.    The program below displays a number as a string and as a float.
  232. */
  233. #include <stdlib.h>
  234. #include <stdio.h>
  235. void main() {
  236.    char pi[] = "3.141593";
  237.    printf("String pi is %s.  ", pi);
  238.    printf("Float pi is %.6f.\n", atof(pi));
  239.    }
  240. /* End atof  */
  241. #undef main
  242. /**** name=atoi ****/
  243. #define main TEST_atoi
  244.  
  245. /*
  246.    The program below displays a number as a string and as an int.
  247. */
  248. #include <stdlib.h>
  249. #include <stdio.h>
  250. void main() {
  251.    char num[] = "412";
  252.    printf("String number is %s.\n", num);
  253.    printf("Int number is %d.\n", atoi(num));
  254.    }
  255. /* End atoi  */
  256. #undef main
  257. /**** name=atol ****/
  258. #define main TEST_atol
  259.  
  260. /*
  261.    The program below displays a number as a string and as an int.
  262. */
  263. #include <stdlib.h>
  264. #include <stdio.h>
  265. void main() {
  266.    char num[] = "1267543";
  267.    printf("String number is %s.\n", num);
  268.    printf("Long int number is %d.\n", atol(num));
  269.    }
  270. /* End atol  */
  271. #undef main
  272. /**** name=_bdos ****/
  273. #define main TEST__bdos
  274.  
  275. #include <dos.h>
  276.  
  277. void main() {
  278.    char msg[] = "Display this on the screen.\r\n";
  279.    int k = 0;
  280.  
  281.    while (msg[k]!=0)
  282.       _bdos(2,msg[k++],0);
  283.    }
  284. /* End _bdos  */
  285. #undef main
  286. /**** name=_bios_disk ****/
  287. #define main TEST__bios_disk
  288.  
  289.    /*
  290.       This program tests status of Drive C.
  291.       Assumes High C users will always have a hard disk drive.
  292.    */
  293.  
  294.    #include <stdio.h>
  295.    #include <bios.h>
  296.  
  297.    void main() {
  298.       unsigned status = 0;
  299.       struct diskinfo_t info;
  300.  
  301.       info.drive    = 0x80;  /* Drive C:                                 */
  302.       info.head     = 0;     /* Use the boot sector.                     */
  303.       info.track    = 0;
  304.       info.sector   = 1;
  305.       info.nsectors = 1;
  306.       info.buffer   = 0;
  307.  
  308.       status = _bios_disk(_DISK_STATUS, &info);
  309.       printf("Status of disk drive C: is %x",status);
  310.       }
  311. /* End _bios_disk  */
  312. #undef main
  313. /**** name=_bios_equiplist ****/
  314. #define main TEST__bios_equiplist
  315.  
  316. /*
  317.    Program detects various hardware attachments.
  318. */
  319. #include <stdio.h>
  320. #include <bios.h>
  321.  
  322. void main() {
  323.    unsigned equip;                         /* Holder for return value.   */
  324.    unsigned bits;                          /* Holder for isolated bits.  */
  325.  
  326.    equip = _bios_equiplist();                     /* Call the function.  */
  327.    printf("equip => %x\n\n", equip);
  328.    bits  = equip & 0x1;                           /* Mask bit 0 first.   */
  329.    if (bits == 1) {
  330.        bits = equip & 0xC0;                       /* Bits 6 and 7 for    */
  331.                                                   /*  number of drives.  */
  332.        bits=bits >> 6;                            /* Shift right to get  */
  333.                                                   /*  number.            */
  334.  
  335.        printf("%i floppy disk(s) installed"
  336.               "\n", bits + 1);
  337.        }
  338.    else printf("No floppy disks installed\n");
  339.    bits = equip & 0x2;                                   /* Mask bit 1.  */
  340.    if(bits == 2)
  341.       printf("Math coprocessor installed.\n");
  342.    else printf("No math coprocessor installed.\n");
  343.    bits = equip & 0x100;                                 /* Mask bit 8.  */
  344.    if(bits == 0)
  345.       printf("DMA chip is installed.\n");
  346.    else printf("No DMA chip is installed.\n");
  347.    bits = equip & 0x1000;                                /* Mask bit 12. */
  348.    if(bits == 2048)
  349.       printf("Game adapter is installed.\n");
  350.    else printf("Game adapter is not installed.\n");
  351.    bits = equip & 0x2000;                                /* Mask bit 13. */
  352.    if(bits == 4096)
  353.       printf("Serial printer attached.\n");
  354.    else printf("Serial printer is not attached.\n");
  355.    }
  356.  
  357. /* End _bios_equiplist  */
  358. #undef main
  359. /**** name=_bios_keybrd ****/
  360. #define main TEST__bios_keybrd
  361.  
  362. /*
  363.    This program tests keyboard read and status functions.
  364.  
  365. */
  366. #include <stdio.h>
  367. #include <bios.h>
  368. void main() {
  369.    unsigned key = 0;  /* Return value.       */
  370.    char bits[10];     /* Status bit pattern. */
  371.    int pow2[8]  = {128,64,32,16,8,4,2,1};
  372.    int i;
  373.  
  374.    while ((char) key != 27) {
  375.       printf("\nPress any key to test, or "
  376.              "ESC to quit\n");
  377.  
  378.       /* Loop until something happens. */
  379.       while ((_bios_keybrd(_KEYBRD_READY))==0);
  380.       /* Check for ESC. */
  381.       key = _bios_keybrd(_KEYBRD_READ);
  382.       if ((char)key != 27) {
  383.          printf("You entered  %c  \n",key);
  384.          if ((key =
  385.              _bios_keybrd(_KEYBRD_SHIFTSTATUS)) != 0) {
  386.             for ( i = 0; i <= 7; i++) {
  387.                if (((char)key & pow2[i]) != 0)
  388.                   bits[i] = '1';
  389.  
  390.                else
  391.                   bits[i] = '0';
  392.                }
  393.             bits[8] = 0;
  394.             printf("With a status bit pattern of %s"
  395.                    "\n",bits);
  396.             }
  397.          }
  398.       }
  399.    }
  400. /* End _bios_keybrd  */
  401. #undef main
  402. /**** name=_bios_memsize ****/
  403. #define main TEST__bios_memsize
  404.  
  405. /*
  406.    This program prints the amount of system memory.
  407. */
  408.  
  409. #include <stdio.h>
  410. #include <bios.h>
  411.  
  412. void main() {
  413.    unsigned memsize;
  414.  
  415.    memsize = _bios_memsize();
  416.    printf("The amount of system memory "
  417.           "is %uKB \n", memsize);
  418.    }
  419. /* End _bios_memsize  */
  420. #undef main
  421. /**** name=_bios_printer ****/
  422. #define main TEST__bios_printer
  423.  
  424. /*
  425.    This program tests the printer output and status functions.
  426. */
  427.  
  428. #include <stdio.h>
  429. #include <bios.h>
  430. #define LPT1 0
  431. void main() {
  432.    unsigned status;   /* Receives return value. */
  433.    unsigned data;     /* Output data.           */
  434.    char bits[10];     /* Status bit pattern.    */
  435.  
  436.    static int pow2[8] = {128,64,32,16,8,4,2,1};
  437.    int i;
  438.  
  439.    data = 0xff;
  440.    status = _bios_printer(_PRINTER_STATUS, LPT1, data);
  441.  
  442.    for (i = 0; i <= 7; i++) {
  443.       if (((char)status & pow2[i]) != 0)
  444.           bits[i] = '1';
  445.       else
  446.           bits[i] = '0';
  447.       }
  448.    bits[8] = 0;
  449.    printf("Status bit pattern of LPT1:"
  450.           " %s \n",bits);
  451.    }
  452. /* End _bios_printer  */
  453. #undef main
  454. #undef  LPT1
  455. /**** name=_bios_serialcom ****/
  456. #define main TEST__bios_serialcom
  457.  
  458. /*
  459.    This program tests serial-port functions.
  460. */
  461.  
  462. #include <stdio.h>
  463. #include <bios.h>
  464. #define _COM1   0
  465.  
  466. void main() {
  467.    int i;
  468.    unsigned data;     /* Output byte.        */
  469.  
  470.    unsigned status;   /* Return value.       */
  471.    char bits[18];     /* Status bit pattern. */
  472.    static int pow2[16] =
  473.    {1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
  474.     1024, 2048, 4096, 8192, 16384,32768};
  475.  
  476.    data   = _COM_CHR8 | _COM_STOP1 | _COM_NOPARITY |
  477.             _COM_2400;
  478.    status = _bios_serialcom(_COM_STATUS,_COM1,data);
  479.  
  480.    for (i = 0; i <= 15; i++) {
  481.       if ((status & pow2[15 - i]) != 0)
  482.            bits[i] = '1';
  483.       else
  484.            bits[i] = '0';
  485.       }
  486.    bits[16] = 0;
  487.    printf("Status bit pattern of COM1:%s \n", bits);
  488.    }
  489. /* End _bios_serialcom  */
  490. #undef main
  491. #undef  _COM1
  492. /**** name=_bios_timeofday ****/
  493. #define main TEST__bios_timeofday
  494.  
  495. /*
  496.    This program counts the number of clock ticks for 1,000 iterations of a
  497.    function.
  498. */
  499. #include <stdio.h>
  500. #include <bios.h>
  501. #include <math.h>
  502. void main() {
  503.    long i, start, stop;
  504.    double x  = 0.75, t1 = 0.50025;
  505.  
  506.    puts("Testing _bios_timeofday...\n");
  507.    _bios_timeofday (_TIME_GETCLOCK, &start);
  508.    printf("Start tick count: %lu\n", start);
  509.    printf("Doing 1000 iterations "
  510.           "of: sqrt(exp(log(x)/t1))...\n");
  511.    for (i=0; i<=1000; i++)
  512.       x = sqrt(exp(log(x)/t1));
  513.    _bios_timeofday (_TIME_GETCLOCK, &stop);
  514.    printf("Stop  tick count: %lu\n", stop);
  515.    printf("-----------------\n");
  516.    printf("Total ticks     : %lu\n\n", stop - start);
  517.    stop = 0;
  518.    _bios_timeofday (_TIME_GETCLOCK, &start);
  519.    printf("Start tick count: %lu\n", start);
  520.    printf("Doing 1000 iterations "
  521.           "of: sqrt(exp(log(x)/t1))...\n");
  522.    for (i=0; i<=1000; i++)
  523.       x = sqrt(exp(log(x)/t1));
  524.    _bios_timeofday (_TIME_GETCLOCK, &stop);
  525.    printf("Stop  tick count: %lu\n", stop);
  526.    printf("-----------------\n");
  527.    printf("Total ticks     : %lu\n", stop - start);
  528.    }
  529. /* End _bios_timeofday  */
  530. #undef main
  531. /**** name=bsearch ****/
  532. #define main TEST_bsearch
  533.  
  534. #include <stdio.h>
  535. #include <stdlib.h>
  536. #include <string.h>
  537.  
  538. struct info {
  539.    char name[8];
  540.    int  age;
  541.    char phone[9];
  542.    };
  543.  
  544. /* Array of info sorted by age, and then by name. */
  545. struct info data[] = {
  546.    { "Alice", 19, "555-7979" },
  547.    { "Fred",  27, "555-1221" },
  548.    { "Frank", 30, "555-1965" },
  549.    { "Carol", 32, "555-4299" },
  550.    { "Anne",  33, "555-5552" },
  551.    { "Larry", 33, "555-8235" },
  552.    { "Sonya", 36, "555-1976" },
  553.    };
  554. static int compare_function(const struct info *item1,
  555.                             const struct info *item2) {
  556.    if (item1->age < item2->age)      return (-1);
  557.    else if (item1->age > item2->age) return (1);
  558.    else return (strcmp(item1->name, item2->name));
  559.    }
  560.  
  561. struct info key = { "Carol", 32, "" };
  562.  
  563. void main() {
  564.    struct info *p;
  565.    p = bsearch(&key,data,sizeof(data)/sizeof(*data),
  566.                sizeof(*data), compare_function);
  567.    if (p) printf("Name=%s, Age=%d, Phone=%s\n",
  568.                   p->name, p->age, p->phone);
  569.    else   printf("%s NOT FOUND\n", key.name);
  570.    }
  571.  
  572. /* End bsearch  */
  573. #undef main
  574. /**** name=_cabs ****/
  575. #define main TEST__cabs
  576.  
  577. #include <math.h>
  578.  
  579. void main() {
  580.    struct complex zz;
  581.  
  582.    zz.x = 5.0;
  583.    zz.y = 7.0;
  584.  
  585.    printf("Complex absolute value of (5.0, 7.0i)"
  586.           " is %f\n", _cabs(zz));
  587.    }
  588.  
  589. /* End _cabs  */
  590. #undef main
  591. /**** name=calloc ****/
  592. #define main TEST_calloc
  593.  
  594. /*
  595.    The following example demonstrates calloc() in two different  ways.   In
  596.    function f1(), calloc() initializes integers  to zero.  Next, f2() shows
  597.    how  calloc()  initializes  chars  to  NULL,  and  also illustrates  the
  598.    difference between calloc() and malloc().
  599. */
  600. #include <stdlib.h>
  601. #include <stdio.h>
  602. extern void f1();
  603. extern void f2();
  604.  
  605. void main() {
  606.    printf("\nEXAMPLE 1:\n");
  607.    f1();
  608.    printf("\nEXAMPLE 2:\n");
  609.    f2();
  610.    }
  611.  
  612. void f1() {
  613.    int i;
  614.    typedef int grade;
  615.    grade *grade_ptr;
  616.  
  617.    /* Allocate space for grades, setting them to 0. */
  618.    grade_ptr = (int *) calloc (4, sizeof(grade));
  619.    for (i = 0; i <= 3; i++)
  620.       printf("Grade %d is initially %d.\n",
  621.               i, grade_ptr[i]);
  622.    }
  623.  
  624. void f2() {
  625.    typedef struct {
  626.       char *first;
  627.       char *last;
  628.       } names ;
  629.    names *name_ptr=(names *)calloc(1,sizeof(names));
  630.    if (name_ptr == NULL)
  631.       printf("Out of Memory!\n");
  632.    else
  633.    /* calloc initializes char to NULL */
  634.       if (name_ptr->first == NULL)
  635.          printf("calloc was used"
  636.                 " to allocate space.\n");
  637.       else
  638.    /* malloc does not; space contains garbage. */
  639.  
  640.          printf("malloc was used"
  641.                 " to allocate space.\n");
  642.    }
  643.  
  644. /* End calloc  */
  645. #undef main
  646. /**** name=ceil ****/
  647. #define main TEST_ceil
  648.  
  649. #include <math.h>
  650. #include <stdio.h>
  651.  
  652. void main() {
  653.    double d = 2.789;
  654.    printf("The number is %.2e\n", d);
  655.    printf("ceil returned %.2e\n", ceil(d));
  656.    }
  657.  
  658. /* End ceil  */
  659. #undef main
  660. /**** name=_cgets ****/
  661. #define main TEST__cgets
  662.  
  663. #include <stdio.h>
  664.  
  665. void main() {
  666.    extern char *_cgets(char *bufr);
  667.    char p[85];
  668.    char *q;
  669.  
  670.    p[0] = 80;
  671.    puts("Testing _cgets...\n");
  672.    puts("Enter a line of text.");
  673.    q = (_cgets(p));
  674.    puts("The characters entered were:");
  675.    puts(q);
  676.    printf("\nThe entire buffer length in bytes is"
  677.           " %d.\n",p[1]);
  678.    }
  679.  
  680. /* End _cgets  */
  681. #undef main
  682. /**** name=_chdir ****/
  683. #define main TEST__chdir
  684.  
  685. /*
  686.    This program changes the current directory.
  687. */
  688. #include <stdio.h>
  689. #include <direct.h>
  690. void main() {
  691.  
  692. if (_chdir("c:\\") == -1) /* Go to root directory. */
  693.       perror("error in _chdir");
  694.    else
  695.       printf("Directory successfully"
  696.              " changed to c:\\.\n");
  697.    }
  698. /* End _chdir  */
  699. #undef main
  700. /**** name=_chmod ****/
  701. #define main TEST__chmod
  702.  
  703. /*
  704.    This program alters the permission setting of a user-specified file.
  705. */
  706. #include <sys\types.h>
  707. #include <sys\stat.h>
  708. #include <io.h>
  709. #include <stdio.h>
  710. #include <stdlib.h>
  711. void main() {
  712.    char filename[50];
  713.    int newmode;
  714.  
  715.    printf("Change permission setting of file: ");
  716.    scanf("%s", filename);
  717.    printf("\n1. read-only\n2. read/write\n");
  718.    printf("Enter number of mode to change to:\n");
  719.    scanf("%d", &newmode);
  720.    switch (newmode) {
  721.       case 1:
  722.          if (_chmod(filename, S_IREAD) == -1)
  723.             perror("File not found.");
  724.          else
  725.             printf("%s changed to read only \n",
  726.                     filename);
  727.          break;
  728.       case 2:
  729.          if (_chmod(filename, S_IREAD | S_IWRITE) == -1)
  730.             perror("File not found");
  731.          else
  732.             printf("%s changed to read/write \n",
  733.                     filename);
  734.  
  735.       }
  736.    }
  737. /* End _chmod  */
  738. #undef main
  739. /**** name=_chsize ****/
  740. #define main TEST__chsize
  741.  
  742. /*
  743.    The program opens/creates the file chsize.tmp.  If the file has a write
  744.    permission then it writes 512 bytes to the file and uses the function
  745.    _chsize to change the size to 256 bytes.  If the file chsize.tmp does
  746.    not have write permission an appropriate message is printed.
  747. */
  748.  
  749. #include <stdio.h>
  750. #include <string.h>
  751. #include <io.h>
  752. #include <fcntl.h>
  753. #include <types.h>
  754. #include <stat.h>
  755. #include <errno.h>
  756. #define NEWSIZE 256L
  757.  
  758. void main() {
  759.    int   handle;
  760.    char  buffer[BUFSIZE];
  761.    char *filename = "chsize.tmp";
  762.  
  763.    puts("Testing _chsize...\n");
  764.    /* Open new file. */
  765.    if ((handle = _open(filename, O_RDWR | O_CREAT, 
  766.                        S_IWRITE | S_IREAD)) > 0) {
  767.       /* Check for write permission. */
  768.       if (! access(filename,3)) {
  769.          /* Write to the buffer. */
  770.          memset(buffer, 'X', BUFSIZE);
  771.          /* Write buffer to file. */
  772.          _write(handle, buffer, BUFSIZE);
  773.          printf("File %s is %Ld bytes long.\n",
  774.          filename, filelength(handle));
  775.  
  776.          if (! _chsize(handle, NEWSIZE))
  777.             printf("File %s is now %Ld bytes long.\n",
  778.                    filename, filelength(handle));
  779.          else 
  780.             printf("ERROR _chsize was unsuccessful.\n");
  781.          }
  782.       else 
  783.          printf("ERROR: %s not WRITEABLE."
  784.                 "\n", filename);
  785.       }
  786.    else printf("Error occured in opening file. %s:"
  787.                " ERROR NUMBER %d.\n", filename, errno);
  788.    }
  789. /* End _chsize  */
  790. #undef main
  791. #undef  NEWSIZE
  792. /**** name=clock ****/
  793. #define main TEST_clock
  794. /* End clock  */
  795. #undef main
  796. /**** name=CLOCKS_PER_SEC ****/
  797. #define main TEST_CLOCKS_PER_SEC
  798.  
  799. /*
  800.    This example sets up a real-world timer that uses the first command-line
  801.    argument as a value for time.
  802. */
  803. #include <stdio.h>
  804. #include <stdlib.h>
  805. #include <time.h>
  806.  
  807. void main() {
  808.    clock_t start = clock()/CLOCKS_PER_SEC;
  809.    clock_t duration = 5;
  810.  
  811.    printf("Waiting for %ld seconds to pass.\n",
  812.           (long) duration);
  813.    while (((clock()/CLOCKS_PER_SEC) - start) <=
  814.           duration)
  815.       ;
  816.    puts("Time elapsed.");
  817.    }
  818.  
  819. /* End CLOCKS_PER_SEC  */
  820. #undef main
  821. /**** name=clock_t ****/
  822. #define main TEST_clock_t
  823. /* End clock_t  */
  824. #undef main
  825. /**** name=_close ****/
  826. #define main TEST__close
  827.  
  828. /*
  829.    This program opens  (creates  new)  and  closes  a file.  It changes the
  830.    permission of the file.
  831. */
  832.  
  833. #include <sys\types.h>
  834. #include <sys\stat.h>
  835. #include <stdio.h>
  836. #include <fcntl.h>
  837. #include <io.h>
  838.  
  839. void main() {
  840.    int fh, bad;
  841.  
  842.    puts("Testing _close...\n");
  843.    fh = _open("close.tmp", O_CREAT | O_EXCL,
  844.                _S_IWRITE | _S_IREAD);
  845.    bad = _close(fh);
  846.    if (bad == 0)
  847.       puts("Created close.tmp, a zero-length file.");
  848.    else 
  849.       puts("_close failed.");
  850.    }
  851. /* End _close  */
  852. #undef main
  853. /**** name=_compare ****/
  854. #define main TEST__compare
  855.  
  856. /*
  857.    This program tests the function _compare.
  858. */
  859.  
  860. #include <stdio.h>
  861. #include <string.h>
  862.  
  863. void main() {
  864.    char String1[]="this is a test";
  865.    char String2[]="that is a test";
  866.    char String3[]="testing 1 2 3 ";
  867.    char String4[]="";
  868.    int i;
  869.  
  870.    puts("Testing _compare...\n");
  871.    printf("String 1:%s,\tString 2:"
  872.           "%s.\n",String1, String1);
  873.    i = _compare(String1, String1, strlen(String1));
  874.  
  875.    if (i == 0)
  876.       printf("Strings are equal. \n");
  877.    else
  878.       printf("Strings are not equal.\n");
  879.    printf("Return is %d.\n\n",i);
  880.  
  881.    printf("String 1:%s,\tString 2:"
  882.            "%s.\n",String2, String3);
  883.    i = _compare(String2, String3, strlen(String3));
  884.    if (i == 0)
  885.       printf("Strings are equal. \n");
  886.    else
  887.       printf("Strings are not equal.\n");
  888.    printf("Return is %d.\n\n",i);
  889.  
  890.    printf("String 1:%s,\tString 2:"
  891.           "%s.\n",String4, String2);
  892.    i = _compare(String4, String2, strlen(String2));
  893.    if (i == 0)
  894.       printf("Strings are equal. \n");
  895.    else
  896.       printf("Strings are not equal.\n");
  897.  
  898.    printf("Return is %d.\n",i);
  899.    }
  900.  
  901. /* End _compare  */
  902. #undef main
  903. /**** name=cos ****/
  904. #define main TEST_cos
  905.  
  906. /*
  907.    This  function shows the behavior of cos.  Note that the argument to cos
  908.    is in radians.
  909. */
  910. #include <math.h>
  911. #include <stdio.h>
  912.  
  913. void main() {
  914.    printf("Cosine of 45 degrees (PI/4) = %f\n",
  915.           cos(_PI/4));
  916.    printf("Cosine of 90 degrees (PI/2) = %f\n",
  917.           cos(_PI/2));
  918.    }
  919. /* End cos  */
  920. #undef main
  921. /**** name=cosh ****/
  922. #define main TEST_cosh
  923.  
  924. /*
  925.    This program tests the cosh function.
  926. */
  927. #include <math.h>
  928. #include <stdio.h>
  929.  
  930. void main() {
  931.    printf("cosh(0.0) = %f.\n", cosh(0.0));
  932.    printf("cosh(3.5) = %f.\n", cosh(3.5));
  933.    /* Next, display the symmetry about y-axis. */
  934.    printf("cosh(-3.5) = %f.\n", cosh(-3.5));
  935.    }
  936. /* End cosh  */
  937. #undef main
  938. /**** name=_cprintf ****/
  939. #define main TEST__cprintf
  940.  
  941. /*
  942.    The example for fprintf shows the similarity of the two functions.
  943. */
  944.  
  945. #include <conio.h>
  946.  
  947. void main() {
  948.    char s[14] = "like this one", c = '*';
  949.    int i = 10, x = 255, o = 45;
  950.    double d = 3.1415927, nf = -3.449123;
  951.    _cprintf("_cprintf prints strings (%s),\r\n", s);
  952.    _cprintf("decimal numbers (%d),", i);
  953.    _cprintf(" hex numbers (%04X),\r\n", x);
  954.    _cprintf("floating-point numbers (%+.4e)\r\n", d);
  955.    _cprintf("percent signs (%%),\r\n");
  956.    _cprintf("characters (%-5c),\r\n", c);
  957.    _cprintf("negative floating-points "
  958.            "(% 010.2e),\r\n", nf);
  959.    _cprintf("and even octal numbers (%-+#5o).", o);
  960.    }
  961.  
  962. /* End _cprintf  */
  963. #undef main
  964. /**** name=_cputs ****/
  965. #define main TEST__cputs
  966.  
  967. #include <conio.h>
  968.  
  969. void main() {
  970.    _cputs("This is a te");
  971.    _cputs("st of the _cputs\nfunction.\r");
  972.    }
  973.  
  974. /* End _cputs  */
  975. #undef main
  976. /**** name=_creat ****/
  977. #define main TEST__creat
  978.  
  979. #include <io.h>
  980. #include <fcntl.h>
  981. #include <sys\stat.h>
  982. #include <stdio.h>
  983.  
  984. void main() {
  985.    int pan;
  986.  
  987.    pan=_creat("creat.tmp", S_IREAD | S_IWRITE);
  988.    if (pan == -1)
  989.       printf("Cannot creat/truncate file creat.tmp,"
  990.              " error code=%d\n",pan);
  991.    else
  992.       printf("file creat.tmp has been"
  993.              " creat/truncated,"
  994.              " handle=%d\n",pan);
  995.    }
  996.  
  997. /* End _creat  */
  998. #undef main
  999. /**** name=_crotX ****/
  1000. #define main TEST__crotX
  1001.  
  1002. #include <stdlib.h>
  1003. #include <stdio.h>
  1004.  
  1005. void main() {
  1006.    char k=1;
  1007.    char j=0x80;
  1008.  
  1009.    printf("Rotate left and right by"
  1010.           " one bit at a time\n");
  1011.    do {
  1012.       printf("%4x %4x\n", k, j);
  1013.       k = _crotl(k,1);
  1014.       j = _crotr(j,1);
  1015.       }
  1016.    while (k != 1);
  1017.    }
  1018. /* End _crot*  */
  1019. #undef main
  1020. /**** name=ctime ****/
  1021. #define main TEST_ctime
  1022.  
  1023. /*
  1024.    This example illustrates the use of ctime() with time().
  1025. */
  1026. #include <time.h>
  1027. #include <stdio.h>
  1028.  
  1029. void main() {
  1030.    time_t t;
  1031.  
  1032.    time(&t);
  1033.    printf("Time: %s", ctime(&t));
  1034.    }
  1035. /* End ctime  */
  1036. #undef main
  1037.  
  1038. /*****names*****/
  1039.  
  1040. char * names[]={
  1041.    "abort",
  1042.    "abs",
  1043.    "_access",
  1044.    "_alloca",
  1045.    "asctime",
  1046.    "asin",
  1047.    "assert",
  1048.    "atan",
  1049.    "atan2",
  1050.    "atexit",
  1051.    "atof",
  1052.    "atoi",
  1053.    "atol",
  1054.    "_bdos",
  1055.    "_bios_disk",
  1056.    "_bios_equiplist",
  1057.    "_bios_keybrd",
  1058.    "_bios_memsize",
  1059.    "_bios_printer",
  1060.    "_bios_serialcom",
  1061.    "_bios_timeofday",
  1062.    "bsearch",
  1063.    "_cabs",
  1064.    "calloc",
  1065.    "ceil",
  1066.    "_cgets",
  1067.    "_chdir",
  1068.    "_chmod",
  1069.    "_chsize",
  1070.    "CLOCKS_PER_SEC",
  1071.    "_close",
  1072.    "_compare",
  1073.    "cos",
  1074.    "cosh",
  1075.    "_cprintf",
  1076.    "_cputs",
  1077.    "_creat",
  1078.    "_crotX",
  1079.    "ctime",
  1080.    "",""};
  1081.    int nextfunum;
  1082. void main() {
  1083.    char ans[90];
  1084.    for (;;) {
  1085.       for (int j=0;j< 39;j++)
  1086.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  1087.       else printf("%4d %-21s",j+1,names[j]);
  1088.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  1089.       gets(ans);
  1090.       if (ans[0] != 0) nextfunum=atoi(ans);
  1091.       printf("\n\n\n");
  1092.       switch(nextfunum) {
  1093.          case 0:exit(0);
  1094.          case 1:TEST_abort();break;
  1095.          case 2:TEST_abs();break;
  1096.          case 3:TEST__access();break;
  1097.          case 4:TEST__alloca();break;
  1098.          case 5:TEST_asctime();break;
  1099.          case 6:TEST_asin();break;
  1100.          case 7:TEST_assert();break;
  1101.          case 8:TEST_atan();break;
  1102.          case 9:TEST_atan2();break;
  1103.          case 10:TEST_atexit();break;
  1104.          case 11:TEST_atof();break;
  1105.          case 12:TEST_atoi();break;
  1106.          case 13:TEST_atol();break;
  1107.          case 14:TEST__bdos();break;
  1108.          case 15:TEST__bios_disk();break;
  1109.          case 16:TEST__bios_equiplist();break;
  1110.          case 17:TEST__bios_keybrd();break;
  1111.          case 18:TEST__bios_memsize();break;
  1112.          case 19:TEST__bios_printer();break;
  1113.          case 20:TEST__bios_serialcom();break;
  1114.          case 21:TEST__bios_timeofday();break;
  1115.          case 22:TEST_bsearch();break;
  1116.          case 23:TEST__cabs();break;
  1117.          case 24:TEST_calloc();break;
  1118.          case 25:TEST_ceil();break;
  1119.          case 26:TEST__cgets();break;
  1120.          case 27:TEST__chdir();break;
  1121.          case 28:TEST__chmod();break;
  1122.          case 29:TEST__chsize();break;
  1123.          case 30:TEST_CLOCKS_PER_SEC();break;
  1124.          case 31:TEST__close();break;
  1125.          case 32:TEST__compare();break;
  1126.          case 33:TEST_cos();break;
  1127.          case 34:TEST_cosh();break;
  1128.          case 35:TEST__cprintf();break;
  1129.          case 36:TEST__cputs();break;
  1130.          case 37:TEST__creat();break;
  1131.          case 38:TEST__crotX();break;
  1132.          case 39:TEST_ctime();break;
  1133.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  1134.          }
  1135.       printf("\n\npress enter to select another function\n");
  1136.       gets(ans);
  1137.       }
  1138.    }
  1139.