home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d497 / reboot / reboot.c < prev    next >
C/C++ Source or Header  |  1991-06-06  |  4KB  |  165 lines

  1. /*
  2.  *  Reboot.c - make an executable which calls exec's ColdReboot()
  3.  *
  4.  *  Public Domain
  5.  *
  6.  *  by Stefan Sticht
  7.  *
  8.  *  V1.00 initial release
  9.  *  V1.01 added Ctrl-C detect
  10.  *  V1.02 added PROMPTing and YES
  11.  */
  12.  
  13. #define VERSION "1.02"
  14.  
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <dos/dos.h>
  18. #include <clib/dos_protos.h>
  19. #include <pragmas/dos_pragmas.h>
  20. #include <clib/exec_protos.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <clib/utility_protos.h>
  23. #include <pragmas/utility_pragmas.h>
  24.  
  25. /*
  26.  *  our template for ReadArgs()
  27.  */
  28. #define TEMPLATE    "PROMPT/K,YES/K,TIMEOUT/K/N,QUIET/S"
  29. #define OPT_PROMPT  0
  30. #define OPT_YES     1
  31. #define OPT_TIMEOUT 2
  32. #define OPT_QUIET   3
  33. #define OPT_COUNT   4
  34.  
  35. /*
  36.  *  default prompt and answer
  37.  */
  38. #define PROMPT  "Do you really want to reboot (y/N)? "
  39. #define YES     "y"
  40.  
  41. static char prompt[] = PROMPT;
  42. static char yes[] = YES;
  43.  
  44. /*
  45.  *  this array will be filled by ReadArgs()
  46.  */
  47. long opts[] = {
  48.     (long)prompt,   /* OPT_PROMPT   */
  49.     (long)yes,      /* OPT_YES      */
  50.     0l,             /* OPT_TIMEOUT  */
  51.     0l              /* OPT_QUIET    */
  52.     };
  53.  
  54. /*
  55.  *  a string, which will be found by Version
  56.  */
  57. char versionstring[] ="\0$VER: Reboot V" VERSION;
  58.  
  59. /*
  60.  *  some SAS/C specularities, change for other compilers!
  61.  */
  62. #ifdef __SASC
  63. extern struct Library *DOSBase; /* Library base pointer from startup code */
  64. extern struct Library *SysBase; /* Library base pointer from startup code */
  65. void chkabort(void) {}          /* disable Ctrl-C detect */
  66. #endif
  67.  
  68. void _main(char *line)
  69. {
  70.     struct RDArgs *argsptr;
  71.     struct Library *UtilityBase;
  72.     unsigned long seconds = 0l;
  73.     char buffer[16];
  74.     char *prompt;
  75.     char *yes;
  76.     short quiet;
  77.     short rc = 0;
  78.  
  79.     /*
  80.      *  check for right kickstart! Anything under V37 will be refused!
  81.      */
  82.     if ((DOSBase->lib_Version < 37) || (SysBase->lib_Version < 37)) exit(30);
  83.  
  84.     /*
  85.      *  command line parsing by DOS
  86.      */
  87.     if (argsptr = ReadArgs(TEMPLATE, opts, NULL)) {
  88.  
  89.         /*
  90.          *  if the user didn't specify an argument, we'll get defaults
  91.          */
  92.         quiet   = opts[OPT_QUIET];
  93.         prompt  = (char *)opts[OPT_PROMPT];
  94.         yes     = (char *)opts[OPT_YES];
  95.  
  96.         /*
  97.          *  string in yes mustn't be longer than buffer!
  98.          */
  99.         if (strlen(yes) > (sizeof(buffer) - 1l)) {
  100.             if (!quiet) PutStr("String for YES too long!\n");
  101.             rc = 10;
  102.             }
  103.  
  104.         if (opts[OPT_TIMEOUT]) {
  105.  
  106.             if (*(long *)(opts[OPT_TIMEOUT]) < 0l) {
  107.                 if (!quiet) PutStr("Timeout must be positive!\n");
  108.                 rc = 6;
  109.                 }
  110.  
  111.             else seconds = *(long *)(opts[OPT_TIMEOUT]);
  112.  
  113.             }
  114.  
  115.         } /* if argsptr = ReadArgs() */
  116.  
  117.     else {
  118.         PrintFault(IoErr(), NULL);
  119.         rc = 20;
  120.         }
  121.  
  122.     /*
  123.      *  prompt the user
  124.      */
  125.     if (!rc && !quiet) {
  126.  
  127.         if (UtilityBase = OpenLibrary("utility.library", 37l)) {
  128.  
  129.             PutStr(prompt);
  130.             Flush(Output());
  131.             if (FGets(Input(), buffer, sizeof(buffer))) {
  132.                 if (Strnicmp(yes, buffer, strlen(yes))) rc = 5;
  133.                 }
  134.             else rc = 5;
  135.  
  136.             CloseLibrary(UtilityBase);
  137.             }
  138.         else {
  139.             PutStr("Can't open utility.library v37 or higher!\n");
  140.             rc = 15;
  141.             }
  142.  
  143.         }
  144.  
  145.     if (!rc) {
  146.  
  147.         if (seconds) {
  148.             if (!quiet)
  149.                 VPrintf("Rebooting in %ld seconds. Press Ctrl-C to abort!\n", &seconds);
  150.             Delay(seconds * 50l);
  151.             }
  152.  
  153.         if (SetSignal(0l, 0l) & SIGBREAKF_CTRL_C) {
  154.             if (!quiet) PutStr("***Break\n");
  155.             rc = 5;
  156.             }
  157.         else ColdReboot();
  158.  
  159.         }
  160.  
  161.     FreeArgs(argsptr);
  162.  
  163.     exit(rc);
  164. }
  165.