home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / sync.zip / sync.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  3KB  |  96 lines

  1. /* 
  2.    This code has been released as public domain.
  3.    Use at your own risc !
  4.    
  5.    Radim Kolar
  6.    Fidonet 2:423/66.111
  7.  
  8.    Note: This program DON'T Notify PM (and WPS) about system reboot.
  9.          'Reboot' PM-Hooks don't works with this program. (I hope)
  10.  
  11.    Rev. history
  12.    
  13.    29.6.95 sync.c created 
  14.    2 .7.95 better error handling
  15.    6. 7.95 added -halt and -reboot command line options
  16.            help added
  17.    8. 7.95 added error code 1 and exit when parameter is wrong.
  18.            added count arguments check
  19.    5. 6.96 added -wait sec, option. Can be used with -reboot or -halt
  20. */
  21.  
  22. #ifdef DEBUG
  23. #define SHUTDOWNVALUE 1
  24. #else
  25. #define SHUTDOWNVALUE 0
  26. #endif
  27.  
  28. #define INCL_BASE
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <os2.h>
  33.  
  34. void Help()
  35. {
  36.  printf("\nsync [-halt | -reboot] [ -wait n]\n");
  37.  exit(1);
  38. }
  39.  
  40. int main(int argc, char *args[])
  41. {
  42.  int i;
  43.  APIRET rc;
  44.  int action=0; /* 0=sync, 1=halt , 2=reboot */
  45.  HFILE  hf=0;
  46.  ULONG dummy;
  47.  ULONG waittime=0; /* 0 sec wait */
  48.  
  49. /* if(argc>3) Help();*/
  50.  for(i=1;i<argc;i++)
  51.   if(!strcmp(args[i],"-halt")) action=1;
  52.   else if(!strcmp(args[i],"-reboot")) action=2;
  53.   else if(!strcmp(args[i],"-wait")) waittime=1000*atoi(args[(i++)+1]);
  54.   else { printf("\nUnknown switch %s",args[i]);Help();}
  55.  
  56.   rc = DosOpen("DOS$", &hf, &dummy, 0L, FILE_NORMAL, FILE_OPEN,
  57.        OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE |
  58.        OPEN_FLAGS_FAIL_ON_ERROR, NULL);
  59.  
  60.  switch(action)
  61.  {
  62.  case 0:  rc=DosShutdown(1);break;
  63.  default: 
  64.         if (waittime) printf("\nSystem will be halted/rebooted in %ld sec. Press CTRL-C to abort.\n",waittime/1000);
  65.         DosSleep(waittime);
  66. /*      if (_read_kbd(0,0,0)!=-1) { printf("\nAction Aborted by user request."); exit(2);}; */
  67.         printf("\nPlease wait, shutdown still in progress.\n"
  68.                  "DO NOT TURN POWER OFF NOW!\n"); 
  69.         rc=DosShutdown(SHUTDOWNVALUE); 
  70.         if(!rc) printf("\nShutdown has completed.\nIt is now safe to turn"
  71.          " off your computer, or restart system \nby pressing Ctrl+Alt+Del.\a");
  72.  }
  73.  if(rc==87) printf("\nYou must have at least OS/2 version 2.1 to run this"
  74.   " program.\n");
  75.  if(rc==274) printf("\nsync: Filesystem is allready shutdown"
  76.   " or shutdown is in progress.\n");
  77.  if(action==2)
  78.  {
  79.      if(hf)
  80.      {
  81.         printf("\n\nRebooting system in 10 secs...");
  82.         DosSleep(10000);
  83.         /* 32-Bit reboot */
  84.         DosDevIOCtl( hf, 0xd5, 0xab, NULL, 0, NULL, NULL, 0, NULL );
  85.  
  86.         /* old 16-bit reboot
  87.         DosDevIOCtl(NULL, NULL, 0xab, 0xd5, hf);
  88.         */
  89.         DosClose(hf);
  90.      }
  91.      else
  92.        printf ("\nDOS.SYS not installed. Can't Reboot.");
  93.  }
  94.  
  95.  return rc;
  96. }