/* BUFFERS.C -- value of BUFFERS= */
/* also see COUNTF.C to determine value of FILES= */

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

#pragma pack(1)

#ifdef __TURBOC__
#define ASM asm
#else
#define ASM _asm
#endif

typedef unsigned char BYTE;

typedef struct dskbuf {
    struct dskbuf far *next;
    BYTE drive;
    BYTE flags;
    } DSKBUF;
    
unsigned buffers(void)
{
    BYTE far *doslist;
    
    ASM mov ah, 52h
    ASM int 21h
    ASM mov doslist+2, es
    ASM mov doslist, bx
    
    if (_osmajor < 4)
    {
        DSKBUF far *diskbuff;
        unsigned buffers;

        /* pointer to first disk buffer in List of Lists */
        if ((_osmajor==2) || ((_osmajor==3) && (_osminor==0)))
            diskbuff = *((void far * far *) (doslist + 0x13));
        else 
            diskbuff = *((void far * far *) (doslist + 0x12));

        for (buffers=1; ; buffers++)
            if ((diskbuff = diskbuff->next) == -1L)
                break;
        return buffers;
    }
    else
        /* BUFFERS= value kept directly in List of Lists */
        return *(doslist+0x3F);
}

main()
{ 
    printf("BUFFERS=%d\n", buffers());
}
