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

  1. /*  Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13  */
  2.  
  3. /**** name=_dieeetomsbin ****/
  4. #define main() TEST__dieeetomsbin()
  5.  
  6. /*
  7.    Test the IEEE / Microsoft conversion routines.
  8. */
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12.  
  13. void main() {
  14.    float x, x2, x3;
  15.    double y, y2, y3;
  16.    unsigned char *a;
  17.  
  18.    x = 4.;
  19.    y = 4.;
  20.  
  21.    _fieeetomsbin(&x, &x2);
  22.  
  23.    a = (unsigned char*)&x;
  24.  
  25.    printf("\n IEEE float \n");
  26.    printf("%x  %x  %x  %x \n", a[0],a[1],a[2],a[3]);
  27.  
  28.    a = (unsigned char*)&x2;
  29.    printf(" MS equivalent \n");
  30.    printf("%x  %x  %x  %x \n",a[0],a[1],a[2],a[3]);
  31.  
  32.    _fmsbintoieee(&x2,&x3);
  33.  
  34.    a = (unsigned char*)&x3;
  35.    printf(" Back to IEEE \n");
  36.    printf("%x  %x  %x  %x \n",a[0],a[1],a[2],a[3]);
  37.  
  38.    /* Now the doubles. */
  39.  
  40.    a = (unsigned char*)&y;
  41.    printf("\n IEEE double \n");
  42.    printf("%x  %x  %x  %x  %x  %x  %x  %x\n",
  43.           a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
  44.  
  45.    _dieeetomsbin(&y,&y2);
  46.  
  47.    a = (unsigned char*)&y2;
  48.    printf(" MS equivalent \n");
  49.    printf("%x  %x  %x  %x  %x  %x  %x  %x\n",
  50.           a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
  51.  
  52.    _dmsbintoieee(&y2, &y3);
  53.  
  54.    a = (unsigned char*)&y3;
  55.    printf(" Back to IEEE \n");
  56.    printf("%x  %x  %x  %x  %x  %x  %x  %x\n",
  57.           a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
  58.    }
  59.  
  60. /* End _dieeetomsbin  */
  61. #undef main
  62. /**** name=difftime ****/
  63. #define main() TEST_difftime()
  64. /* End difftime  */
  65. #undef main
  66. /**** name=div ****/
  67. #define main() TEST_div()
  68.  
  69. #include <stdlib.h>
  70. #include <stdio.h>
  71.  
  72. void main() {
  73.    div_t result;
  74.    int num=20, den=3;
  75.  
  76.    puts("Testing div...\n");
  77.    result = div(num,den);
  78.    printf("Numerator = %d,"
  79.           "\tDenominator = %d.",num,den);
  80.    printf("\nResult: Quotient = %d,\n"
  81.           "Remainder= %d.\n\n",result.quot,result.rem);
  82.    num = -3;  den = -4;
  83.    printf("Numerator = %d,"
  84.           "\tDenominator = %d.",num,den);
  85.    result = div(num,den);
  86.    printf("\nResult: Quotient = %d,\n"
  87.           "Remainder= %d.\n",result.quot,result.rem);
  88.    }
  89.  
  90. /* End div  */
  91. #undef main
  92. /**** name=_dmsbintoieee ****/
  93. #define main() TEST__dmsbintoieee()
  94. /* End _dmsbintoieee  */
  95. #undef main
  96. /**** name=_dos_allocmem ****/
  97. #define main() TEST__dos_allocmem()
  98.  
  99. /*
  100.    This program allocates and frees 10 paragraphs of memory.
  101. */
  102.  
  103. #include <stdio.h>
  104. #include <dos.h>
  105.  
  106. unsigned seg_address;
  107.  
  108. void main() {
  109.    if (_dos_allocmem(10, &seg_address) != 0)
  110.       perror("_dos_allocmem failed.");
  111.    else {
  112.       printf("_dos_allocmem successful.\n");
  113.       if (_dos_freemem (seg_address) != 0)
  114.          perror("_dos_freemem failed.");
  115.       else
  116.          printf("_dos_freemem successful.\n");
  117.       }
  118.    }
  119.  
  120. /* End _dos_allocmem  */
  121. #undef main
  122. /**** name=_dos_close ****/
  123. #define main() TEST__dos_close()
  124.  
  125. /*
  126.    This  program  creates  a  dummy  file  and  then  closes  it using  the
  127.    _dos_close function.
  128. */
  129. #include <io.h>
  130. #include <dos.h>
  131. #include <fcntl.h>
  132. #include <sys/stat.h>
  133. #include <stdio.h>
  134.  
  135. void main() {
  136.    int pan, bad;
  137.  
  138.    puts("Testing _dos_close...\n");
  139.    pan = open("dsclose.tmp", 
  140.                O_CREAT | O_BINARY| O_TRUNC,
  141.                S_IREAD | S_IWRITE);
  142.  
  143.    if (pan == -1)
  144.       printf("Cannot truncate file dsclose.tmp,"
  145.              " error code=%d.\n",pan);
  146.    else
  147.       printf("File dsclose.tmp has been truncated,"
  148.              " handle=%d.\n",pan);
  149.    bad = _dos_close(pan);
  150.    if (bad)
  151.       puts("dsclose.tmp is not closed.");
  152.    else 
  153.       puts("dsclose.tmp is closed.");
  154.    }
  155.  
  156. /* End _dos_close  */
  157. #undef main
  158. /**** name=_dos_creatX ****/
  159. #define main() TEST__dos_creatX()
  160.  
  161. /*
  162.    This program opens a data file with _dos_creat and attempts to open it
  163.    again with _dos_creatnew, which fails because the file already exists.
  164. */
  165.  
  166. #include <stdio.h>
  167. #include <dos.h>
  168.  
  169. void main() {
  170.    int handle1, handle2;
  171.  
  172.    if (_dos_creat("doscreat.tmp",
  173.                   _A_NORMAL, &handle1) != 0)
  174.       printf("_dos_creat - no data file created.");
  175.    else
  176.       printf("_dos_creat - created data file"
  177.              " doscreat.tmp.\n");
  178.  
  179.    if (_dos_creatnew("doscreat.tmp",
  180.                      _A_RDONLY, &handle2)
  181.        != 0)
  182.       printf("_dos_creatnew - no data file created.");
  183.    else
  184.       printf("_dos_creatnew - created data"
  185.              " file doscreat.tmp.\n");
  186.    }
  187. /* End _dos_creat*  */
  188. #undef main
  189. /**** name=_dosexterr ****/
  190. #define main() TEST__dosexterr()
  191.  
  192. /*
  193.    This program tries to open a file and, if the  open  fails, displays the
  194.    DOS extended error information.
  195. */
  196.  
  197. #include <dos.h>
  198. #include <fcntl.h>
  199. #include <io.h>
  200. #include <stdio.h>
  201.  
  202. void main() {
  203.    struct DOSERROR err;
  204.    int handle;
  205.  
  206.    if ((handle = open("1junkfile", O_RDWR)) == -1) {
  207.       _dosexterr(&err);
  208.       printf("error  = %d\nclass  = %d"
  209.              "\naction = %d\nlocus  = %d\n",
  210.              err.exterror, err.class,
  211.              err.action, err.locus);
  212.       }
  213.    else
  214.       printf("Extended information printed"
  215.              " only if open fails.");
  216.    }
  217. /* End _dosexterr  */
  218. #undef main
  219. /**** name=_dos_findX ****/
  220. #define main() TEST__dos_findX()
  221.  
  222. /*
  223.    This program repeatedly prompts the user for a file specification and
  224.    prints all matching file names in the current directory.
  225. */
  226.  
  227. #include <stdio.h>
  228. #include <dos.h>
  229.  
  230. void main() {
  231.    struct find_t c_file;
  232.    char filespec[80];
  233.  
  234.    strcpy (filespec, "*.*");
  235.    do {
  236.       if (_dos_findfirst(filespec, _A_NORMAL,
  237.           & c_file) == 0) {
  238.          printf ("\nListing of files "
  239.                  "w/ filespec %s\n\n", filespec);
  240.          printf ("%12s is %6lu bytes "
  241.                  "long\n", c_file.name, c_file.size);
  242.          while (_dos_findnext(&c_file) == 0)
  243.             printf("%12s is %6lu bytes long\n",
  244.                     c_file.name, c_file.size);
  245.          }
  246.       else perror("error in findfirst");
  247.       printf("\nEnter new filespec ( . to exit): ");
  248.       scanf("%s", filespec);
  249.       } while (*filespec != '.');
  250.    }
  251. /* End _dos_find*  */
  252. #undef main
  253. /**** name=_dos_freemem ****/
  254. #define main() TEST__dos_freemem()
  255. /* End _dos_freemem  */
  256. #undef main
  257. /**** name=_dos_getdate ****/
  258. #define main() TEST__dos_getdate()
  259.  
  260. /*
  261.    This program displays the current system date and time.
  262. */
  263.  
  264. #include <stdio.h>
  265. #include <dos.h>
  266.  
  267. void main() {
  268.    char *day[] =
  269.       {"Sunday", "Monday", "Tuesday", "Wednesday",
  270.        "Thursday", "Friday", "Saturday"};
  271.    struct dosdate_t date;
  272.    struct dostime_t time;
  273.  
  274.    _dos_getdate (&date);
  275.    _dos_gettime (&time);
  276.    printf("The date is: %s, %d-%d-%d\n",
  277.           day[date.dayofweek],
  278.           date.month, date.day, date.year);
  279.    printf("The time is: %d:%d:%d.%d\n",
  280.            time.hour, time.minute,
  281.            time.second, time.hsecond);
  282.    }
  283.  
  284. /* End _dos_getdate  */
  285. #undef main
  286. /**** name=_dos_getdiskfree ****/
  287. #define main() TEST__dos_getdiskfree()
  288.  
  289. /*
  290.    This program computes the total capacity and remaining free space of the
  291.    default drive.
  292. */
  293.  
  294. #include <stdio.h>
  295. #include <dos.h>
  296.  
  297. void main() {
  298.    struct diskfree_t drive;
  299.    unsigned tc, ac, spc, bps;
  300.  
  301. /* Get info on default drive. */
  302.    _dos_getdiskfree (0, &drive);
  303.    tc  = drive.total_clusters;
  304.    ac  = drive.avail_clusters;
  305.    spc = drive.sectors_per_cluster;
  306.    bps = drive.bytes_per_sector;
  307.  
  308.    printf("The current drive has a capacity of %lu "
  309.           "bytes.\n", (unsigned long)tc*spc*bps);
  310.    printf("The current drive has %lu bytes free.\n",
  311.            (unsigned long)ac*spc*bps);
  312.    }
  313.  
  314. /* End _dos_getdiskfree  */
  315. #undef main
  316. /**** name=_dos_getdrive ****/
  317. #define main() TEST__dos_getdrive()
  318.  
  319. /*
  320.    This program will get disk drive information for a user-specified drive.
  321. */
  322.  
  323. #include <stdio.h>
  324. #include <dos.h>
  325. #include <bios.h>
  326. #include <ctype.h>
  327.  
  328. void main() {
  329.    unsigned drive, savedrive;
  330.    unsigned logicaldrives, ok;
  331.    int choice;
  332.    struct diskfree_t dinfo;
  333.  
  334. /* Save the current default drive. */
  335.    _dos_getdrive( &savedrive);
  336.  
  337. /* Get choice from user. */
  338.    do {
  339.       printf("\nFor which drive would you"
  340.             " like info (a-z)?: ");
  341.       choice = _bios_keybrd(_KEYBRD_READ);
  342.       printf("%c\n\n", (char) choice);
  343.       if ((ok = isalpha((char) choice)) == 0)
  344.           printf("Enter a drive letter.\n");
  345.       } while (!ok);
  346.  
  347. /* Change drive to selected. */
  348.    drive = (unsigned)(tolower(choice) - 'a' + 1);
  349.    _dos_setdrive(drive, &logicaldrives);
  350.  
  351. /* Get drive info and print it. */
  352.    _dos_getdiskfree(drive, &dinfo);
  353.    printf("Info for drive %c\n",
  354.            toupper((char)choice));
  355.    printf(" total clusters    : %d\n",
  356.            dinfo.total_clusters);
  357.    printf(" available clusters: %d\n",
  358.            dinfo.avail_clusters);
  359.    printf(" sectors/cluster   : %d\n",
  360.            dinfo.sectors_per_cluster);
  361.    printf(" bytes/sector      : %d\n",
  362.            dinfo.bytes_per_sector);
  363.  
  364. /* Change drive back. */
  365.  
  366.    _dos_setdrive(savedrive, &logicaldrives);
  367.    }
  368.  
  369. /* End _dos_getdrive  */
  370. #undef main
  371. /**** name=_dos_getfileattr ****/
  372. #define main() TEST__dos_getfileattr()
  373.  
  374. /*
  375.    Create a file and read its attributes.
  376. */
  377.  
  378. #include <stdio.h>
  379. #include <dos.h>
  380.  
  381. void main() {
  382.    unsigned attribute;
  383.    int handle;
  384. /*
  385.    Create file getattr.tmp only if it does not already exist.
  386. */
  387.    if (_dos_creatnew("getattr.tmp",
  388.        _A_NORMAL, &handle) != 0)
  389.       perror("_dos_creatnew");
  390.    else {
  391.       printf("Created file getattr.tmp.\n");
  392.  
  393.    /* Get file attributes of newly created file. */
  394.       _dos_getfileattr("getattr.tmp", &attribute);
  395.       if ((attribute & _A_RDONLY) != 0)
  396.          printf("getattr.tmp read only.\n");
  397.       else
  398.          printf("getattr.tmp not read only.\n");
  399.    /* Reset file attributes. */
  400.       _dos_setfileattr("getattr.tmp", _A_RDONLY);
  401.    /* Get file attributes again. */
  402.       _dos_getfileattr("getattr.tmp",&attribute);
  403.       if ((attribute & _A_RDONLY) != 0)
  404.          printf("getattr.tmp now read only.\n");
  405.       else
  406.          printf("getattr.tmp not read only.\n");
  407.       }
  408.    }
  409. /* End _dos_getfileattr  */
  410. #undef main
  411. /**** name=_dos_getftime ****/
  412. #define main() TEST__dos_getftime()
  413.  
  414. /*
  415.    This program creates a file and displays its date stamp.
  416. */
  417.  
  418. #include <stdio.h>
  419. #include <fcntl.h>
  420. #include <dos.h>
  421.  
  422. void main() {
  423.    unsigned date, time, day, month, year;
  424.    int handle;
  425.  
  426. /* Create a new file. */
  427.    if (_dos_creatnew("getftime.tmp",
  428.        _A_NORMAL, &handle) != 0)
  429.       perror("_dos_creatnew error");
  430.    else {
  431.    /* Get date stamp on file. */
  432.       _dos_getftime(handle, &date, &time);
  433.  
  434.    /* Compute and display statistics. */
  435.       day   = date & 0x1f;
  436.       month = date >> 5 & 0xf;
  437.       year  = (date >> 9 & 0x7f) + 1980;
  438.       printf("The file getftime.tmp is stamped:\n");
  439.       printf("\tday  : %u\n", day);
  440.       printf("\tmonth: %u\n", month);
  441.       printf("\tyear : %u\n", year);
  442.       }
  443.    }
  444.  
  445. /* End _dos_getftime  */
  446. #undef main
  447. /**** name=_dos_gettime ****/
  448. #define main() TEST__dos_gettime()
  449. /* End _dos_gettime  */
  450. #undef main
  451. /**** name=_dos_open ****/
  452. #define main() TEST__dos_open()
  453. /* End _dos_open  */
  454. #undef main
  455. /**** name=_dos_read ****/
  456. #define main() TEST__dos_read()
  457.  
  458. /*
  459.    This program opens, reads from, and closes a file.
  460. */
  461.  
  462. #include <stdio.h>
  463. #include <dos.h>
  464. #include <fcntl.h>
  465. #define BLOCK 1024
  466.  
  467. void main() {
  468.    int handle;
  469.    _Far char buffer[BLOCK];
  470.    unsigned bytes;
  471.  
  472.    /* Open file in read-only mode. */
  473.    if (_dos_open("test.dat",
  474.           O_RDONLY, &handle) != 0)
  475.       perror("_dos_open failed to open file.");
  476.    else {
  477.       printf("_dos_open opened file.\n");
  478.    /* Read from file into buffer. */
  479.       if (_dos_read(handle,buffer,BLOCK,&bytes) != 0)
  480.          perror("_dos_read failed to read file.");
  481.       else
  482.          printf("\nBuffer contents:\n%s", buffer);
  483.    /* Close file. */
  484.       if (_dos_close(handle) != 0)
  485.          perror("Close failed.\n");
  486.       else
  487.          printf("File successfully closed.\n");
  488.       }
  489.    }
  490. /* End _dos_read  */
  491. #undef main
  492. #undef  BLOCK
  493. /**** name=_dos_setblock ****/
  494. #define main() TEST__dos_setblock()
  495.  
  496. /*
  497.    This program allocates a block  of memory, increases the allocation, and
  498.    releases the memory.
  499. */
  500.  
  501. #include <stdio.h>
  502. #include <dos.h>
  503.  
  504. unsigned segment;
  505. unsigned maxavail;
  506.  
  507. void main() {
  508. /* Allocate five paragraphs of memory. */
  509.    if (_dos_allocmem(5, &segment) != 0)
  510.       perror("_dos_allocmem error");
  511.    else {
  512.       printf("Five paragraphs allocated.\n");
  513.  
  514.    /* Increase allocation to ten paragraphs. */
  515.       if (_dos_setblock(10,segment,&maxavail) != 0)
  516.          perror("_dos_setblock error.");
  517.       else
  518.          printf("Allocation increased to "
  519.                 "ten paragraphs.\n");
  520.  
  521.    /* Free allocated memory. */
  522.       if (_dos_freemem(segment) != 0)
  523.          perror("_dos_freemem error.");
  524.       else
  525.          printf("Memory freed.\n");
  526.       }
  527.    }
  528.  
  529. /* End _dos_setblock  */
  530. #undef main
  531. /**** name=_dos_setdate ****/
  532. #define main() TEST__dos_setdate()
  533.  
  534. /*
  535.    This program sets the new date and time for the system.
  536. */
  537.  
  538. #include <stdio.h>
  539. #include <dos.h>
  540.  
  541. void main() {
  542.    char *day[] = {"Sunday", "Monday", "Tuesday", 
  543.                   "Wednesday", "Thursday",
  544.                   "Friday", "Saturday"};
  545.    struct dosdate_t date, date2;
  546.    struct dostime_t time, time2;
  547.  
  548.    puts("Testing _dos_setdate...\n");
  549.    _dos_getdate(&date2);
  550.    _dos_gettime(&time2);
  551.  
  552.    day[date.dayofweek] = "Sunday";
  553.    date.month = 12;
  554.    date.day = 25;
  555.    date.year = 2000;
  556.    _dos_setdate (&date);                          /* Set the above date. */
  557.  
  558.    time.hour = 0;
  559.    time.minute = 0;
  560.    time.second = 0;
  561.    time.hsecond = 0;
  562.    _dos_settime(&time);                           /* Set the above time. */
  563.  
  564.    printf("The date is: %s, %d-%d-%d\n", 
  565.            day[date.dayofweek], 
  566.            date.month, date.day, date.year);
  567.    printf("The time is: %d:%d:%d.%d\n",
  568.            time.hour, time.minute, 
  569.            time.second, time.hsecond);
  570.    _dos_setdate(&date2);
  571.    _dos_settime(&time2);
  572.    }
  573.  
  574. /* End _dos_setdate  */
  575. #undef main
  576. /**** name=_dos_setdrive ****/
  577. #define main() TEST__dos_setdrive()
  578.  
  579. /*
  580.    This program lists all currently installed logical drives.
  581. */
  582.  
  583. #include <stdio.h>
  584. #include <dos.h>
  585.  
  586. void main() {
  587.    unsigned drive;
  588.    unsigned logical_drives;
  589.    unsigned savedrive;
  590.    int i;
  591.  
  592. /* Save current drive. */
  593.    _dos_getdrive(&savedrive);
  594.  
  595. /* Give a list of all currently installed drives. */
  596.    for (i = 1; i <= 26; i++) {
  597.       _dos_setdrive(i, &logical_drives);
  598.       _dos_getdrive(&drive);
  599.       if (i == drive)
  600.          printf("drive %c: is installed\n",
  601.                 'a' + i - 1);
  602.       else
  603.          printf("drive %c: is not installed\n",
  604.                 'a' + i - 1);
  605.       }
  606.    printf("\nThere are %d logical drives\n",
  607.           logical_drives);
  608.  
  609. /* Restore to original drive. */
  610.    _dos_setdrive(savedrive, &logical_drives);
  611.    }
  612.  
  613. /* End _dos_setdrive  */
  614. #undef main
  615. /**** name=_dos_setfileattr ****/
  616. #define main() TEST__dos_setfileattr()
  617.  
  618. #include <stdio.h>
  619. #include <dos.h>
  620.  
  621. void main() {
  622.    unsigned attribute;
  623.    int handle;
  624.  
  625. /*
  626.    Create file setattr.tmp only if it does not already exist.
  627. */
  628.  
  629.    if (_dos_creatnew("setattr.tmp", _A_NORMAL,
  630.                                                              &handle) != 0)
  631.       perror("_dos_creatnew");
  632.    else {
  633.       printf("Created file setattr.tmp.\n");
  634.       /* Get file attributes of newly created file. */
  635.       _dos_getfileattr("setattr.tmp", &attribute);
  636.       if ((attribute & _A_RDONLY) != 0)
  637.          printf("setattr.tmp read only.\n");
  638.       else
  639.          printf("setattr.tmp not read only.\n");
  640.  
  641.       /* Reset file attributes. */
  642.       _dos_setfileattr("setattr.tmp", _A_RDONLY);
  643.  
  644.       /* Get file attributes again. */
  645.       _dos_getfileattr("setattr.tmp",&attribute);
  646.       if ((attribute & _A_RDONLY) != 0)
  647.          printf("setattr.tmp now read only.\n");
  648.       else
  649.          printf("setattr.tmp now not read only.\n");
  650.       }
  651.    }
  652. /* End _dos_setfileattr  */
  653. #undef main
  654. /**** name=_dos_setftime ****/
  655. #define main() TEST__dos_setftime()
  656.  
  657. /*
  658.    This program creates a file and alters its date/time stamp.
  659. */
  660.  
  661. #include <stdio.h>
  662. #include <fcntl.h>
  663. #include <dos.h>
  664.  
  665. void main() {
  666.    unsigned date, time, day, month, year;
  667.    int handle;
  668.  
  669.    /* Create a new file. */
  670.    if (_dos_creatnew("setftime.tmp", _A_NORMAL,
  671.                                      &handle) != 0)
  672.       perror("_dos_creatnew error");
  673.    else {
  674.    /* Set date and time stamp on file. */
  675.       date = 0x0;
  676.       time = 0x0;
  677.       _dos_setftime(handle, date, time);
  678.  
  679.    /* Compute and display new statistics. */
  680.       _dos_getftime(handle, &date, &time);
  681.       day   = date & 0x1f;
  682.       month = date >> 5 & 0xf;
  683.       year  = (date >> 9 & 0x7f) + 1980;
  684.  
  685.       printf("The file setftime.tmp is now stamped:\n");
  686.       printf("\tday  : %u\n", day);
  687.       printf("\tmonth: %u\n", month);
  688.       printf("\tyear : %u\n", year);
  689.       }
  690.    }
  691.  
  692. /* End _dos_setftime  */
  693. #undef main
  694. /**** name=_dos_settime ****/
  695. #define main() TEST__dos_settime()
  696.  
  697. /*
  698.    This program alters then restores the current system date and time.
  699.  
  700.    CAUTION: YOUR SYSTEM TIME AND DATE WILL BE SLIGHTLY ALTERED!!
  701. */
  702.  
  703. #include <stdio.h>
  704. #include <dos.h>
  705.  
  706. char *days[] ={"Sunday", "Monday", "Tuesday",
  707.    "Wednesday", "Thursday", "Friday", "Saturday"};
  708.  
  709. void main() {
  710.    struct dosdate_t date;
  711.    struct dostime_t time;
  712.    unsigned char day;
  713.    unsigned int  year;
  714.    unsigned char hour;
  715.  
  716. /* Print the current system date and time. */
  717.    _dos_getdate (&date);
  718.    _dos_gettime (&time);
  719.    printf("The date is: %s, %d-%d-%d\n",
  720.            days[date.dayofweek], date.month,
  721.            date.day, date.year);
  722.    printf("The time is: %d:%d:%d.%d\n", time.hour,
  723.            time.minute, time.second, time.hsecond);
  724.  
  725. /* Save then alter the date and time. */
  726.    year = date.year, date.year = 1999;
  727.    day  = date.day,  date.day  = 0;
  728.    hour = time.hour, time.hour = 11;
  729.  
  730. /* Print the new date and time. */
  731.    _dos_setdate(&date);
  732.    _dos_settime(&time);
  733.    printf("The new date is: %s, %d-%d-%d\n",
  734.            days[date.dayofweek], date.month,
  735.            date.day, date.year);
  736.  
  737.    printf("The new time is: %d:%d:%d.%d\n",
  738.            time.hour, time.minute, time.second,
  739.            time.hsecond);
  740.  
  741. /* Restore correct date and time. */
  742.    date.year = year;
  743.    date.day  = day;
  744.    time.hour = hour;
  745.    _dos_setdate(&date);
  746.    _dos_settime(&time);
  747.    printf("The restored date is: %s,"
  748.           " %d-%d-%d\n", days[date.dayofweek],
  749.            date.month, date.day, date.year);
  750.    printf("The restored time is: %d:%d:%d.%d\n",
  751.            time.hour, time.minute,
  752.            time.second, time.hsecond);
  753.    }
  754.  
  755. /* End _dos_settime  */
  756. #undef main
  757. /**** name=_dos_write ****/
  758. #define main() TEST__dos_write()
  759.  
  760. /*
  761.    This program opens, writes to, and closes a file.
  762. */
  763. #include <stdio.h>
  764. #include <fcntl.h>
  765. #include <string.h>
  766. #include <dos.h>
  767.  
  768. void main() {
  769.    int  handle;
  770.    void *buffer =
  771.       "This line to go into file doswrite.tmp.";
  772.    unsigned length = 40, bytes;
  773.  
  774.    /* Open file in read/write mode. */
  775.    if (_dos_creat("doswrite.tmp",O_RDWR,&handle) != 0)
  776.       perror("_dos_open could not open file"
  777.              " doswrite.tmp.");
  778.    else {
  779.       printf("_dos_open opened file doswrite.tmp.\n");
  780.       /* Copy buffer to file. */
  781.       if (_dos_write(handle, (_Far void *) buffer,
  782.           length, &bytes) != 0) {
  783.          perror("_dos_write failed to write buffer to"
  784.                 " doswrite.tmp.");
  785.          printf("%d bytes actually written\n", bytes);
  786.          }
  787.       else printf("_dos_write wrote buffer"
  788.                   " to file doswrite.tmp.\n");
  789.       /* Close file. */
  790.       if (_dos_close(handle) != 0)
  791.          perror("_dos_close failed to close file"
  792.                 " doswrite.tmp.");
  793.       else
  794.          printf("_dos_close closed file"
  795.                 " doswrite.tmp.\n");
  796.       }
  797.    }
  798. /* End _dos_write  */
  799. #undef main
  800. /**** name=_dupX ****/
  801. #define main() TEST__dupX()
  802.  
  803. #include <io.h>
  804. #include <fcntl.h>
  805. #include <sys/stat.h>
  806. #include <stdio.h>
  807. #include <conio.h>
  808. #define  STDOUT 1
  809.  
  810. int  pan, fry;
  811. char c;
  812. extern void f1();
  813. extern void f2();
  814.  
  815. void main() {
  816.    printf("\nEXAMPLE 1:\n");
  817.    f1();
  818.    printf("\nEXAMPLE 2:\n");
  819.    f2();
  820.    }
  821.  
  822. void f1() {
  823.    pan = open("test.dat",O_RDONLY | O_BINARY);
  824.  
  825.    if (pan != -1) {
  826.       printf("File test.dat exists, handle=%d.\n",pan);
  827.       fry = _dup(pan);
  828.       while(read(pan, &c, 1), putch(c), (c != '='));
  829.       close(pan); /* Close the first file handle. */
  830.       while(read(fry, &c, 1) > 0)
  831.    /* Continue reading dup.  */
  832.          putch(c);                                       /* File handle. */
  833.       }
  834.    else
  835.        printf("File test.dat cannot be opened,"
  836.               " error code = %d.\n",pan);
  837.    close(pan);
  838.    }
  839.  
  840. void f2() {
  841.    pan = open("dup2test.tmp",
  842.                O_CREAT | O_BINARY | O_TRUNC,
  843.                S_IWRITE);
  844.  
  845.    if (pan != -1) {
  846.       printf("Created dup2test.tmp, handle=%d\n",pan);
  847.       fry=_dup(STDOUT);
  848.       printf("This message should show"
  849.              " on the screen.\n");
  850.       _dup2(pan, STDOUT);
  851.       printf("This message must not show"
  852.              " on the screen!!!\n");
  853.       _dup2(fry,STDOUT);
  854.       printf("And this is the last screen message.\n");
  855.       }
  856.    else
  857.       printf("file dup2test.tmp cannot be opened,"
  858.              " error code=%d\n",pan);
  859.    close(pan);
  860.    }
  861.  
  862. /* End _dup*  */
  863. #undef main
  864. #undef   STDOUT
  865. /**** name=_ecvt ****/
  866. #define main() TEST__ecvt()
  867.  
  868. /*
  869.    This program uses _ecvt to convert the number "E" to a string.
  870. */
  871.  
  872. #include <stdlib.h>
  873. #define E 2.718281828459045
  874.  
  875. void main() {
  876.    char  *result; /* _ecvt return string.                                */
  877.    int    dpos;   /* Stored decimal-point position.                      */
  878.    int    sign;   /* Sign of converted number.                           */
  879.  
  880.    result = _ecvt(E, 5, &dpos, &sign);
  881.    printf("------- _ecvt test --------\n");
  882.    printf(" value: %0.15lf\n", E);
  883.    printf("--------------------------\n");
  884.    printf("resulting string: %s\n decimal point @: %d\n"
  885.           "            sign: %d\n", result, dpos, sign);
  886.    }
  887.  
  888. /* End _ecvt  */
  889. #undef main
  890. #undef  E
  891. /**** name=_eof ****/
  892. #define main() TEST__eof()
  893.  
  894. #include <io.h>
  895. #include <conio.h>
  896. #include <fcntl.h>
  897. #include <stdio.h>
  898.  
  899. void main() {
  900.    int  pan;
  901.    char c;
  902.  
  903.    pan = open("test.dat", O_RDONLY);
  904.    if (pan != -1) {
  905.       printf("File test.dat exists,"
  906.              " handle=%d.\n", pan);
  907.       while (!_eof(pan)) {
  908.          read(pan, &c, 1);
  909.          putch(c);
  910.          }
  911.       }
  912.    else
  913.       printf("File test.dat cannot be opened,"
  914.              " error code = %d.\n", pan);
  915.    }
  916.  
  917. /* End _eof  */
  918. #undef main
  919. /**** name=_execX ****/
  920. #define main() TEST__execX()
  921.  
  922. /*
  923.    _exec a child process chosen by the user.
  924. */
  925.  
  926. #include <stdio.h>
  927. #include <process.h>
  928. #include <string.h>
  929.  
  930. void main() {
  931.    char path[120], *args[10];
  932.    int i;
  933.  
  934.    printf("Enter name of program to execute"
  935.           " (with arguments): ");
  936.    gets(path);
  937.    args[0] = strtok(path, " ");
  938.    i = 1;
  939.    while ((args[i] = strtok(NULL, " ")) != NULL)
  940.       i++;
  941.    printf("\nexec-ing subprocess %s...\n", path);
  942.    /*
  943.       If exec() was successful, the following statements should not be
  944.       seen.
  945.    */
  946.    printf("Failed to exec program, return code is: %d",    _execv(path, args));
  947.    printf(" errno is: %d\n",errno);
  948.    }
  949.  
  950. /* End _exec*  */
  951. #undef main
  952. /**** name=exit ****/
  953. #define main() TEST_exit()
  954.  
  955. #include <stdlib.h>
  956. #include <stdio.h>
  957.  
  958. void main() {
  959.    int day;
  960.    printf("Enter the week day desired: ");
  961.    scanf("%d", &day);
  962.    if ((day >= 1) && (day <= 7)) {
  963.       printf("\nCorrect day entry."
  964.              "  Exiting Program.\n");
  965.       exit(0);     /* Returns correct status. */
  966.       }
  967.    else {
  968.       printf("\nIncorrect!  Exiting Program!\n");
  969.       exit(1);     /* Returns bad status. */
  970.       }
  971.    }
  972.  
  973. /* End exit  */
  974. #undef main
  975. /**** name=exp ****/
  976. #define main() TEST_exp()
  977.  
  978. /*
  979.    This program tests the functions exp() and cosh().
  980. */
  981.  
  982. #include <math.h>
  983. #include <stdio.h>
  984.  
  985. void main() {
  986.    double x = 2.0, result;
  987.  
  988.    result = ((exp(x) + exp(-x))/2);
  989.    printf("Result => %f\n",result);
  990.    result = cosh(x);
  991.    printf("Result => %f\n",result);
  992.    result = exp(1000.0);
  993.    printf("Result => %f\n",result);
  994.    result = exp(-50.0);
  995.    printf("Result => %f\n",result);
  996.    }
  997.  
  998. /* End exp  */
  999. #undef main
  1000.  
  1001. /*****names*****/
  1002.  
  1003. char * names[]={
  1004.    "_dieeetomsbin",
  1005.    "div",
  1006.    "_dos_allocmem",
  1007.    "_dos_close",
  1008.    "_dos_creatX",
  1009.    "_dosexterr",
  1010.    "_dos_findX",
  1011.    "_dos_getdate",
  1012.    "_dos_getdiskfree",
  1013.    "_dos_getdrive",
  1014.    "_dos_getfileattr",
  1015.    "_dos_getftime",
  1016.    "_dos_read",
  1017.    "_dos_setblock",
  1018.    "_dos_setdate",
  1019.    "_dos_setdrive",
  1020.    "_dos_setfileattr",
  1021.    "_dos_setftime",
  1022.    "_dos_settime",
  1023.    "_dos_write",
  1024.    "_dupX",
  1025.    "_ecvt",
  1026.    "_eof",
  1027.    "_execX",
  1028.    "exp",
  1029.    "exit",
  1030.    "",""};
  1031.    int nextfunum;
  1032. void main() {
  1033.    char ans[90];
  1034.    for (;;) {
  1035.       for (int j=0;j< 26;j++)
  1036.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  1037.       else printf("%4d %-21s",j+1,names[j]);
  1038.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  1039.       gets(ans);
  1040.       if (ans[0] != 0) nextfunum=atoi(ans);
  1041.       printf("\n\n\n");
  1042.       switch(nextfunum) {
  1043.          case 0:exit(0);
  1044.          case 1:TEST__dieeetomsbin();break;
  1045.          case 2:TEST_div();break;
  1046.          case 3:TEST__dos_allocmem();break;
  1047.          case 4:TEST__dos_close();break;
  1048.          case 5:TEST__dos_creatX();break;
  1049.          case 6:TEST__dosexterr();break;
  1050.          case 7:TEST__dos_findX();break;
  1051.          case 8:TEST__dos_getdate();break;
  1052.          case 9:TEST__dos_getdiskfree();break;
  1053.          case 10:TEST__dos_getdrive();break;
  1054.          case 11:TEST__dos_getfileattr();break;
  1055.          case 12:TEST__dos_getftime();break;
  1056.          case 13:TEST__dos_read();break;
  1057.          case 14:TEST__dos_setblock();break;
  1058.          case 15:TEST__dos_setdate();break;
  1059.          case 16:TEST__dos_setdrive();break;
  1060.          case 17:TEST__dos_setfileattr();break;
  1061.          case 18:TEST__dos_setftime();break;
  1062.          case 19:TEST__dos_settime();break;
  1063.          case 20:TEST__dos_write();break;
  1064.          case 21:TEST__dupX();break;
  1065.          case 22:TEST__ecvt();break;
  1066.          case 23:TEST__eof();break;
  1067.          case 24:TEST__execX();break;
  1068.          case 25:TEST_exp();break;
  1069.          case 26:TEST_exit();break;
  1070.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  1071.          }
  1072.       printf("\n\npress enter to select another function\n");
  1073.       gets(ans);
  1074.       }
  1075.    }
  1076.