home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJTST200.ZIP / tests / libc / dos / dostest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  5.0 KB  |  168 lines

  1. /*
  2.  * DOSTEST.C
  3.  *
  4.  * Test for _dos_xxxxx function group.
  5.  * Written by Peter Sulyok 1995 <sulyok@math.klte.hu>.
  6.  *
  7.  * Tested with the following DOS C compilers:
  8.  *   DJGPP v2.0
  9.  *   Borland C\C++ 3.1
  10.  *   Borland C\C++ 4.02
  11.  *   Borland C\C++ 4.5
  12.  *   Microsoft C 6.00A
  13.  *   Microsoft C\C++ 7.00
  14.  *   Microsoft Visual C\C++ 1.5
  15.  *   Symantec C\C++ 6.11
  16.  *   Watcom C\C++ 9.5
  17.  *   Watcom C\C++ 10.0A
  18.  */
  19.  
  20. #include <fcntl.h>
  21. #include <malloc.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <dos.h>
  25.  
  26. #if defined(__GNUC__) || defined(__DPMI32__) || defined(__WATCOMC__)
  27. #define FILESIZE 135000U    /* For protected mode C compilers */
  28. #else
  29. #define FILESIZE 64000U     /* For real mode C compilers */
  30. #endif
  31.  
  32. void main(void)
  33. {
  34.   unsigned int drive, drives_available, result, attr, i, fd, ft;
  35.   struct diskfree_t space;
  36.   unsigned long freebytes;
  37.   int handle;
  38.   unsigned char pattern;
  39.   unsigned char *file_buffer;
  40.   struct find_t fs;
  41.   struct _dosdate_t date;
  42.   struct _dostime_t time;
  43.  
  44.  
  45.   /* _dos_getdrive/_dos_setdrive test */
  46.   _dos_getdrive(&drive);
  47.   printf("The current drive is %c:\n", 'A' + drive - 1);
  48.   _dos_setdrive(1, &drives_available);
  49.   printf("The current drive is set to A:\n");
  50.   printf("(%u logical drives are available)\n", drives_available);
  51.   _dos_setdrive(drive, &drives_available);
  52.   printf("The current drive is restore to %c:\n", 'A' - 1 + drive);
  53.  
  54.  
  55.   /* _dos_getdiskfree test */
  56.   if ( !_dos_getdiskfree(0, &space) )
  57.   {
  58.     freebytes = (unsigned long)space.avail_clusters *
  59.                 (unsigned long)space.bytes_per_sector *
  60.                 (unsigned long)space.sectors_per_cluster;
  61.     printf("There is %lu free bytes on the current drive.\n", freebytes);
  62.   }
  63.   else
  64.     puts("Unable to get free room of default drive !");
  65.  
  66.  
  67.   /* _dos_setfileattr\_dos_getfileattr test */
  68.   if ( !_dos_getfileattr("DOSTEST.C", &attr) )
  69.   {
  70.     printf("Attributes of DOSTEST.C file is: %c%c%c%c%c%c\n",
  71.            ( attr & _A_ARCH )   ? 'A' : '.',
  72.            ( attr & _A_RDONLY ) ? 'R' : '.',
  73.            ( attr & _A_HIDDEN ) ? 'H' : '.',
  74.            ( attr & _A_SYSTEM ) ? 'S' : '.',
  75.            ( attr & _A_VOLID )  ? 'V' : '.',
  76.            ( attr & _A_SUBDIR ) ? 'D' : '.');
  77.   }
  78.   else
  79.     puts("Unable to get attributes of DOSTEST.C file !");
  80.  
  81.  
  82.   /* _dos_creat\_dos_close\_dos_write test */
  83.   if ( !_dos_creat("FOO.DAT", _A_ARCH, &handle) )
  84.   {
  85.     puts("FOO.DAT creating was successful.");
  86.     if ( (file_buffer=malloc(FILESIZE)) != NULL )
  87.     {
  88.       memset(file_buffer, 0, FILESIZE);
  89.       for(i=0, pattern=0; i < FILESIZE; i++, pattern++)
  90.         file_buffer[i] = pattern;
  91.       if ( !_dos_write(handle, file_buffer, FILESIZE, &result) )
  92.         printf("%i bytes written into FOO.DAT.\n", result);
  93.       free(file_buffer);
  94.     }
  95.     else
  96.       puts("Not enough memory for file buffer.");
  97.     _dos_getftime(handle, &fd, &ft);
  98.     _dos_close(handle);
  99.   }
  100.   else
  101.     puts("FOO.DAT creating failed.");
  102.  
  103.  
  104.   /* _dos_getftime test */
  105.   printf("FOO.DAT date and time is: %04u-%02u-%02u %02u:%02u:%02u.\n",
  106.        ((fd >> 9) & 0x7F) + 1980U, (fd >>  5) & 0x0F, fd & 0x1F,
  107.        (ft >> 11) & 0x1F, (ft >>  5) & 0x3F, (ft & 0x1F) * 2);
  108.  
  109.  
  110.   /* _dos_creatnew test */
  111.   if ( !_dos_creatnew("FOO.DAT", _A_ARCH, &handle) )
  112.     puts("FOO.DAT recreating was successful (????).");
  113.   else
  114.     /* This is GOOD ! */
  115.     puts("FOO.DAT recreating failed.");
  116.  
  117.  
  118.   /* _dos_open\_dos_read test */
  119.   if ( !_dos_open("FOO.DAT", O_RDWR, &handle) )
  120.   {
  121.     puts("FOO.DAT opening was successful.");
  122.     if ( (file_buffer=malloc(FILESIZE)) != NULL )
  123.     {
  124.       memset(file_buffer, 0, FILESIZE);
  125.       if ( !_dos_read(handle, file_buffer, FILESIZE, &result) )
  126.         printf("%i bytes read from FOO.DAT.\n", result);
  127.       for(i=0, pattern=0; i < result; i++, pattern++)
  128.         if ( file_buffer[i] != pattern )
  129.         {
  130.           printf("Different pattern at %X: %u - %u.\n", i, file_buffer[i], pattern);
  131.           break;
  132.         }
  133.       free(file_buffer);
  134.     }
  135.     else
  136.       puts("Not enough memory for file buffer.");
  137.     _dos_close(handle);
  138.   }
  139.   else
  140.     puts("FOO.DAT opening failed.");
  141.  
  142.  
  143.   /* _dos_findfirst\_dos_findnext test */
  144.   puts("Contents of current directory:");
  145.   if ( !_dos_findfirst("*.*", _A_ARCH|_A_RDONLY|_A_HIDDEN|_A_SYSTEM|_A_SUBDIR, &fs) )
  146.   {
  147.     do
  148.     {
  149.       printf("%-14s %10u %02u:%02u:%02u %02u/%02u/%04u\n",
  150.              fs.name, fs.size,
  151.              (fs.wr_time >> 11) & 0x1f,
  152.              (fs.wr_time >>  5) & 0x3f,
  153.              (fs.wr_time & 0x1f) * 2,
  154.              (fs.wr_date >>  5) & 0x0f,
  155.              (fs.wr_date & 0x1f),
  156.              ((fs.wr_date >> 9) & 0x7f) + 1980U);
  157.     } while( !_dos_findnext(&fs) );
  158.   }
  159.  
  160.  
  161.   /* _dos_gettime\_dos_getdate test. */
  162.   _dos_getdate(&date);
  163.   _dos_gettime(&time);
  164.   printf("The current date and time is: %04u-%02u-%02u %02u:%02u:%02u\n",
  165.          (unsigned)date.year, (unsigned)date.month, (unsigned)date.day,
  166.          (unsigned)time.hour, (unsigned)time.minute, (unsigned)time.second);
  167. }
  168.