delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/04/08:54:11

From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How to detect drives attached to a system
Date: Mon, 4 Oct 1999 10:42:56 +0200
Organization: NetVision Israel
Lines: 41
Message-ID: <Pine.SUN.3.91.991004103456.23739L-100000@is>
References: <7t993c$d8a$1 AT solomon DOT cs DOT rose-hulman DOT edu>
NNTP-Posting-Host: is.elta.co.il
Mime-Version: 1.0
X-Trace: news.netvision.net.il 939026503 1051 199.203.121.2 (4 Oct 1999 08:41:43 GMT)
X-Complaints-To: abuse AT netvision DOT net DOT il
NNTP-Posting-Date: 4 Oct 1999 08:41:43 GMT
X-Sender: eliz AT is
In-Reply-To: <7t993c$d8a$1@solomon.cs.rose-hulman.edu>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019