home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CDD.ZIP / CDD.C next >
Text File  |  1991-02-14  |  4KB  |  135 lines

  1. #define    INCL_BASE
  2. #include   <os2.h>
  3.  
  4. #include   <string.h>
  5.  
  6. /* forward references */
  7. void far start(void);
  8. void _saveregs near realstart(int i);
  9. void initproc(void);
  10.  
  11. /* structure of an INIT device packet */
  12. typedef struct
  13. {
  14.    char    PktLen;             /* Lenght in bytes of packet            */
  15.    char    PktUnit;            /* Subunit number of block device       */
  16.    char    PktCmd;             /* Command code                         */
  17.    int     PktStatus;          /* Status word                          */
  18.    long    PktDOSLink;         /* Reserved                             */
  19.    long    PktDevLink;         /* Device multiple-request link         */
  20.    char    PktData[1];         /* Data pertaining to specific packet   */
  21.    int     codesize;           /* code size on INIT return             */
  22.    int     datasize;           /* data size on INIT return             */
  23. } PKT;
  24.  
  25. /* definitions of device driver return status */
  26. #define done   0x0100          /* Word representing the code for DONE  */
  27. #define error  0x8000          /* error return                         */
  28. #define general_failure 0xc    /* aaargh!                              */
  29.  
  30. /* structure of device header */
  31. typedef struct
  32. {
  33.    PSZ  ptr_to_next;           /* pointer to next device driver        */
  34.                                /* set to -1 OS/2 will fill it in       */
  35.    int  attributes;            /* attributes of device driver          */
  36.    PFN  strategy;              /* pointer to strategy routine          */
  37.                                /* + the ignored SEG part of strategy!  */
  38.    char name[8];               /* name of the device                   */
  39.    int rsvd[4];                /* used by OS/2                         */
  40. } DEVHDR;
  41.  
  42. /* definitions of some of the bits in the attribute word */
  43. #define devlev_1       0x0080  /* Bits 7-9 OS/2 1.x                    */
  44. #define dev_char_dev   0x8000  /* Bit 15: Device is a character device */
  45. #define dev_open       0x0800  /* Bit 11: Accepts Open/Close/Removable */
  46.  
  47. /* define this device */
  48. static DEVHDR devhdr =
  49. {
  50.    (PSZ) -1L,
  51.    devlev_1 | dev_char_dev | dev_open,
  52.    (PFN) start,
  53.    "CDD$    "
  54. };
  55.  
  56. /* definitions of the command values used */
  57. #define INIT   0
  58.  
  59. /* define _acrtused to prevent the C runtime startup code being linked in */
  60. int _acrtused = 0;
  61.  
  62. /* device driver entry point */
  63. /* ES:BX points to the device driver request packet */
  64. void far start(void)
  65. {
  66.    /* call the real entry point */
  67.    realstart(0);
  68. }
  69.  
  70. /* the 'real' device driver code - work around for the C5.1 bug */
  71. void _saveregs near realstart(int dummy)
  72. {
  73.    PKT far  *pkt;
  74.    int      *regptr;
  75.  
  76.    #define ES -11      /* offset for ES in saved registers */
  77.    #define BX -5       /* offset for BX in saved registers */
  78.  
  79.    regptr = &dummy;    /* get address of top of saved registers */
  80.  
  81.    pkt = MAKEP( regptr[ES], regptr[BX]);
  82.  
  83.    /* if not init command, return failure! */
  84.    if (pkt->PktCmd != INIT)
  85.    {
  86.        pkt->PktStatus = done + error + general_failure;
  87.        return;
  88.    }
  89.  
  90.    /* perform the initialisation function */
  91.    initproc();
  92.  
  93.    /* save code and data size to initialise as a device driver */
  94.    pkt->codesize  = (int)initproc;
  95.    pkt->datasize  = sizeof(DEVHDR);
  96.    pkt->PktStatus = done;
  97.  
  98. } /* realstart */
  99.  
  100. void initproc(void)
  101. {
  102.    static  KBDKEYINFO kbci;
  103.    RESULTCODES        res;
  104.    int                i;
  105.  
  106.    /* initial read to kick things off - seems needed to wake up KBD subsystem */
  107.    KbdPeek(&kbci, 0);
  108.  
  109.    /* tell the world we're ready */
  110.    VioWrtTTY("Press ESC for command prompt...", 31, 0);
  111.  
  112.    /* wait up to 5 seconds for a keypress then carry on */
  113.    for ( i=0; i<10; i++)
  114.    {
  115.        DosSleep(500L);
  116.  
  117.        /* drop out on key press */
  118.        if ((KbdCharIn(&kbci, IO_NOWAIT, 0) == 0) && (kbci.fbStatus & 0x40))
  119.            break;
  120.    } /* for */
  121.  
  122.    /* check for ESC, which kicks off CMD.EXE */
  123.    if (kbci.chChar == '\033')
  124.    {
  125.        DosExecPgm(NULL,
  126.                   0,
  127.                   EXEC_SYNC,
  128.                   "cmd\0",                 /* no arguments            */
  129.                   NULL,                    /* inherit the environment */
  130.                   &res,                    /* resultcode              */
  131.                   "C:\\os2\\cmd.exe");     /* program name            */
  132.    }
  133.  
  134. } /* initproc */
  135.