home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / AT.C < prev    next >
C/C++ Source or Header  |  1992-08-13  |  6KB  |  278 lines

  1. /*
  2. * AT.C - Start a program at a specified time.
  3. * UNTIL - Suspend execution until a specified time.
  4. *        
  5. *
  6. * PROGRAMMER:        Martti Ylikoski
  7. * CREATED:        8.3.1992
  8. */
  9. static char *VERSION = "Version  1.0. Copyright(c) Martti Ylikoski, 1992. " ;
  10. /*
  11. */
  12. #define INCL_DOS
  13. #include <os2.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #include <param.h>
  19. #include <paramstd.h>
  20. #include "dirscan.h"
  21. /* prototypes */
  22. int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds) ;
  23. void TimeWaitUntil(int hours, int minutes, int seconds) ;
  24. int ParamSetFile(int argc, char *argv[], int index );
  25.  
  26. //#define AT
  27. //#define UNTIL
  28.  
  29. extern unsigned long pflags ;
  30.  
  31. ParamEntry pentry[12] = {
  32.      "NOD", &ParamSetNoDefault, 0,
  33.      "P", &ParamSetPause, 0,
  34. #ifdef AT
  35.      "F", &ParamSetFile, 2,
  36. #endif
  37.      "V", &ParamSetVerbose, 1,
  38.      "R", &ParamSetReport, 0,
  39.      "S", &ParamSetSubDirs, 0,
  40.      "?", &ParamSetHelp, 1,
  41.      "H", &ParamSetHelp, 1,
  42.      "TEST", &ParamSetTest, 0,
  43.      "Y", &ParamSetYes, 0,
  44.      "N", &ParamSetTest, 0,
  45.      "\0", NULL, 0
  46. } ;
  47.  
  48. ParamBlock params = {
  49.     "/-",   IGNORECASE | PRIORITY | NOTRIGGERSALLOWED ,
  50.     pentry
  51. } ;
  52.  
  53. static char *progname, *fname ;
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. DATETIME dt ;
  58. UCHAR hours, minutes, seconds ;
  59. FILE *fp ;
  60. char fbuff[125], *t1, *t2 ;
  61.  
  62.    progname = argv[0] ; 
  63.  
  64.    ParamHandle(¶ms, &argc, argv) ;
  65.  
  66. #ifdef AT
  67.    if (pflags & PA_HELP)
  68.    {
  69.       printf("%s - start a program at a specified time.\n", progname) ;
  70.       puts(VERSION) ;
  71.       puts("Usage: at  time \"program with parameters\" | [/? | /H]") ;
  72. #endif
  73. #ifdef UNTIL
  74.    if (pflags & PA_HELP)
  75.    {
  76.       printf("%s - suspend execution until a specified time.\n", progname) ;
  77.       puts(VERSION) ;
  78.       puts("Usage: until time | [/? | /H]") ;
  79. #endif
  80.       puts("Where,") ;
  81.       puts("     /H,/? = Help") ;
  82.       puts(" TIME FORMAT:") ;
  83.       puts(" \"hh:mm:ss\" or +\"hh:mm:ss\"") ;
  84. //      puts(" : is the time separator character used in your country.") ;
  85.       puts("+ before the time means that the value is added to the current time") ;
  86. #ifdef AT
  87.       puts("E.g. at +00:00:10 dir, means that after 10 seconds the dir-command is run") ;
  88. #endif
  89. #ifdef UNTIL
  90.       puts("E.g. until 10:00 wait until the time is ten o'clock.") ;
  91. #endif
  92.       return( 0 ) ;
  93.    }
  94.  
  95. #ifdef UNTIL
  96.    if (argc < 2)
  97.    {
  98.       fprintf(stderr, "Missing time...\nExit\n") ;
  99.       return( 1 ) ;
  100.    }
  101. #endif
  102. #ifdef AT
  103.    if (pflags & PA_FILE || argc == 1)
  104.    {
  105.       if (argc == 1)
  106.       {
  107.          printf("%s: no command line parameters.\n", progname);
  108.          puts("Reading default at.dat file for commands.") ;         
  109.          fname = "at.dat" ; /* default for at-file */
  110.       }
  111.          
  112.       if ((fp = fopen (fname, "r")) == NULL)
  113.       {
  114.          printf("%s : error opening at-file %s.\nExit\n", progname, fname) ;
  115.          return( 1 ) ; 
  116.       }
  117.  
  118.       while (fgets(fbuff, sizeof(fbuff), fp ) != NULL)
  119.       {
  120.          if (strlen(fbuff) == 0 || fbuff[0] == '\n')
  121.             continue ;
  122.             
  123.          if (fbuff[0] == '#' || fbuff[0] == ';') /* handle comment lines */
  124.             continue ;
  125.  
  126.          if (strnicmp(fbuff, "CONTINUE",8) == 0)
  127.          {
  128.             rewind(fp) ;
  129.             continue ;
  130.          }
  131.  
  132.          if (strnicmp(fbuff, "BREAK",5) == 0 || strnicmp(fbuff, "QUIT",4) == 0)
  133.          {
  134.             fclose(fp) ;
  135.             return( 0 )  ;
  136.          }
  137.          t1 = strtok(fbuff, "     ") ;
  138.          t2 = t1 + strlen(t1) +1;
  139.  
  140.          StrExpandTime(t1, ":", &hours, &minutes, &seconds) ;         
  141.          TimeWaitUntil(hours, minutes, seconds) ;
  142.          if (strlen(t2) > 0)
  143.             system(t2) ;
  144.       }
  145.  
  146.       fclose (fp) ;
  147.    } 
  148.    else
  149. #endif
  150.    {
  151. #ifdef AT
  152.       if (argc < 3)
  153.       {
  154.          fprintf(stderr, "Missing time or program...\nExit\n") ;
  155.          return( 1 ) ;
  156.       }
  157. #endif
  158.       StrExpandTime(argv[1], ":", &hours, &minutes, &seconds) ;
  159.       TimeWaitUntil(hours, minutes, seconds) ;
  160. #ifdef AT
  161.       system(argv[2]) ;
  162. #endif
  163.    }
  164.  
  165.  
  166.    return( 0 ) ;
  167. }
  168.  
  169. void TimeWaitUntil(int hours, int minutes, int seconds)
  170. {
  171. DATETIME dt ;
  172. LONG sleepval ;
  173. USHORT tomorrow = FALSE ;
  174.  
  175.  
  176.    DosGetDateTime(&dt) ;
  177.    
  178.    if (dt.hours > hours)  /* schedule for tomorrow */
  179.       tomorrow = TRUE ;
  180.  
  181.    do
  182.    {
  183.       DosGetDateTime(&dt) ;
  184.  
  185.       if(dt.hours == 0)
  186.          tomorrow = FALSE ;
  187.          
  188.       sleepval = ((hours - dt.hours)*3600 + (minutes-dt.minutes)*60 +
  189.                  seconds-dt.seconds)/2 ;
  190.  
  191.       if (sleepval < 0 && tomorrow == FALSE)
  192.          break ;
  193.  
  194.       if (sleepval == 0)
  195.         DosSleep(320L) ;
  196.       else
  197.          DosSleep(sleepval*1000L) ;
  198.  
  199.    } while ( hours > dt.hours ||
  200.      (hours ==dt.hours && minutes > dt.minutes) ||
  201.      (hours ==dt.hours  && minutes == dt.minutes && seconds > dt.seconds) ||
  202.      tomorrow == TRUE ) ;
  203. }
  204.  
  205. int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds)
  206. {
  207. int add = FALSE ;
  208. DATETIME dt ;
  209. char *tmp ;
  210.    /* set default values for time */
  211.    DosGetDateTime(&dt) ;
  212.    *hours = dt.hours ;
  213.    *minutes = dt.minutes ;
  214.    *seconds = dt.seconds ;
  215.  
  216.    if (str[0] == '+')
  217.       add = TRUE ;
  218.  
  219.    if ( (tmp=strtok(str, tsepa)) == NULL)
  220.       return( 1 ) ;
  221.  
  222.     if (add==TRUE)
  223.        *hours += atoi(tmp) ;
  224.     else
  225.        *hours=atoi(tmp) ;
  226.  
  227.    if (*hours >24)
  228.    {
  229.       *hours -= 24 ;
  230.    }
  231.    
  232.    /* new default values */
  233.    if (add == FALSE)
  234.    {
  235.       *minutes = '0' ;
  236.       *seconds = '0' ;
  237.    }
  238.  
  239.    if ( (tmp=strtok(NULL, tsepa)) == NULL)
  240.       return( 0 ) ;
  241.  
  242.     if (add==TRUE)
  243.        *minutes += atoi(tmp) ;
  244.     else
  245.        *minutes = atoi(tmp) ;
  246.    
  247.    if (*minutes >60)
  248.    {
  249.       *minutes -= 60 ;
  250.       *hours += 1 ;
  251.    }
  252.  
  253.    if ( (tmp=strtok(NULL, tsepa)) == NULL)
  254.       return( 0 ) ;
  255.  
  256.     if (add==TRUE)
  257.        *seconds += atoi(tmp) ;
  258.     else
  259.        *seconds = atoi(tmp) ;
  260.  
  261.    if (*seconds >60)
  262.    {
  263.       *seconds -=60 ;
  264.       *minutes += 1 ;
  265.    }
  266.    
  267.    return( 0 ) ;
  268. }
  269.  
  270.  
  271. int ParamSetFile(int argc, char *argv[], int index )
  272. {
  273.    pflags |= PA_FILE ;
  274.    fname = argv[index+1] ;
  275.    return( 2 ) ;
  276. }
  277.  
  278.