/***********************************************
 * Isemu.c
 * Written: 03/14/95 by Kang-Jin Lee
 * Modified: 05/31/95 by Scott Buchholz
 *  - Detects and shows Dosemu major version number
 *
 * Modified 04/03/95
 *  - works only with Dosemu 0.53.58+
 *  - Detects and shows Dosemu version, subversion
 *    and patchlevel
 *  - returns errorlevel = VERSION * 100 + SUBLEVEL
 *    Dosemu-1.2.4 returns errorlevel 102
 ***********************************************/

#include <dos.h>    /* geninterrupt */
#include <stdio.h>  /* printf       */
#include <string.h> /* _fstrncmp    */

#define DOS_HELPER_INT           0xE6
#define DOS_HELPER_DOSEMU_CHECK  0x00

void (far * dosemu_id_proc)(void) = (void (far *)(void))MK_FP(0xfc00, 0x0000);

/* returns non-zero version number if DOSEMU is loaded */
int main(void)
{
    unsigned int version, sublevel, patchlevel;
    char far * id_string;

    version  = 0;
    sublevel = 0;
    patchlevel = 0;

    /* Check for version 0.61+ */
    _AH = 0xC0;
    geninterrupt(0x15);
    id_string = (char far *)MK_FP(_ES, _BX+10);
    if (_fstrncmp(id_string, "DOSEMU ", 7) == 0) {
         dosemu_id_proc();
         if (_AX == 0xaa55) {
              patchlevel = _CX;
              version = _BX / 0x100;
              sublevel = _BX % 0x100;
         }
    }

    /* Sanity check -- ensure that INT 0xE6 != NULL before calling it */
    if (!version && !sublevel && !patchlevel) {
         _AH = 0x35;
         _AL = DOS_HELPER_INT;
         geninterrupt(0x21);
         if (_ES != 0 || _BX != 0) {
              _AL = DOS_HELPER_DOSEMU_CHECK;
              geninterrupt(DOS_HELPER_INT);
              if (_AX == 0xaa55) {
                   patchlevel = _CX;
                   version = _BX / 0x100;
                   sublevel = _BX % 0x100;
              }
         }
    }

    if (version || sublevel || patchlevel) {
         printf("Detected: Dosemu %d.%d.%d\n", version, sublevel, patchlevel);
         return (version * 100 + sublevel);
    }
    else {
         printf("Dosemu not detected\n");
         return (0);
    }
}

