home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
new
/
amigalibdisks
/
disk950
/
clocktool
/
clocktool.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-21
|
26KB
|
1,493 lines
#define VERSION "1.0"
#define EARLIEST_LIB 37 /* 2.04 and above */
/*
*
* ClockTool.c
*
* Displays/adjusts/sets times of battery clock and/or system clock
* from one or the other.
*
* Options:
*
* -b ; battery clock
* -s ; system clock
* -annnn ; adjust battery clock time by -nnnn millisecs",
* +annnn ; adjust battery clock time by +nnnn millisecs",
* -l<file> ; log system time to <file> , (default LOG_CLOCKTOOL:)
* -n<str> ; append the <str> to log file entry, e.g. "reboot"
*
* (see Usage () )
*
* Runs under AmigaDOS 2.04+, and only machines containing
* a battery clock.
*
*
*
***** Written by Gary Duncan
*
* Bug reports etc via e-mail to gduncan@philips.oz.au) , or mail to
*
*Work: Gary Duncan
* Philips PTS
* 23 Lakeside Dr
* Tally-Ho Technology Park
* Burwood East Vic 3151
* Australia
*
*Home: Gary Duncan
* 57 Melbourne Hill Rd
* Warrandyte
* Vic 3113
* Australia
*
*
*
***** Freely distributable for non-commercial purposes.
* Please keep this header intact.
*
*
* Compiles under SAS 6.2
*
* Formatted with 'indent -gnu' ; a big time-saving program.
*
* Functions:-
main
tv2dss
tv2str
dss2tv
dss2str
str2dss
Init_Timer
hexit
DeleteTimer
usage
scan_args
tv_diff
tv_diff_str
format_diff_str
set_system_time
get_system_time
get_battclock_time
set_battclock_time
tv_update
check4battclock
get_syncd_batt_time
get_syncd_sys_time
print_clock
display_clocks
adjust_clock
set_syncd_system_time
set_syncd_battclock_time
wait_for_timer
pause
*/
#include "ClockTool.h"
/*
* string for AmigaDOS Version Command
*/
char A_vers[] = "\0$VER: clocktool\t" VERSION " (" __DATE__ " " __TIME__ ")";
LIBRARY *UtilityBase = NULL;
LIBRARY *BattClockBase;
TIMEVAL tv_sysclock;
TIMEVAL tv_battclock;
TIMEVAL tv_tmp;
DATESTAMP dss_sysclock;
DATESTAMP dss_battclock;
LONG adjust;
LONG temp;
MSGPORT *TimeP = NULL;
TIMERREQUEST *Timermsg;
IOREQUEST *Timer = NULL;
BOOL a_flag = FALSE;
BOOL b_flag = FALSE;
BOOL l_flag = FALSE;
BOOL n_flag = FALSE;
BOOL s_flag = FALSE;
char buf[80];
char *xstr = "";
int opt_count = 0;
char *logfile;
FILE *fp = NULL;
char log_buf[80];
/*
* used by date conversion functions
*/
DM days_month[] =
{
31, "Jan",
28, "Feb",
31, "Mar",
30, "Apr",
31, "May",
30, "Jun",
31, "Jul",
31, "Aug",
30, "Sep",
31, "Oct",
30, "Nov",
31, "Dec",
};
/*
*********************************************************************
*/
VOID
main (int argc, char **argv)
{
/*
* abort if no battclock and not AmigaDos 2.xx
*/
if (check4battclock () == FALSE)
{
hexit (1);
}
scan_args (argc, argv);
Init_Timer ();
/*
***** Mode (1) ? - just display both clocks ?
*/
if (opt_count == 0)
{
get_syncd_batt_time (&tv_battclock);
get_system_time (&tv_sysclock);
display_clocks (&tv_battclock, &tv_sysclock);
hexit (1);
}
/*
* check that there is at least only one b or s option
*/
if ((b_flag == TRUE) && (s_flag == TRUE))
{
usage ("both b and s options set");
hexit (1);
}
if ((b_flag == FALSE) && (s_flag == FALSE))
{
usage ("need either b or s option");
hexit (1);
}
/*
***** Mode (2) - just a s or b option ?
*/
if (opt_count == 1)
{
/*
* just set one clock from the other
*/
if (s_flag == TRUE)
{
(void) set_syncd_system_time ();
print_old_new ("system", &tv_sysclock, &tv_battclock);
}
else
/* set battclock from system time */
{
(void) set_syncd_battclock_time ();
print_old_new ("battery", &tv_sysclock, &tv_battclock);
}
hexit (1);
}
/*
***** Mode (3) - adjust a clock ?
*/
if ((n_flag == TRUE) && (l_flag == FALSE))
{
usage ("need l option");
hexit (1);
}
if (a_flag == TRUE)
{
adjust_clock (adjust, &tv_tmp);
}
if (l_flag == FALSE)
{
if (s_flag == TRUE)
{
print_old_new ("system", &tv_sysclock, &tv_tmp);
}
else
{
print_old_new ("battery", &tv_battclock, &tv_tmp);
}
hexit (1);
}
/*
***** Mode (4) - log selected clock time ?
*/
/*
* we have l option here...
*/
if (s_flag == TRUE)
{
temp = set_syncd_system_time ();
sprintf (log_buf, "S: %s %s", tv2str (&tv_sysclock),
format_diff_str (temp, "(%s secs) "));
}
else
/* set battclock from system time */
{
temp = set_syncd_battclock_time ();
sprintf (log_buf, "B: %s %s", tv2str (&tv_battclock),
format_diff_str (temp, "(%s secs) "));
}
if (n_flag == TRUE)
{
strcat (log_buf, xstr);
}
strcat (log_buf, "\n");
printf ("%s", log_buf);
#if 1
/*
* now log message , maybe
*/
if (fp)
{
int rote;
int len = strlen (log_buf);
if ((rote = fwrite (log_buf, 1, len, fp)) != len)
{
fprintf (stderr, "fwrite error (wanted=%d),(actual=%d)\n",
len, rote);
}
}
#endif
hexit (1);
}
/***************************************************************************
Function : tv2dss
Purpose: given a Timeval structure , returns Datestamp
Entry : ptr to Timeval
ptr to Datestamp
Returns :
****************************************************************************/
void
tv2dss (TIMEVAL * tv, DATESTAMP * dss)
{
long rem;
dss->ds_Days = tv->tv_secs / (24 * 60 * 60);
rem = tv->tv_secs % (24 * 60 * 60); /* secs in last day */
dss->ds_Minute = rem / 60;
rem = rem % 60; /* secs in last minute */
rem = (rem * 1000) + (tv->tv_micro / 1000); /* msecs in last minute */
dss->ds_Tick = rem / (1000 / TICKS_PER_SECOND); /* ticks in last minute */
}
/***************************************************************************
Function : tv2str
Purpose: given a Timeval structure , returns date string
Entry : ptr to Timeval
Returns :
****************************************************************************/
char *
tv2str (TIMEVAL * tv)
{
static char tbuf[40];
DATESTAMP dss;
tv2dss (tv, &dss);
dss2str (&dss, tbuf);
return tbuf;
}
/***************************************************************************
Function : dss2tv
Purpose: given a DateStamp structure , returns an updated TIMEVAL
Entry : ptr to DateStamp structure
Returns :
****************************************************************************/
void
dss2tv (DATESTAMP * t1, TIMEVAL * tv)
{
ULONG secs;
secs = t1->ds_Days * 24 * 60 * 60;
secs += t1->ds_Minute * 60;
secs += t1->ds_Tick / TICKS_PER_SECOND;
tv->tv_secs = secs;
tv->tv_micro = (t1->ds_Tick % TICKS_PER_SECOND) * (1000000 / TICKS_PER_SECOND);
}
/***************************************************************************
Function : dss2str
Purpose: generates a date string given a DateStamp
Entry : ptr to Datestamp
Returns : ptr to date string
****************************************************************************/
void
dss2str (DATESTAMP * dss, char *str)
{
static TIMEVAL tv;
struct tm Time;
int j;
long time, ago;
dss2tv (dss, &tv); /* date to secs/usecs since 01 Jan 78 */
time = tv.tv_secs;
Time.tm_sec = time % 60;
time /= 60;
Time.tm_min = time % 60;
time /= 60;
Time.tm_hour = time % 24;
time /= 24;
Time.tm_wday = time % 7;
Time.tm_year = 78 + (time / (4 * 365 + 1)) * 4;
time %= 4 * 365 + 1;
while (time)
{
ago = 365;
if ((Time.tm_year & 3) == 0) /* leap year */
ago++;
if (time < ago)
break;
time -= ago;
Time.tm_year++;
}
Time.tm_yday = ++time;
for (j = 0; j < 12; j++)
{
ago = days_month[j].days;
if (j == 1 && (Time.tm_year & 3) == 0) /* Feb , and leap year ? */
ago++;
if (time <= ago)
break;
time -= ago;
}
Time.tm_mon = j;
Time.tm_mday = time;
/* date format : 23-dec-88 22:33:01 */
sprintf (str, "%02d-%s-%2d %02d:%02d:%02d.%03d",
Time.tm_mday,
days_month[Time.tm_mon].months,
Time.tm_year,
Time.tm_hour,
Time.tm_min,
Time.tm_sec,
tv.tv_micro / 1000
);
}
/***********