/* INDOS.C - Functions to manage DOS flags */

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

#define GET_INDOS       0x34
#define GET_CRIT_ERR    0x5D06

char far *indos_ptr=0;
char far *crit_err_ptr=0;

int   DosBusy(void);
void  InitInDos(void);

/*****
Function: Init InDos Pointers
Initialize pointers to InDos Flags
*****/
void InitInDos(void)
{ 
    union  REGS regs;
    struct SREGS segregs;

    regs.h.ah = GET_INDOS;   
    intdosx(&regs,&regs,&segregs);
    /* pointer to flag is returned in ES:BX */
    FP_SEG(indos_ptr) = segregs.es;
    FP_OFF(indos_ptr) = regs.x.bx;

    if (_osmajor < 3)  /* flag is one byte after InDos */
        crit_err_ptr = indos_ptr + 1;
    else if (_osmajor==3 && _osminor == 0) /* flag is one byte before */     
        crit_err_ptr = indos_ptr - 1;
    else
    {
        regs.x.ax = GET_CRIT_ERR;   
        intdosx(&regs,&regs,&segregs);
        /* pointer to flag is returned in DS:SI */
        FP_SEG(crit_err_ptr) = segregs.ds;
        FP_OFF(crit_err_ptr) = regs.x.si;
    }
}

/*****
Function: DosBusy
This function will non-zero if DOS is busy
*****/ 
int DosBusy(void)
{
    if (indos_ptr && crit_err_ptr)
        return (*crit_err_ptr || *indos_ptr);
    else
        return 0xFFFF;  /* return dos busy if flags are not set */
}

/*****
Function: Int28DosBusy
This function will return non-zero if the InDos flag is > 1 or
the critical error flag is non zero. To be used inside of an
INT 28 loop. Note that inside INT 28, InDOS == 1 is normal, and
indicates DOS is *not* busy; InDOS > 1 inside INT 28 means it is.
*****/
int Int28DosBusy(void)
{
    if (indos_ptr && crit_err_ptr)
      return (*crit_err_ptr || (*indos_ptr > 1));
    else
      return 0xFFFF;  /* return dos busy if flags are not set */
}   
