home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / cmdbt10.zip / CMDBOOT.C next >
C/C++ Source or Header  |  1996-04-15  |  3KB  |  109 lines

  1. /*
  2.  *  CMD boot v1.0. Run a shell or some other program if a key is pressed within
  3.  *    x seconds. Good to put optional .EXEs in your CONFIG.SYS
  4.  *
  5.  *  (Optional CMD.EXE before loading the Presentation Manager shell, for
  6.  *    example...)
  7.  *
  8.  *  Copyright (c) Lighting and Sound Technologies, 1996.
  9.  *    Written by Ryan C. Gordon.
  10.  */
  11.  
  12. /*
  13.  * USAGE : cmdboot.exe [timetowait] [progtorun.exe] [arguments...]
  14.  *
  15.  * [timetowait] is optional # of seconds to wait before running.
  16.  *               Defaults to 5. You must specify this option if you want to
  17.  *               specify...
  18.  * [progtorun.exe] Optional executable to run. Defaults to c:\os2\cmd.exe
  19.  *                 you must specify this option if you want to specify...
  20.  * [arguments...] variable number of arguments to pass to [progtorun.exe]
  21.  */
  22.  
  23. #define INCL_DOS
  24. #define INCL_KBD
  25. #include <os2.h>
  26. #include <stdio.h>
  27. #include <process.h>
  28.  
  29. #define DEFAULT_TIMEOUT  5
  30. static UCHAR DEFAULT_SHELL[] = "c:\\os2\\cmd.exe";
  31.  
  32. static UCHAR MSG_STARTUP[] =
  33.                     "CMD boot v1.0\r\n"
  34.                     "  Copyright (c) Lighting and Sound Technologies, 1996\r\n"
  35.                     "  Written by Ryan C. Gordon.\r\n\r\n";
  36.  
  37.  
  38.  
  39. int main(int argc, char *argv[], char *envp[])
  40. {
  41.     ULONG ulBytesWritten;   /* bytes written. Dummy storage variable. */
  42.     UCHAR ucShell[255];        /* Program to load.                       */
  43.     ULONG ulTimeout;          /* Time to wait before continuing...      */
  44.     ULONG ulLooper;            /* FOR...NEXT control variable.           */
  45.     BOOL  bGetOut = FALSE;  /* Secondary FOR...NEXT control.           */
  46.     char  *pcArgs[argc];    /* for passing args to child.               */
  47.     HKBD  hKbd;                /* Handle to the keyboard.                   */
  48.     KBDKEYINFO kki;            /* For calls to KbdCharIn()...               */
  49.     ULONG ulStartTime;        /* For figuring if key has been pressed.  */
  50.  
  51.     printf("%s", MSG_STARTUP);
  52.  
  53.     if (argc >= 3)
  54.         strcpy(ucShell, argv[2]);
  55.     else
  56.         strcpy(ucShell, DEFAULT_SHELL);
  57.  
  58.     if (argc >= 2)
  59.         ulTimeout = atoi(argv[1]);
  60.     else
  61.         ulTimeout = DEFAULT_TIMEOUT;
  62.  
  63.     pcArgs[0] = ucShell;
  64.  
  65.     if (argc > 3)
  66.     {
  67.         for (ulLooper = 3; ulLooper <= argc; ulLooper++)
  68.             pcArgs[ulLooper - 2] = argv[ulLooper];
  69.     } /* if */
  70.  
  71.     else
  72.         pcArgs[1] = NULL;
  73.  
  74.     KbdOpen(&hKbd);
  75.     KbdGetFocus(IO_WAIT, hKbd);
  76.     KbdCharIn(&kki, IO_NOWAIT, hKbd);  /* get original kki.time value. */
  77.     ulStartTime = kki.time;
  78.  
  79.     printf("Will run %s if key is pressed within %d seconds...\n",
  80.             ucShell, ulTimeout);
  81.  
  82.     for (ulLooper = ulTimeout; ((ulLooper > 0) && (!bGetOut)); ulLooper--)
  83.     {
  84.         printf("%d...", ulLooper);
  85.         DosSleep(1000);    /* Wait for 1 second...*/
  86.  
  87.         KbdCharIn(&kki, IO_NOWAIT, hKbd);
  88.         if (kki.time != ulStartTime)
  89.         {
  90.             printf("\nLoading...\n\n");        
  91.             KbdFreeFocus(hKbd);
  92.             KbdClose(hKbd);
  93.             spawnve(P_WAIT | P_DEFAULT, ucShell, pcArgs, envp);
  94.             bGetOut = TRUE;
  95.         } /* if */
  96.     } /* for */
  97.  
  98.     if (!bGetOut)
  99.     {
  100.         printf("\nTimed out; no program will be loaded.\n");
  101.         KbdFreeFocus(hKbd);
  102.         KbdClose(hKbd);
  103.     } /* if */
  104.  
  105.     return(0);
  106.  
  107. } /* main */
  108.  
  109.