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