{ LASTDRV3.PAS }

program LastDrv;
uses dos;

type 
    Dos20 = record
        numdrives : Byte;
        maxbytes : Word;
        first_diskbuff : Longint;
        nul : array [1..18] of Byte;
    end;

    Dos30 = record
        numblkdev : Byte;
        maxbytes : Word;
        first_diskbuff : Longint;
        currdir : Longint;
        lastdrive : Byte;
        stringarea : Longint;
        size_stringarea : Word;
        fcbtab : Longint;
        fcb_y : Word;
        nul : array [1..18] of Byte;
    end;

    Dos31 = record      { DOS 3.1 and higher }
        maxbytes : Word;
        diskbuff : Longint;
        currdir : Longint;
        fcb : Longint;
        numprotfcb : Word;
        numblkdev : Byte;
        lastdrive : Byte;
        nul : array [1..18] of Byte;
        numjoin : Word;
    end;

    ListOfLists = record
        shareretrycount : Word;
        shareretrydelay : Word;
        currdiskbuf : Longint;
        unreadcon : Word;
        mcb : Word;
        dpb : Longint;
        filetable : Longint;
        clock : Longint;
        con : Longint;
        case Word of
            20 : (dos20 : Dos20);
            30 : (dos30 : Dos30);
            31 : (dos31 : Dos31);
end;

var
    lastdrive : Word;

function GetLastDrive : Word;
var
    doslist : ^ListOfLists;
    r : registers;
    vers : Word;
begin
    { Get pointer to DOS List Of Lists }
    with r do begin
        ah := $52;
        es := 0; bx := 0;
        MsDos(r);
        if (es = 0) and (bx = 0) then begin
            GetLastDrive := 0;
            Exit;
        end;
        doslist := Ptr(es, bx - 12);
    end;
    { LASTDRIVE offset depends on DOS version }
    GetLastDrive := doslist^.dos31.lastdrive;
    vers := DosVersion;
    case Lo(vers) of 
        0 : GetLastDrive := 0; { DOS 1 }
        2 : GetLastDrive := doslist^.dos20.numdrives;
        3 : if Hi(vers) = 0 then 
               GetLastDrive := doslist^.dos30.lastdrive;
    end;
end;    

begin
    lastdrive := GetLastDrive;
    if lastdrive = 0 then 
        Halt(0);
    Writeln('LASTDRIVE=', Chr(Ord('A') - 1 + lastdrive));
    Halt(lastdrive);
end.
