home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / shtdwn16.zip / shutdwn2.c < prev   
C/C++ Source or Header  |  1994-08-05  |  8KB  |  271 lines

  1. /* Shutdown for OS/2
  2.  
  3.   version    : 1.6
  4.   compiler   : Borland C++ v1.0 for OS/2
  5.  
  6.   created on : 29 March 1993
  7.                Santiago Crespo, 2:341/24@fidonet.org
  8.   modified   : 29 December 1993
  9.                Steven van.Loef, 2:512/45.16@fidonet.org
  10.                added option to shutdown after specified seconds
  11.                added option to shutdown on specified date and/or time
  12.   modified   : 5 August 1994
  13.                Steven van.Loef, 2:512/45.16@fidonet.org
  14.                added option to reboot after shutdown
  15. */
  16.  
  17. #define USAGE "Shutdown v1.6 for OS/2 2.x\n\n" \
  18. "Usage      : SHUTDOWN.EXE [[/axxxx] | [[/dyyyymmdd] [/thhmm]] [/b]]\n" \
  19. "             /a - shutdown after xxxx seconds\n" \
  20. "             /d - shutdown on YYYYMMDD (Year, Month, Day)\n" \
  21. "             /t - shutdown on HHMM (24 hour format)\n" \
  22. "             /b - reboot after shutdown has completed\n" \
  23. "             /? - show this help\n" \
  24. "             SHUTDOWN without options will shutdown the system immediately!!!\n\n"
  25.  
  26. #define INCL_DOSPROCESS
  27. #define INCL_DOSERRORS
  28. #define INCL_NOPMAPI
  29. #define INCL_DOSDEVICES
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <os2.h>
  35.  
  36. #define OPT_STRING      "a~d~t~?b"
  37. /* Meaning:
  38.  *
  39.  * -? and -b take no arguments.
  40.  * -a, -d and -t take mandatory arguments (I set 'optneed' to '~', below).
  41.  */
  42.  
  43. #define OPT_CHARS       "-/"
  44. /* Meaning:
  45.  *
  46.  * Options can begin with '-', or '/'.
  47.  */
  48.  
  49. /* Global variables for egetopt(): */
  50. extern int optneed;     /* character used for mandatory arguments */
  51. extern int optmaybe;    /* character used for optional arguments */
  52. extern int optchar;     /* character which begins a given argument */
  53. extern int optbad;      /* what egetopt() returns for a bad option */
  54. extern int opterrfd;    /* where egetopt() error messages go */
  55. extern char *optstart;  /* string which contains valid option start chars */
  56. extern int optind;      /* index of current argv[] */
  57. extern int optopt;      /* the actual option pointed to */
  58. extern int opterr;      /* set to 0 to suppress egetopt's error messages */
  59. extern char *optarg;    /* the argument of the option */
  60.  
  61. /* Functions */
  62. extern int egetopt(int, char **, char *);
  63. void sdWaitSec(ULONG);
  64. void sdWaitDayTime(DATETIME *);
  65. void Usage(void);
  66.  
  67. void main(int argc, char *argv[])
  68. {
  69.     DATETIME dtSdTime;
  70.     HFILE hf;
  71.     ULONG    seconds = 0,
  72.              sdate,
  73.              stime,
  74.              dummy;
  75.     USHORT   rc, rc2;
  76.     UCHAR    method = 0,
  77.              bReboot = FALSE;
  78.     int      ch;
  79.     char     months[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  80.                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  81.  
  82.     opterrfd = fileno(stderr);      /* errors to stderr */
  83.     opterr = 1;             /* set this to 1 to get egetopt's error msgs */
  84.     optbad = '!';           /* return '!' instead of '?' on error */
  85.     optneed = '~';          /* mandatory arg identifier (in OPT_STRING) */
  86.     optstart = OPT_CHARS;   /* characters that can start options */
  87.  
  88.     DosGetDateTime (&dtSdTime);
  89.     dtSdTime.hours = dtSdTime.minutes = 0;
  90.  
  91.     /* process command line arguments */
  92.     while ((ch = egetopt(argc, argv, OPT_STRING)) != EOF)
  93.     {
  94.         switch (ch)
  95.         {
  96.             case '?':
  97.                 Usage();
  98.                 break;
  99.  
  100.             case 'a':
  101.                 if (method > 1 || strlen(optarg) > 4)
  102.                     Usage();
  103.                 seconds = atol(optarg);
  104.                 method = 1; /* in x seconds */
  105.                 break;
  106.  
  107.             case 'd':
  108.                 if (seconds != 0 || strlen(optarg) != 8)
  109.                     Usage();
  110.                 sdate = atol(optarg);
  111.                 dtSdTime.year  = sdate / 10000;
  112.                 dtSdTime.month = (sdate - dtSdTime.year * 10000) / 100;
  113.                 dtSdTime.day   = (sdate - (dtSdTime.year * 10000) -
  114.                                  (dtSdTime.month * 100));
  115.                 if (method == 3)
  116.                     method = 4;   /* date & time */
  117.                 else
  118.                     method = 2;   /* date only */
  119.                 break;
  120.  
  121.             case 't':
  122.                 if (seconds != 0 || strlen(optarg) != 4)
  123.                     Usage();
  124.                 stime = atol(optarg);
  125.                 dtSdTime.hours   = stime / 100;
  126.                 dtSdTime.minutes = (stime - dtSdTime.hours * 100);
  127.                 if (method == 2)
  128.                     method = 4;   /* date & time */
  129.                 else
  130.                     method = 3;   /* time only */
  131.                 break;
  132.  
  133.             case 'b':
  134.                 bReboot = TRUE;
  135.                 break;
  136.  
  137.             case '!':
  138.                 Usage();
  139.                 break;
  140.  
  141.             default:
  142.                 Usage();
  143.                 break;
  144.         }
  145.     }
  146.  
  147.     fprintf(stderr, "Shutdown v1.6 for OS/2 2.x\n\n");
  148.     switch (method)
  149.     {
  150.         case 1:
  151.             fprintf(stderr, "Shutting down the system in %4d seconds", seconds);
  152.             break;
  153.  
  154.         case 2:
  155.         case 3:
  156.         case 4:
  157.             fprintf(stderr, "The system will shutdown on %02d %s %4d at " \
  158.                     "%02d:%02d\n",
  159.                     dtSdTime.day, months[dtSdTime.month - 1], dtSdTime.year,
  160.                     dtSdTime.hours, dtSdTime.minutes);
  161.             break;
  162.  
  163.         default:
  164.             fprintf(stderr, "The system will shutdown immediately...\n");
  165.     }
  166.  
  167.     /* Wait until shutdown time */
  168.     if (method != 0)
  169.     {
  170.         if (method == 1)
  171.             sdWaitSec(seconds);
  172.         else
  173.             sdWaitDayTime(&dtSdTime);
  174.     }
  175.  
  176.     if (bReboot)
  177.         rc2 = DosOpen("DOS$", &hf, &dummy, 0L, FILE_NORMAL, FILE_OPEN,
  178.                       OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE |
  179.                       OPEN_FLAGS_FAIL_ON_ERROR, NULL);
  180.  
  181.     /* Perform the shutdown */
  182.     rc = DosShutdown(0L);
  183.  
  184.     /* Display result of shutdown */
  185.     switch (rc)
  186.     {
  187.         case NO_ERROR:
  188.             fprintf(stderr, "\nThe system was shutdown succesfully, you can " \
  189.                             "now turn of the power");
  190.             break;
  191.  
  192.         case ERROR_ALREADY_SHUTDOWN:
  193.             fprintf(stderr, "\nThe system was already shutdown, therefor it " \
  194.                             "is safe to turn of the power now");
  195.             break;
  196.  
  197.         case ERROR_INVALID_PARAMETER:
  198.             fprintf(stderr, "\nInvalid paramater to DosShutdown, please " \
  199.                             "contact the author");
  200.             break;
  201.  
  202.         default:
  203.             fprintf(stderr, "\nUnknown return code [%lu], please contact " \
  204.                             "the author", rc);
  205.             break;
  206.     }
  207.  
  208.     if (bReboot)
  209.     {
  210.         if (!rc2)
  211.         {
  212.             DosDevIOCtl( hf, 0xd5, 0xab, NULL, 0, NULL, NULL, 0, NULL );
  213.             DosClose(hf);
  214.         }
  215.         else
  216.             fprintf(stderr, "\nCould not reboot, DOS.SYS not in CONFIG.SYS");
  217.     }
  218. }
  219.  
  220. void sdWaitSec(ULONG seconds)
  221. {
  222.     DATETIME dtTime;
  223.     ULONG    sdtime,
  224.              total,
  225.              diff;
  226.  
  227.     diff = sdtime = 0;
  228.  
  229.     /* enter loop until shutdown time */
  230.     while (diff < seconds)
  231.     {
  232.         DosGetDateTime (&dtTime);
  233.         total = (dtTime.hours) * 3600 +
  234.                 (dtTime.minutes * 60) +
  235.                 (dtTime.seconds);
  236.  
  237.         if (sdtime == 0)
  238.             sdtime = total;
  239.  
  240.         diff = (total - sdtime);
  241.         fprintf (stderr, "%4d seconds", (seconds - diff));
  242.         DosSleep(500);
  243.     }
  244.     fprintf(stderr, "\n");
  245. }
  246.  
  247. void sdWaitDayTime(DATETIME *dtSdTime)
  248. {
  249.     DATETIME dtTime;
  250.  
  251.     while (1)
  252.     {
  253.         DosGetDateTime (&dtTime);
  254.         if (dtTime.year == dtSdTime->year)
  255.             if (dtTime.month == dtSdTime->month)
  256.                 if (dtTime.day == dtSdTime->day)
  257.                     if (dtTime.hours == dtSdTime->hours)
  258.                         if (dtTime.minutes == dtSdTime->minutes)
  259.                             break;
  260.         DosSleep(500);
  261.     }
  262. }
  263.  
  264. /* Display usage information if wrong argument supplied or /? issued */
  265. void Usage(void)
  266. {
  267.     fprintf(stderr, USAGE);
  268.     exit(0);
  269. }
  270.  
  271.