home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / 54 < prev    next >
Encoding:
Text File  |  1992-04-14  |  24.1 KB  |  985 lines

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