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 >
Wrap
C/C++ Source or Header
|
1992-08-13
|
6KB
|
278 lines
/*
* AT.C - Start a program at a specified time.
* UNTIL - Suspend execution until a specified time.
*
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 8.3.1992
*/
static char *VERSION = "Version 1.0. Copyright(c) Martti Ylikoski, 1992. " ;
/*
*/
#define INCL_DOS
#include <os2.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <param.h>
#include <paramstd.h>
#include "dirscan.h"
/* prototypes */
int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds) ;
void TimeWaitUntil(int hours, int minutes, int seconds) ;
int ParamSetFile(int argc, char *argv[], int index );
//#define AT
//#define UNTIL
extern unsigned long pflags ;
ParamEntry pentry[12] = {
"NOD", &ParamSetNoDefault, 0,
"P", &ParamSetPause, 0,
#ifdef AT
"F", &ParamSetFile, 2,
#endif
"V", &ParamSetVerbose, 1,
"R", &ParamSetReport, 0,
"S", &ParamSetSubDirs, 0,
"?", &ParamSetHelp, 1,
"H", &ParamSetHelp, 1,
"TEST", &ParamSetTest, 0,
"Y", &ParamSetYes, 0,
"N", &ParamSetTest, 0,
"\0", NULL, 0
} ;
ParamBlock params = {
"/-", IGNORECASE | PRIORITY | NOTRIGGERSALLOWED ,
pentry
} ;
static char *progname, *fname ;
int main(int argc, char *argv[])
{
DATETIME dt ;
UCHAR hours, minutes, seconds ;
FILE *fp ;
char fbuff[125], *t1, *t2 ;
progname = argv[0] ;
ParamHandle(¶ms, &argc, argv) ;
#ifdef AT
if (pflags & PA_HELP)
{
printf("%s - start a program at a specified time.\n", progname) ;
puts(VERSION) ;
puts("Usage: at time \"program with parameters\" | [/? | /H]") ;
#endif
#ifdef UNTIL
if (pflags & PA_HELP)
{
printf("%s - suspend execution until a specified time.\n", progname) ;
puts(VERSION) ;
puts("Usage: until time | [/? | /H]") ;
#endif
puts("Where,") ;
puts(" /H,/? = Help") ;
puts(" TIME FORMAT:") ;
puts(" \"hh:mm:ss\" or +\"hh:mm:ss\"") ;
// puts(" : is the time separator character used in your country.") ;
puts("+ before the time means that the value is added to the current time") ;
#ifdef AT
puts("E.g. at +00:00:10 dir, means that after 10 seconds the dir-command is run") ;
#endif
#ifdef UNTIL
puts("E.g. until 10:00 wait until the time is ten o'clock.") ;
#endif
return( 0 ) ;
}
#ifdef UNTIL
if (argc < 2)
{
fprintf(stderr, "Missing time...\nExit\n") ;
return( 1 ) ;
}
#endif
#ifdef AT
if (pflags & PA_FILE || argc == 1)
{
if (argc == 1)
{
printf("%s: no command line parameters.\n", progname);
puts("Reading default at.dat file for commands.") ;
fname = "at.dat" ; /* default for at-file */
}
if ((fp = fopen (fname, "r")) == NULL)
{
printf("%s : error opening at-file %s.\nExit\n", progname, fname) ;
return( 1 ) ;
}
while (fgets(fbuff, sizeof(fbuff), fp ) != NULL)
{
if (strlen(fbuff) == 0 || fbuff[0] == '\n')
continue ;
if (fbuff[0] == '#' || fbuff[0] == ';') /* handle comment lines */
continue ;
if (strnicmp(fbuff, "CONTINUE",8) == 0)
{
rewind(fp) ;
continue ;
}
if (strnicmp(fbuff, "BREAK",5) == 0 || strnicmp(fbuff, "QUIT",4) == 0)
{
fclose(fp) ;
return( 0 ) ;
}
t1 = strtok(fbuff, " ") ;
t2 = t1 + strlen(t1) +1;
StrExpandTime(t1, ":", &hours, &minutes, &seconds) ;
TimeWaitUntil(hours, minutes, seconds) ;
if (strlen(t2) > 0)
system(t2) ;
}
fclose (fp) ;
}
else
#endif
{
#ifdef AT
if (argc < 3)
{
fprintf(stderr, "Missing time or program...\nExit\n") ;
return( 1 ) ;
}
#endif
StrExpandTime(argv[1], ":", &hours, &minutes, &seconds) ;
TimeWaitUntil(hours, minutes, seconds) ;
#ifdef AT
system(argv[2]) ;
#endif
}
return( 0 ) ;
}
void TimeWaitUntil(int hours, int minutes, int seconds)
{
DATETIME dt ;
LONG sleepval ;
USHORT tomorrow = FALSE ;
DosGetDateTime(&dt) ;
if (dt.hours > hours) /* schedule for tomorrow */
tomorrow = TRUE ;
do
{
DosGetDateTime(&dt) ;
if(dt.hours == 0)
tomorrow = FALSE ;
sleepval = ((hours - dt.hours)*3600 + (minutes-dt.minutes)*60 +
seconds-dt.seconds)/2 ;
if (sleepval < 0 && tomorrow == FALSE)
break ;
if (sleepval == 0)
DosSleep(320L) ;
else
DosSleep(sleepval*1000L) ;
} while ( hours > dt.hours ||
(hours ==dt.hours && minutes > dt.minutes) ||
(hours ==dt.hours && minutes == dt.minutes && seconds > dt.seconds) ||
tomorrow == TRUE ) ;
}
int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds)
{
int add = FALSE ;
DATETIME dt ;
char *tmp ;
/* set default values for time */
DosGetDateTime(&dt) ;
*hours = dt.hours ;
*minutes = dt.minutes ;
*seconds = dt.seconds ;
if (str[0] == '+')
add = TRUE ;
if ( (tmp=strtok(str, tsepa)) == NULL)
return( 1 ) ;
if (add==TRUE)
*hours += atoi(tmp) ;
else
*hours=atoi(tmp) ;
if (*hours >24)
{
*hours -= 24 ;
}
/* new default values */
if (add == FALSE)
{
*minutes = '0' ;
*seconds = '0' ;
}
if ( (tmp=strtok(NULL, tsepa)) == NULL)
return( 0 ) ;
if (add==TRUE)
*minutes += atoi(tmp) ;
else
*minutes = atoi(tmp) ;
if (*minutes >60)
{
*minutes -= 60 ;
*hours += 1 ;
}
if ( (tmp=strtok(NULL, tsepa)) == NULL)
return( 0 ) ;
if (add==TRUE)
*seconds += atoi(tmp) ;
else
*seconds = atoi(tmp) ;
if (*seconds >60)
{
*seconds -=60 ;
*minutes += 1 ;
}
return( 0 ) ;
}
int ParamSetFile(int argc, char *argv[], int index )
{
pflags |= PA_FILE ;
fname = argv[index+1] ;
return( 2 ) ;
}