home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / amigalibdisks / disk950 / clocktool / clocktool.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  26KB  |  1,493 lines

  1. #define VERSION "1.0"
  2.  
  3. #define EARLIEST_LIB    37    /* 2.04 and above */
  4.  
  5. /*
  6.  *
  7.  * ClockTool.c
  8.  *
  9.  *  Displays/adjusts/sets times of battery clock and/or system clock
  10.  *  from one or the other.
  11.  *
  12.  * Options:
  13.  *
  14.  *    -b       ; battery clock
  15.  *    -s       ; system  clock
  16.  *    -annnn   ; adjust battery clock time by -nnnn millisecs",
  17.  *    +annnn   ; adjust battery clock time by +nnnn millisecs",
  18.  *    -l<file> ; log system time to <file> , (default LOG_CLOCKTOOL:)
  19.  *    -n<str>  ; append the <str> to log file entry, e.g. "reboot"
  20.  *
  21.  *  (see Usage () )
  22.  *
  23.  * Runs under AmigaDOS 2.04+, and only machines containing
  24.  * a battery clock.
  25.  *
  26.  *
  27.  *
  28.  *****    Written by Gary Duncan
  29.  *
  30.  *    Bug reports etc via e-mail to gduncan@philips.oz.au) , or mail to
  31.  *
  32.  *Work:    Gary Duncan
  33.  *    Philips PTS
  34.  *      23 Lakeside Dr
  35.  *    Tally-Ho Technology Park
  36.  *    Burwood East Vic 3151
  37.  *    Australia
  38.  *
  39.  *Home:    Gary Duncan
  40.  *    57 Melbourne Hill Rd
  41.  *    Warrandyte
  42.  *    Vic 3113
  43.  *     Australia
  44.  *
  45.  *
  46.  *
  47.  *****    Freely distributable for non-commercial purposes.
  48.  *    Please keep this header intact.
  49.  *
  50.  *
  51.  *    Compiles under SAS 6.2
  52.  *
  53.  *    Formatted with 'indent -gnu' ; a big time-saving program.
  54.  *
  55.  * Functions:-
  56.  
  57.     main
  58.     tv2dss
  59.     tv2str
  60.     dss2tv
  61.     dss2str
  62.     str2dss
  63.     Init_Timer
  64.     hexit
  65.     DeleteTimer
  66.     usage
  67.     scan_args
  68.     tv_diff
  69.     tv_diff_str
  70.     format_diff_str
  71.     set_system_time
  72.     get_system_time
  73.     get_battclock_time
  74.     set_battclock_time
  75.     tv_update
  76.     check4battclock
  77.     get_syncd_batt_time
  78.     get_syncd_sys_time
  79.     print_clock
  80.     display_clocks
  81.     adjust_clock
  82.     set_syncd_system_time
  83.     set_syncd_battclock_time
  84.     wait_for_timer
  85.     pause
  86.  
  87.  */
  88.  
  89. #include "ClockTool.h"
  90.  
  91.  
  92. /*
  93.  * string for AmigaDOS Version Command
  94.  */
  95. char A_vers[] = "\0$VER: clocktool\t" VERSION " (" __DATE__ " " __TIME__ ")";
  96.  
  97.  
  98. LIBRARY *UtilityBase = NULL;
  99. LIBRARY *BattClockBase;
  100.  
  101. TIMEVAL tv_sysclock;
  102. TIMEVAL tv_battclock;
  103. TIMEVAL tv_tmp;
  104.  
  105. DATESTAMP dss_sysclock;
  106. DATESTAMP dss_battclock;
  107.  
  108. LONG adjust;
  109. LONG temp;
  110.  
  111. MSGPORT *TimeP = NULL;
  112. TIMERREQUEST *Timermsg;
  113. IOREQUEST *Timer = NULL;
  114.  
  115. BOOL a_flag = FALSE;
  116. BOOL b_flag = FALSE;
  117. BOOL l_flag = FALSE;
  118. BOOL n_flag = FALSE;
  119. BOOL s_flag = FALSE;
  120.  
  121. char buf[80];
  122. char *xstr = "";
  123.  
  124. int opt_count = 0;
  125.  
  126. char *logfile;
  127. FILE *fp = NULL;
  128. char log_buf[80];
  129.  
  130. /*
  131.  * used by date conversion functions
  132.  */
  133.  
  134. DM days_month[] =
  135. {
  136.   31, "Jan",
  137.   28, "Feb",
  138.   31, "Mar",
  139.   30, "Apr",
  140.   31, "May",
  141.   30, "Jun",
  142.   31, "Jul",
  143.   31, "Aug",
  144.   30, "Sep",
  145.   31, "Oct",
  146.   30, "Nov",
  147.   31, "Dec",
  148. };
  149.  
  150. /*
  151.  *********************************************************************
  152.  */
  153.  
  154. VOID
  155. main (int argc, char **argv)
  156. {
  157.  
  158.   /*
  159.    * abort if no battclock and not AmigaDos 2.xx
  160.    */
  161.   if (check4battclock () == FALSE)
  162.     {
  163.       hexit (1);
  164.     }
  165.  
  166.   scan_args (argc, argv);
  167.  
  168.   Init_Timer ();
  169.  
  170.   /*
  171.    ***** Mode (1) ? - just display both clocks ?
  172.    */
  173.   if (opt_count == 0)
  174.     {
  175.       get_syncd_batt_time (&tv_battclock);
  176.       get_system_time (&tv_sysclock);
  177.  
  178.       display_clocks (&tv_battclock, &tv_sysclock);
  179.       hexit (1);
  180.     }
  181.   /*
  182.    * check that there is at least only one b or s option
  183.    */
  184.  
  185.   if ((b_flag == TRUE) && (s_flag == TRUE))
  186.     {
  187.       usage ("both b and s options set");
  188.       hexit (1);
  189.     }
  190.  
  191.   if ((b_flag == FALSE) && (s_flag == FALSE))
  192.     {
  193.       usage ("need either b or s option");
  194.       hexit (1);
  195.     }
  196.  
  197.   /*
  198.    ***** Mode (2) -  just a s or b option ?
  199.    */
  200.  
  201.   if (opt_count == 1)
  202.     {
  203.       /*
  204.        * just set one clock from the other
  205.        */
  206.       if (s_flag == TRUE)
  207.     {
  208.       (void) set_syncd_system_time ();
  209.  
  210.       print_old_new ("system", &tv_sysclock, &tv_battclock);
  211.     }
  212.       else
  213.     /* set battclock from system time */
  214.     {
  215.       (void) set_syncd_battclock_time ();
  216.  
  217.       print_old_new ("battery", &tv_sysclock, &tv_battclock);
  218.     }
  219.  
  220.       hexit (1);
  221.     }
  222.  
  223.   /*
  224.    ***** Mode (3) - adjust a clock ?
  225.    */
  226.   if ((n_flag == TRUE) && (l_flag == FALSE))
  227.     {
  228.       usage ("need l option");
  229.       hexit (1);
  230.     }
  231.  
  232.   if (a_flag == TRUE)
  233.     {
  234.       adjust_clock (adjust, &tv_tmp);
  235.     }
  236.  
  237.   if (l_flag == FALSE)
  238.     {
  239.       if (s_flag == TRUE)
  240.     {
  241.       print_old_new ("system", &tv_sysclock, &tv_tmp);
  242.     }
  243.       else
  244.     {
  245.       print_old_new ("battery", &tv_battclock, &tv_tmp);
  246.     }
  247.       hexit (1);
  248.     }
  249.  
  250.   /*
  251.    ***** Mode (4) - log selected clock time  ?
  252.    */
  253.  
  254.  
  255.   /*
  256.    * we have l option here...
  257.    */
  258.  
  259.   if (s_flag == TRUE)
  260.     {
  261.       temp = set_syncd_system_time ();
  262.  
  263.       sprintf (log_buf, "S: %s %s", tv2str (&tv_sysclock),
  264.            format_diff_str (temp, "(%s secs)  "));
  265.     }
  266.   else
  267.     /* set battclock from system time */
  268.     {
  269.       temp = set_syncd_battclock_time ();
  270.  
  271.       sprintf (log_buf, "B: %s %s", tv2str (&tv_battclock),
  272.            format_diff_str (temp, "(%s secs)  "));
  273.     }
  274.  
  275.   if (n_flag == TRUE)
  276.     {
  277.       strcat (log_buf, xstr);
  278.     }
  279.  
  280.   strcat (log_buf, "\n");
  281.  
  282.   printf ("%s", log_buf);
  283.  
  284. #if 1
  285.   /*
  286.    * now log message , maybe
  287.    */
  288.   if (fp)
  289.     {
  290.       int rote;
  291.       int len = strlen (log_buf);
  292.  
  293.       if ((rote = fwrite (log_buf, 1, len, fp)) != len)
  294.     {
  295.       fprintf (stderr, "fwrite error (wanted=%d),(actual=%d)\n",
  296.            len, rote);
  297.     }
  298.     }
  299. #endif
  300.  
  301.   hexit (1);
  302. }
  303.  
  304.  
  305. /***************************************************************************
  306.  
  307.  
  308.   Function :      tv2dss
  309.  
  310.   Purpose:    given a Timeval structure , returns Datestamp
  311.  
  312.   Entry    :    ptr to Timeval
  313.         ptr to Datestamp
  314.  
  315.   Returns  :
  316.  
  317.  
  318. ****************************************************************************/
  319.  
  320.  
  321. void
  322. tv2dss (TIMEVAL * tv, DATESTAMP * dss)
  323.  
  324. {
  325.   long rem;
  326.  
  327.   dss->ds_Days = tv->tv_secs / (24 * 60 * 60);
  328.   rem = tv->tv_secs % (24 * 60 * 60);    /* secs in last day */
  329.  
  330.   dss->ds_Minute = rem / 60;
  331.   rem = rem % 60;        /* secs in last minute */
  332.  
  333.   rem = (rem * 1000) + (tv->tv_micro / 1000);    /* msecs in last minute */
  334.  
  335.   dss->ds_Tick = rem / (1000 / TICKS_PER_SECOND);    /* ticks in last minute */
  336. }
  337.  
  338. /***************************************************************************
  339.  
  340.  
  341.   Function :      tv2str
  342.  
  343.   Purpose:    given a Timeval structure , returns date string
  344.  
  345.   Entry    :    ptr to Timeval
  346.  
  347.   Returns  :
  348.  
  349.  
  350. ****************************************************************************/
  351.  
  352.  
  353. char *
  354. tv2str (TIMEVAL * tv)
  355.  
  356. {
  357.   static char tbuf[40];
  358.   DATESTAMP dss;
  359.  
  360.   tv2dss (tv, &dss);
  361.   dss2str (&dss, tbuf);
  362.   return tbuf;
  363. }
  364.  
  365. /***************************************************************************
  366.  
  367.  
  368.   Function :       dss2tv
  369.  
  370.   Purpose:    given a DateStamp structure , returns an updated TIMEVAL
  371.  
  372.   Entry    :    ptr to DateStamp structure
  373.  
  374.   Returns  :
  375.  
  376.  
  377. ****************************************************************************/
  378.  
  379.  
  380. void
  381. dss2tv (DATESTAMP * t1, TIMEVAL * tv)
  382.  
  383. {
  384.   ULONG secs;
  385.  
  386.   secs = t1->ds_Days * 24 * 60 * 60;
  387.   secs += t1->ds_Minute * 60;
  388.   secs += t1->ds_Tick / TICKS_PER_SECOND;
  389.  
  390.   tv->tv_secs = secs;
  391.   tv->tv_micro = (t1->ds_Tick % TICKS_PER_SECOND) * (1000000 / TICKS_PER_SECOND);
  392.  
  393. }
  394.  
  395. /***************************************************************************
  396.  
  397.  
  398.   Function :      dss2str
  399.  
  400.   Purpose:    generates a date string given a DateStamp
  401.  
  402.   Entry    :    ptr to Datestamp
  403.  
  404.  
  405.   Returns  :    ptr to date string
  406.  
  407.  
  408.  
  409. ****************************************************************************/
  410.  
  411.  
  412. void
  413. dss2str (DATESTAMP * dss, char *str)
  414.  
  415. {
  416.   static TIMEVAL tv;
  417.   struct tm Time;
  418.   int j;
  419.   long time, ago;
  420.  
  421.  
  422.   dss2tv (dss, &tv);        /* date to secs/usecs since 01 Jan 78 */
  423.   time = tv.tv_secs;
  424.  
  425.   Time.tm_sec = time % 60;
  426.   time /= 60;
  427.   Time.tm_min = time % 60;
  428.   time /= 60;
  429.   Time.tm_hour = time % 24;
  430.   time /= 24;
  431.   Time.tm_wday = time % 7;
  432.   Time.tm_year = 78 + (time / (4 * 365 + 1)) * 4;
  433.   time %= 4 * 365 + 1;
  434.  
  435.  
  436.   while (time)
  437.     {
  438.       ago = 365;
  439.       if ((Time.tm_year & 3) == 0)    /* leap year */
  440.     ago++;
  441.       if (time < ago)
  442.     break;
  443.       time -= ago;
  444.       Time.tm_year++;
  445.     }
  446.   Time.tm_yday = ++time;
  447.  
  448.   for (j = 0; j < 12; j++)
  449.     {
  450.       ago = days_month[j].days;
  451.       if (j == 1 && (Time.tm_year & 3) == 0)    /* Feb , and leap year ? */
  452.     ago++;
  453.       if (time <= ago)
  454.     break;
  455.       time -= ago;
  456.     }
  457.   Time.tm_mon = j;
  458.   Time.tm_mday = time;
  459.  
  460.   /* date format : 23-dec-88 22:33:01  */
  461.  
  462.   sprintf (str, "%02d-%s-%2d %02d:%02d:%02d.%03d",
  463.        Time.tm_mday,
  464.        days_month[Time.tm_mon].months,
  465.        Time.tm_year,
  466.        Time.tm_hour,
  467.        Time.tm_min,
  468.        Time.tm_sec,
  469.        tv.tv_micro / 1000
  470.     );
  471.  
  472. }
  473.  
  474.  
  475.  
  476. /***********