home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / DRVALID.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  4KB  |  167 lines

  1. /*
  2. **  DRVALID.C - validate disk drives
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. **
  11. **  Uses ABSDISKC.C and ABSDISK.ASM in SNIPPETS.
  12. */
  13.  
  14. #include <dos.h>
  15. #include <stdlib.h>
  16.  
  17. typedef enum {ERROR = -1, SUCCESS, FALSE = 0, TRUE} LOGICAL;
  18.  
  19. /*
  20. **  Function to read an absolute disk sector
  21. */
  22.  
  23. int AbsDiskRead(unsigned short, size_t, size_t, void *);
  24.  
  25. /*
  26. **  getdrv()
  27. **
  28. **  Just as getcwd() returns the default directory, getdrv() returns
  29. **  the current drive.
  30. **
  31. **  Arguments: None.
  32. **
  33. **  Returns:   Current drive (0 = A:, 1 = B:, etc.)
  34. **
  35. **  Side effects: none
  36. */
  37.  
  38. int getdrv(void)
  39. {
  40.       union REGS regs;
  41.  
  42.       regs.h.ah = 0x19;
  43.       intdos(®s, ®s);
  44.       return (regs.h.al);
  45. }
  46.  
  47. /*
  48. **  chdrv()
  49. **
  50. **  Like chdir(), except changes drives rather than directories.
  51. **
  52. **  Arguments: 1 - target drive (0 = A:, 1 = B:, etc.)
  53. **
  54. **  Returns: SUCCESS or ERROR
  55. **
  56. **  Side effects: none
  57. */
  58.  
  59. LOGICAL chdrv(int drive)
  60. {
  61.       union REGS regs;
  62.  
  63.       regs.h.ah = 0x0e;
  64.       regs.h.dl = (char)drive;
  65.       intdos(®s, ®s);
  66.       if (drive != getdrv())
  67.             return ERROR;
  68.       else  return SUCCESS;
  69. }
  70.  
  71. /*
  72. **  drvalid()
  73. **
  74. **  Verifies whether a logical disk drive is available without
  75. **  triggering the DOS critical error handler.
  76. **
  77. **  Arguments: 1 - target drive (0 = A;, 1 = B:, etc.)
  78. **
  79. **  Returns:   TRUE  - drive is valid
  80. **             FALSE - drive is invalid
  81. **
  82. **  Side effects: none
  83. */
  84.  
  85. LOGICAL drvalid(int drive)
  86. {
  87.       int original, result;
  88.  
  89.       original = getdrv();
  90.       result   = (SUCCESS == chdrv(drive));
  91.       chdrv(original);
  92.       return result;
  93. }
  94.  
  95. /*
  96. **  drvrdy()
  97. **
  98. **  Checks whether a drive with removable media is ready.
  99. **
  100. **  Arguments: 1 - target drive (0 = A;, 1 = B:, etc.)
  101. **
  102. **  Returns:   TRUE  - drive is ready
  103. **             FALSE - drive is not ready
  104. **             ERROR - other read error
  105. **
  106. **  Side effects: none
  107. */
  108.  
  109. LOGICAL drvrdy(int drive)
  110. {
  111.       int status;
  112.       char buf[2048];         /* nice & roomy   */
  113.  
  114.       status = AbsDiskRead(drive, 1, 0, buf);
  115.       if (0 == status)
  116.             return TRUE;
  117.       status &= 0xff;
  118.       if (2 == status)
  119.             return FALSE;
  120.       else  return ERROR;
  121. }
  122.  
  123. #ifdef TEST
  124.  
  125. #include <stdio.h>
  126. #include <ctype.h>
  127.  
  128. int main(int argc, char *argv[])
  129. {
  130.       int drive;
  131.  
  132.       if (2 > argc)
  133.       {
  134.             puts("Usage: DRVALID drive[:]");
  135.             return EXIT_FAILURE;
  136.       }
  137.       drive = toupper(*argv[1]);
  138.       if (!isalpha(drive))
  139.       {
  140.             puts("Error: Invalid drive name");
  141.             return EXIT_FAILURE;
  142.       }
  143.       printf("Drive %c: is %svalid\n", drive,
  144.             drvalid(drive - 'A') ? "" : "not ");
  145.       if (2 < _osmajor)
  146.       {
  147.             union REGS regs;
  148.  
  149.             regs.x.ax = 0x4408;
  150.             regs.h.bl = (unsigned char)(drive - '@');
  151.             intdos(®s, ®s);
  152.             printf("ioctl returned Cflag=%s\n",
  153.                   regs.x.cflag ? "TRUE" : "FALSE");
  154.             printf("ioctl returned AX=0x%X\n", regs.x.ax);
  155.             printf("Drive %c is%s removable\n", drive,
  156.                   regs.x.ax ? " not" : "");
  157.             if (0 == regs.x.ax)
  158.             {
  159.                   printf("Drive %c is %sready\n", drive,
  160.                         drvrdy(drive - 'A') ? "" : "not ");
  161.             }
  162.       }
  163.       return EXIT_SUCCESS;
  164. }
  165.  
  166. #endif /* TEST */
  167.