home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / DRVALID.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  163 lines

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