home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / reboot3.zip / reboot.c next >
C/C++ Source or Header  |  1994-12-18  |  3KB  |  107 lines

  1. #define INCL_DOS
  2. #define INCL_KBD
  3.  
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9.  
  10. /*
  11.  * Public domain from Mark Kimes
  12.  * icc /G4 /O+ /Gs+ /W3 /Kb /B"/RUNFROMVDM" /B"/STACK:16384" /Rn reboot.c
  13.  */
  14.  
  15. VOID ShowHelp (APIRET rc) {
  16.  
  17.   static RESULTCODES rt;
  18.   static CHAR        object[32],runme[CCHMAXPATH];
  19.  
  20.   sprintf(runme,"CMD.EXE /C HELP.CMD SYS%04u",rc);
  21.   runme[strlen(runme) + 1] = 0;
  22.   runme[7] = 0;
  23.   DosExecPgm(object,sizeof(object),EXEC_SYNC,(PVOID)runme,NULL,&rt,(PSZ)runme);
  24. }
  25.  
  26.  
  27. INT to_upper (INT key) {
  28.  
  29.   if(key >= 'a' && key <= 'z')
  30.     return ((key) + 'A' - 'a');
  31.   return key;
  32. }
  33.  
  34.  
  35. int main (int argc,char *argv[]) {
  36.  
  37.   HFILE  hf;
  38.   APIRET rc;
  39.   ULONG  action;
  40.   CHAR   key = 'N';
  41.   INT    x;
  42.   BOOL   clerr = FALSE;
  43.  
  44.   for(x = 0;x < argc;x++) {
  45.     switch(*argv[x]) {
  46.       case '/':
  47.       case '-':
  48.         switch(to_upper(argv[x][1])) {
  49.           case '?':
  50.             printf("\n  Usage:  REBOOT [/y[es]]\n"
  51.                    "\nReboots the system as if you'd pressed CTRL-ALT-DEL."
  52.                    "\n Hector wuz here.\n");
  53.             return 0;
  54.           case 'Y':
  55.             key = 'Y';
  56.             break;
  57.           default:
  58.             printf("\n **\07Unknown command line switch '%s'\n",argv[x]);
  59.             DosSleep(1000L);
  60.             clerr = TRUE;
  61.             break;
  62.         }
  63.         break;
  64.     }
  65.   }
  66.  
  67.   if(clerr)
  68.     key = 'N';
  69.  
  70.   rc = DosOpen("DOS$", &hf, &action, 0L, FILE_NORMAL, FILE_OPEN,
  71.                OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE |
  72.                OPEN_FLAGS_FAIL_ON_ERROR, 0L);
  73.   if(!rc){
  74.     if(key != 'Y') {  /* if /y not on command line */
  75.  
  76.       KBDKEYINFO kbd;
  77.  
  78.       while(!KbdCharIn(&kbd,IO_NOWAIT,0)) { /* flush keyboard */
  79.         if(!(kbd.fbStatus & 0x40))
  80.           break;
  81.       }
  82.       printf("\n\07Rudely reboot system? (y/N) ");
  83.       if(!KbdCharIn(&kbd,IO_WAIT,0)) {
  84.         if(kbd.fbStatus & 0x40)
  85.           key = to_upper(kbd.chChar);
  86.       }
  87.       printf("%s\n",(key == 'Y') ? "Y" : "N\n  **Aborted.");
  88.     }
  89.     if(key == 'Y') {  /* we got the green light */
  90.       rc = DosShutdown(0L);
  91.       if(!rc) {
  92.         rc = DosDevIOCtl(hf,0xd5,0xab,NULL,0,NULL,NULL,0,NULL);
  93.         if(rc)
  94.           printf("\n **\07DosDevIOCtl 0xd5/0xab failed, can't reboot.\n");
  95.       }
  96.       else
  97.         printf("\n **\07DosShutdown failed, won't reboot.\n");
  98.     }
  99.     DosClose(hf);
  100.   }
  101.   else
  102.     printf("\n **\07DOS.SYS not installed, can't reboot.\n");
  103.   if(rc)
  104.     ShowHelp(rc);
  105.   return rc;
  106. }
  107.