{***********************************************************************}
{									}
{ __IsDrvFil  Return the status of a disk drive 			}
{									}
{ function  __IsDrvFil(Drive : char; var ErrorCode : word) : boolean;	}
{									}
{   Drive     The drive to check.  Either an upper or lower case letter }
{	      may be used.						}
{   ErrorCode The returned status code. 				}
{	      (0) Drive is available.					}
{	      (1) Unknown drive; the specified drive does not exist.	}
{	      (2) Drive not ready; either the drive door is open or no	}
{		  disk is mounted.					}
{	      (3) General fault; the drive is unavailable probably due	}
{		  to a hardware error or an unformatted disk(ette).	}
{									}
{ Description This function returns TRUE if the specified drive is	}
{	      available for use, and FALSE otherwise.  If the drive is	}
{	      not ready, a diagnostic error is returned.		}
{									}
{	      On some systems it is possible to have logical drives	}
{	      that are not attached to any physical device.  Moreover,	}
{	      a drive need not be available for use even if it is	}
{	      present.	__IsDrvFil makes it possible for you to ensure	}
{	      that a drive is ready before performing any disk I/O.	}
{									}
{	      __IsDrvFil determines the availability of the drive by	}
{	      performing an absolute read of logical sector 0		}
{	      (interrupt $25).	If it is able to read the disk, the	}
{	      drive is ready, but otherwise it returns immediately with }
{	      an error status code.  __IsDrvFil does not generate a DOS }
{	      critical error if the drive is not ready. 		}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function  __IsDrvFil(Drive : char; var ErrorCode : word) : boolean;

  var
    DriveNo   : word;
    LastDrive : word;
    Reg       : registers;

  begin {__IsDrvFil}

    { First determine the last drive number.  If the specified drive is }
    { greater than this value, set the error code to 1 and return.	}

    with Reg do
      begin
	AH := $19;		       { Return current drive		}
	Intr(_DOS,Reg);

	AH := $0E;		       { Select the current drive, the	}
	DL := AL;		       { number of drives is returned	}
	Intr(_DOS,Reg); 	       { in AL. 			}
	Dec(AL);		       { Last drive is one less.	}
	LastDrive := AL
      end;

    DriveNo := word(UpCase(Drive)) - word('A');
    if (DriveNo > LastDrive) then
       ErrorCode := 1
    else
       begin

	 { To see if the drive is ready, read sector zero.  DOS returns }
	 { immediately even if the drive is unavailable.		}

	 RdSector(DriveNo,ErrorCode);
	 if (ErrorCode <> 0) then
	    if (ErrorCode = $8002) then
	       ErrorCode := 2	       { Drive is not ready.		}
	    else if (ErrorCode = DriveNo) then
	       ErrorCode := 1	       { Unknown drive - not installed	}
	    else if (ErrorCode >= 3) then
	       ErrorCode := 3;	       { Disk is probably unformatted,	}
       end;			       { or hardware error.		}

    __IsDrvFil := (ErrorCode = 0)

  end;	{__IsDrvFil}
