3.3 Logical Drives

MS-DOS creates one or more logical drives that map to the physical drives of a computer. Programs access logical drives by using a single set of MS-DOS functions, regardless of the type of hardware used by the physical drives.

A computer can have up to 26 logical drives. MS-DOS assigns each drive a unique number, sequentially from 1 through 26 (or from 0 through 25 for the Interrupt 21h functions Set Default Drive and Get Default Drive). The drive numbers correspond to the drive letters used in paths: drive 1 corresponds to drive A, drive 2 to drive B, and so on.

Drive 0 corresponds to the default drive—that is, the drive MS-DOS uses whenever a program supplies a path that does not explicitly specify a drive. When MS-DOS first starts, the default drive is the same as the drive from which the system files were loaded (the startup drive). A program can determine the default drive by using Get Default Drive (Interrupt 21h Function 19h) to obtain the drive number. A program can set the default drive by using Set Default Drive (Interrupt 21h Function 0Eh). A program can determine the startup drive by using Get Startup Drive (Interrupt 21h Function 3305h). On a ROM-based version of MS-DOS, there may be no startup drive; in this case, Get Startup Drive returns the number of the drive containing the CONFIG.SYS file.

Set Default Drive also returns the number of logical drives available. Since few computers have a full set of 26 drives, programs that present a list of available drives to the user must determine which drives are valid. If the CONFIG.SYS file contains a lastdrive command, Set Default Drive returns either the number of logical drives for the computer or the number of drives specified by lastdrive, whichever is larger. The lastdrive command is typically used to prepare extra drive numbers for use with network connections or commands such as subst. The extra drive numbers are not valid until a connection to a physical drive is established.

A program can check a logical drive to determine whether it has a corresponding physical drive by using Is Drive Remote (Interrupt 21h Function 4409h). If the logical drive is valid, Is Drive Remote clears the carry flag. Otherwise, the function sets the carry flag and returns 000Fh (ERROR_INVALID_DRIVE). For valid drives, Is Drive Remote also returns the device-attribute value and sets bit 12 if the drive is remote (for example, if it represents a network connection) or is a nonstandard file system (for example, CD-ROM).

Although a program may have determined that a drive is valid, the file system associated with the drive may still be inaccessible. For example, drives with removable media may have an open disk-drive door or no tape mounted on a tape drive. If a program attempts to access a drive under these or similar conditions, the system may prompt the user with an “Abort, Retry, or Fail?” message; if the user selects Abort or Fail, the program terminates immediately. If it is important to prevent the user from terminating the program at this point, the program may need to replace Critical-Error Handler (Interrupt 24h) with a customized handler that receives control whenever drive errors, such as an open drive door, occur. For more information about Critical-Error Handler, see Chapter 7, “Interrupts.”

MS-DOS usually reserves the first two logical drives for floppy disk drives. On computers that have only one floppy disk drive, the second logical drive is often treated as an alias for the first. In this case, Is Drive Remote specifies the first and second logical drives as valid drives, even though they share the same physical drive. A program can determine whether two or more logical drives share a physical drive by using Get Logical Drive Map (Interrupt 21h Function 440Eh). This function clears the carry flag and returns a nonzero drive number in the AL register if the drive has aliases. This drive number specifies which logical drive is currently being used to access the physical drive. If a program attempts to access the physical drive by using another logical drive, the system prompts the user with an “Insert diskette for drive . . . ” message. A program can avoid this problem by first using Set Logical Drive Map (Interrupt 21h Function 440Fh) to change the logical drive that is to be used to access the physical drive.

If the file system is accessible, a program can determine how much space is available in the file system by using Get Disk Free Space (Interrupt 21h Function 36h). The function returns the total number of clusters in the file system and the number of available clusters. (A cluster is the smallest amount of space that MS-DOS will allocate for a file or directory.) Get Disk Free Space also returns the number of bytes per sector and the number of sectors per cluster, so the program can compute the total number of bytes currently available in the file system. A program can also use Get Disk Free Space to determine whether a logical drive has a corresponding physical drive. If there is no corresponding physical drive, the function returns 0FFFFh.