Mail Archives: djgpp/1999/10/04/08:54:11
On Sun, 3 Oct 1999, Damian Yerrick wrote:
> /* To determine the maximum number of drives available on this system */
> _dos_getdrive(&curDrive);
> printf("Current drive is %c:\n", curDrive + '@');
> _dos_setdrive(curDrive, &nDrives);
> puts("Drive letters installed:");
>
> for(checkDrive = 1; checkDrive <= nDrives; checkDrive++)
> {
> unsigned int pdrive;
>
> /* in Borland, I used a command similar to if(_chdrive(checkDrive) == 0) */
> _dos_setdrive(checkDrive, &pdrive);
> _dos_getdrive(&pdrive);
> if(pdrive == checkDrive)
> printf("%c:, ", checkDrive + '@');
> }
I think this code is dangerous: it will crash under some DPMI servers
(e.g. QDPMI) if you try to switch to a floppy drive without a disk, or
with unformatted disk. The same happens with a CDROM drive that is empty
or has an audio disk inserted.
On machines with a single floppy drive, this has another nasty
side-effect: DOS/Windows pops a message asking you to "put a disk in
drive B and press any key". This is especially ugly on Windows, where
the message causes the system to switch into text mode, and, unless your
display is set to 256 colors or less, messes your color palette while at
that.
> Is there a more portable way to detect all drives on a system?
Use the library function `getmntent'. It will report all valid drives
that have media in them, one by one, and already implements all the
tricks required to avoid the above-mentioned gotchas. It also reports
additional info on the drives, which you might find useful; see the
library docs for details.
`getmntent' is portable to many Unix systems.
- Raw text -