REM LASTDRV2.BAS
REM $INCLUDE: 'QB.BI'

DEF FNHI (x) = x \ &H100
DEF FNLO (x) = x AND &HFF

FUNCTION DOSVERSION
    DIM Regs AS RegType
    Regs.ax = &H3000
    CALL INTERRUPT(&H21, Regs, Regs)
    DOSVERSION = Regs.ax
END FUNCTION

SUB DOSEXIT(errorlevel)
    CLOSE
    DIM Regs AS RegType
    Regs.ax = &H4C00 + errorlevel
    CALL INTERRUPT(&H21, Regs, Regs)
END SUB

REM based on DOS version number, find offset of LASTDRIVE
lastdrvofs = &H21
vers = DOSVERSION
IF FNLO(vers) < 3 THEN DOSEXIT(0)
IF (FNLO(vers) = 3) AND (FNHI(vers) = 0) THEN lastdrvofs = &H1B

REM get address of DOS List Of Lists
DIM Regs AS RegTypeX
Regs.ax = &H5200
Regs.es = 0
Regs.bx = 0
REM to use current value of DS, set to -1
Regs.ds = -1
CALL INTERRUPTX(&H21, Regs, Regs)
IF (Regs.es = 0) AND (Regs.bx = 0) THEN DOSEXIT(0)

REM peek at LASTDRIVE field within DOS List Of Lists
DEF SEG = Regs.es
lastdrv = PEEK(Regs.bx + lastdrvofs)
IF lastdrv = &HFF THEN DOSEXIT(0)

REM print LASTDRIVE letter, return LASTDRIVE number
PRINT "LASTDRIVE="; CHR$(ASC("A") - 1 + lastdrv)
CALL DOSEXIT(lastdrv)
END
