/* LASTDRV4.C -- uses undocumented DOS */

#include <stdlib.h>
#include <stdio.h>
#include <dos.h>

#ifndef MK_FP
#define MK_FP(seg,ofs) \
    ((void far *)(((unsigned long)(seg) << 16) | (ofs)))
#endif

main()
{
    union REGS r;
    struct SREGS s;
    char far *doslist;
    unsigned lastdrv_ofs;
    unsigned lastdrv;

    /* get offset for LASTDRIVE within DOS List of Lists */
    if (_osmajor < 2)
        return 0;
    else if (_osmajor == 2)
        lastdrv_ofs = 0x10;
    else if (_osmajor == 3 && _osminor == 0)
        lastdrv_ofs = 0x1b;
    else
        lastdrv_ofs = 0x21;

    /* Get DOS Lists of Lists */
    r.h.ah = 0x52;
    segread(&s);
    s.es = r.x.bx = 0;
    int86x(0x21, &r, &r, &s);
    /* make sure Function 52h is supported */
    if (! s.es && ! r.x.bx)
        return 0;
    doslist = MK_FP(s.es, r.x.bx);

    /* Get LASTDRIVE number */
    lastdrv = doslist[lastdrv_ofs];

    /* OS/2 DOS compatibility box sets LASTDRIVE to FFh */
    if (lastdrv == 0xFF)
        return 0;

    /* Print LASTDRIVE letter */
    fputs("LASTDRIVE=", stdout);
    putchar('A' - 1 + lastdrv);
    putchar('\n');

    /* return LASTDRIVE number to DOS */
    return lastdrv;
}
