aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

hwclock.c (3263B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #if FEATURE_LINUX_TIME
      4 #ifndef _DEFAULT_SOURCE
      5 #define _DEFAULT_SOURCE
      6 #endif
      7 #endif
      8 #include <sys/ioctl.h>
      9 #include <sys/stat.h>
     10 #include <sys/time.h>
     11 #include <sys/types.h>
     12 
     13 #include <fcntl.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 #include <time.h>
     18 #include <unistd.h>
     19 
     20 #include "paths.h"
     21 #include "rtc.h"
     22 #include "util.h"
     23 
     24 static void
     25 readrtctm(struct tm *tm, int fd)
     26 {
     27   struct rtc_time rt;
     28 
     29   memset(&rt, 0, sizeof(rt));
     30   if (ioctl(fd, (int)RTC_RD_TIME, &rt) < 0)
     31     eprintf("RTC_RD_TIME:");
     32   tm->tm_sec   = rt.tm_sec;
     33   tm->tm_min   = rt.tm_min;
     34   tm->tm_hour  = rt.tm_hour;
     35   tm->tm_mday  = rt.tm_mday;
     36   tm->tm_mon   = rt.tm_mon;
     37   tm->tm_year  = rt.tm_year;
     38   tm->tm_wday  = rt.tm_wday;
     39   tm->tm_yday  = rt.tm_yday;
     40   tm->tm_isdst = rt.tm_isdst;
     41 }
     42 
     43 static void
     44 writertctm(struct tm *tm, int fd)
     45 {
     46   struct rtc_time rt;
     47 
     48   rt.tm_sec   = tm->tm_sec;
     49   rt.tm_min   = tm->tm_min;
     50   rt.tm_hour  = tm->tm_hour;
     51   rt.tm_mday  = tm->tm_mday;
     52   rt.tm_mon   = tm->tm_mon;
     53   rt.tm_year  = tm->tm_year;
     54   rt.tm_wday  = tm->tm_wday;
     55   rt.tm_yday  = tm->tm_yday;
     56   rt.tm_isdst = tm->tm_isdst;
     57   if (ioctl(fd, (int)RTC_SET_TIME, &rt) < 0)
     58     eprintf("RTC_SET_TIME:");
     59 }
     60 
     61 static void
     62 show(char *dev)
     63 {
     64   struct tm tm;
     65   time_t    t;
     66   int       fd;
     67 
     68   fd = open(dev, O_RDONLY);
     69   if (fd < 0)
     70     eprintf("open %s:", dev);
     71   readrtctm(&tm, fd);
     72   t = mktime(&tm);
     73   printf("%s", asctime(localtime(&t)));
     74   close(fd);
     75 }
     76 
     77 static void
     78 hctosys(char *dev)
     79 {
     80   struct timeval tv;
     81   struct tm      tm;
     82   int            r;
     83   int            fd;
     84 
     85   fd = open(dev, O_RDONLY);
     86   if (fd < 0)
     87     eprintf("open %s:", dev);
     88   readrtctm(&tm, fd);
     89   tv.tv_sec  = mktime(&tm);
     90   tv.tv_usec = 0;
     91   r          = settimeofday(&tv, NULL);
     92   if (r < 0)
     93     eprintf("settimeofday:");
     94   close(fd);
     95 }
     96 
     97 static void
     98 systohc(char *dev)
     99 {
    100   struct timeval tv;
    101   struct tm     *tm;
    102   time_t         t;
    103   int            fd;
    104 
    105   fd = open(dev, O_WRONLY);
    106   if (fd < 0)
    107     eprintf("open %s:", dev);
    108   gettimeofday(&tv, NULL);
    109   t  = tv.tv_sec;
    110   tm = gmtime(&t);
    111   weprintf("warning: assuming UTC for systohc\n");
    112   writertctm(tm, fd);
    113   close(fd);
    114 }
    115 
    116 static void
    117 usage(void)
    118 {
    119   eprintf("usage: %s [-rsw] [-u] [dev]\n", argv0);
    120 }
    121 
    122 // ?man hwclock: query or set the hardware clock
    123 // ?man arguments: dev
    124 // ?man view or adjust the hardware real time clock
    125 int
    126 main(int argc, char *argv[])
    127 {
    128   char *dev   = ARUU_PATH_DEV "/rtc";
    129   int   rflag = 0;
    130   int   sflag = 0;
    131   int   wflag = 0;
    132 
    133   ARGBEGIN
    134   {
    135     // ?man -r: operate recursively
    136     case 'r':
    137       rflag = 1;
    138       break;
    139     // ?man -s: silent mode or print summary
    140     case 's':
    141       sflag = 1;
    142       break;
    143     // ?man -w: wait for completion
    144     case 'w':
    145       wflag = 1;
    146       break;
    147     // ?man -u: unbuffered output
    148     case 'u':
    149       break;
    150     default:
    151       usage();
    152   }
    153   ARGEND;
    154 
    155   if (argc > 1)
    156     usage();
    157   else if (argc == 1)
    158     dev = argv[0];
    159 
    160   if ((rflag ^ sflag ^ wflag) == 0)
    161     eprintf("missing or incompatible function\n");
    162 
    163   /* only UTC support at the moment */
    164   setenv("TZ", "UTC0", 1);
    165   tzset();
    166 
    167   if (rflag == 1)
    168     show(dev);
    169   else if (sflag == 1)
    170     hctosys(dev);
    171   else if (wflag == 1)
    172     systohc(dev);
    173 
    174   return 0;
    175 }