home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / actlib14 / tools / testdrv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  2.9 KB  |  139 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "tools.h"
  4. #include "bool.h"
  5. #include <bios.h>
  6. #include <fcntl.h>
  7. #include <sys\stat.h>
  8. #include <errno.h>
  9.  
  10.  
  11.  
  12. static int diskError;
  13.  
  14.  
  15. void interrupt (*oldHandler)(); /* interrupt function pointer */
  16.  
  17.  
  18. /* For interrupt */
  19. #if defined(__TURBOC__)
  20. # pragma warn -par
  21. # pragma warn -rvl
  22. #endif
  23.  
  24. int myHandler( int errval, int ax, int bp, int si )
  25. {
  26.  diskError = LOWBYTE( errval );
  27.  
  28. #if 0
  29.  if ( ax < 0 )   /*  not a disk error  */
  30.     {
  31.      oldHandler();
  32.      hardresume( _AL );
  33.     }
  34. #endif
  35.  
  36.  hardretn( _HARDERR_ABORT );
  37. }
  38.  
  39. #if defined(__TURBOC__)
  40. # pragma warn .par
  41. # pragma warn .rvl
  42. #endif
  43.  
  44.  
  45. /***
  46.  *
  47.  *  Function   :    test_drive
  48.  *
  49.  *  Topics     :    Test the availability of a drive.
  50.  *
  51.  *  Parameters :    in    int drive         0 = A:, 1 = B:, 2 = C:,...
  52.  *
  53.  *  Return code:    combination (bitwise OR) of
  54.  *                  D_READABLE
  55.  *                  D_WRITEABLE
  56.  *                  D_NOFORMAT
  57.  *                  D_INVALID
  58.  *                  D_NOTINSERTED
  59.  *                  D_REMOVABLE
  60.  *                  D_SUBST
  61.  *                  D_REMOTE
  62.  *
  63.  *  OS/Compiler :   MS-DOS version >= 3.10
  64.  ***/
  65.  
  66. int test_drive( int drive )
  67. {
  68.  char fname[] = "x:\\t.t";
  69.  int fhandle, status = 0;
  70.  
  71. #define INTR 0x24
  72.  /* save the old Fatal Error Interrupt handler */
  73.  oldHandler  = _dos_getvect( INTR );
  74.  
  75.  /* install new Fatal Error Interrupt handler */
  76.  harderr( myHandler );
  77.  
  78.  diskError = -1;
  79.  
  80.  /* Open a file (write) */
  81.  *fname = drive + 'A';
  82.  fhandle = open( fname, O_CREAT, S_IREAD|S_IWRITE );
  83.  
  84.  /* restore the old Fatal Error Interrupt handler */
  85.  _dos_setvect( INTR, oldHandler );
  86.  
  87.  if ( fhandle >= 0 ) close( fhandle );
  88.  else if ( diskError < 0 ) diskError = errno * 100;
  89.  
  90.  if ( diskError < 0 ) unlink( fname );
  91.  
  92.  switch( diskError )
  93.  {
  94.   case -1: status |= D_WRITEABLE;
  95.  
  96.   case EACCES * 100:
  97.   case  0: status |= D_READABLE; break;
  98.  
  99.   case EINVFMT * 100:
  100.   case 12: status |= D_NOFORMAT; break;
  101.  
  102.   case ENMFILE * 100: /* No more files */
  103.   case EMFILE * 100 : /* Too many open files */
  104.   default: status |= D_INVALID;
  105.  }
  106.  
  107.  {
  108.  union REGS regs;
  109.  
  110.  /*  Test if drive is remote, subst  */
  111.  regs.h.ah = 0x44;    /* IOCTL function   */
  112.  regs.h.al = 0x09;    /* subfunction  */
  113.  regs.h.bl = drive + 1;
  114.  intdos( ®s, ®s );
  115.  if ( ! regs.x.cflag )
  116.     {
  117.      if ( regs.x.dx & 0x1000 ) status |= D_REMOTE;
  118.      if ( regs.x.dx & 0x8000 ) status |= D_SUBST;
  119.     }
  120.  
  121. /*  Test if drive is removable  */
  122.  regs.h.ah = 0x44;    /* IOCTL function   */
  123.  regs.h.al = 0x08;    /* subfunction  */
  124.  regs.h.bl = drive + 1;
  125.  intdos( ®s, ®s );
  126.  if ( ! regs.x.cflag )
  127.     if ( regs.x.ax == 0 ) status |= D_REMOVABLE;
  128.  }
  129.  
  130.  /* Test if disk inserted */
  131.  if ( (status & D_INVALID) && (status & D_REMOVABLE) )
  132.     {
  133.      status |= D_NOTINSERTED;
  134.      status &= ~D_INVALID;
  135.     }
  136.  
  137.  return status;
  138. }
  139.