/* LASTDRV5.C */

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

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

#pragma pack(1)

#define LISTOFLISTS_DECR        12

typedef struct {
    unsigned shareretrycount;
    unsigned shareretrydelay;
    void far *currdiskbuff;
    void near *unreadcon;
    unsigned mcb;
    void far *dpb;
    void far *filetable;
    void far *clock;
    void far *con;
    union {
        struct {
            unsigned char numdrive;
            unsigned maxbytes;
            void far *first_diskbuff;
            unsigned char nul[18];
            } dos2;
        struct {
            unsigned char numblkdev;
            unsigned maxbytes;
            void far *first_diskbuff;
            void far *currdir;
            unsigned char lastdrive;
            void far *stringarea;
            unsigned size_stringarea;
            void far *fcbtab;
            unsigned fcb_y;
            unsigned char nul[18];
            } dos30;
        struct {
            unsigned maxbytes;
            void far *diskbuff;
            void far *currdir;
            void far *fcb;
            unsigned numprotfcb;
            unsigned char numblkdev;
            unsigned char lastdrive;
            unsigned char nul[18];
            unsigned numjoin;
            } dos31;    /* and higher */
        } vers;
    } ListOfLists; 

main()
{
    union REGS r;
    struct SREGS s;
    ListOfLists far *doslist;
    unsigned lastdrive;

    /* No List Of Lists in DOS 1.x */
    if (_osmajor < 2)
        return 0;

    /* Get DOS List Of Lists */
    r.h.ah = 0x52;
    segread(&s);
    s.es = r.x.bx = 0;
    intdosx(&r, &r, &s);
    if (! s.es && ! r.x.bx)
        return 0;
    doslist = MK_FP(s.es, r.x.bx - LISTOFLISTS_DECR);

    /* Get LASTDRIVE value, depending on DOS version */
    if (_osmajor == 3 && _osminor == 0)
        lastdrive = doslist->vers.dos30.lastdrive;
    else if (_osmajor == 2)
        lastdrive = doslist->vers.dos2.numdrive;
    else
        lastdrive = doslist->vers.dos31.lastdrive;

    /* print LASTDRIVE letter, return LASTDRIVE number */
    printf("LASTDRIVE=%c\n", 'A' - 1 + lastdrive);
    return lastdrive;
}
