[hackers] [ubase] Implement -s support for hwclock(8) || sin

From: <git_AT_suckless.org>
Date: Tue, 03 Jun 2014 16:58:12 +0200

commit 5836145a5c4518131303027a47afb42679cc0da1
Author: sin <sin_AT_2f30.org>
Date: Tue Jun 3 15:52:45 2014 +0100

    Implement -s support for hwclock(8)
    
    Rename some functions as well.

diff --git a/hwclock.c b/hwclock.c
index 4193b62..a259a98 100644
--- a/hwclock.c
+++ b/hwclock.c
_AT_@ -12,15 +12,16 @@
 #include "rtc.h"
 #include "util.h"
 
-static void echotime(char *);
 static void readrtctm(struct tm *, int);
 static void writertctm(struct tm *, int);
-static void writetime(char *);
+static void show(char *);
+static void hctosys(char *);
+static void systohc(char *);
 
 static void
 usage(void)
 {
- eprintf("usage: %s [-rw] [-u]
", argv0);
+ eprintf("usage: %s [-rsw] [-u]
", argv0);
 }
 
 int
_AT_@ -28,12 +29,16 @@ main(int argc, char *argv[])
 {
         char *dev = "/dev/rtc";
         int rflag = 0;
+ int sflag = 0;
         int wflag = 0;
 
         ARGBEGIN {
         case 'r':
                 rflag = 1;
                 break;
+ case 's':
+ sflag = 1;
+ break;
         case 'w':
                 wflag = 1;
                 break;
_AT_@ -43,7 +48,7 @@ main(int argc, char *argv[])
                 usage();
         } ARGEND;
 
- if ((rflag ^ wflag) == 0)
+ if ((rflag ^ sflag ^ wflag) == 0)
                 eprintf("missing or incompatible function
");
 
         /* Only UTC support at the moment */
_AT_@ -51,30 +56,16 @@ main(int argc, char *argv[])
         tzset();
 
         if (rflag == 1)
- echotime(dev);
+ show(dev);
+ else if (sflag == 1)
+ hctosys(dev);
         else if (wflag == 1)
- writetime(dev);
+ systohc(dev);
 
         return EXIT_SUCCESS;
 }
 
 static void
-echotime(char *dev)
-{
- struct tm tm;
- time_t t;
- int fd;
-
- fd = open(dev, O_RDONLY);
- if (fd < 0)
- eprintf("open %s:", dev);
- readrtctm(&tm, fd);
- t = mktime(&tm);
- printf("%s", asctime(localtime(&t)));
- close(fd);
-}
-
-static void
 readrtctm(struct tm *tm, int fd)
 {
         struct rtc_time rt;
_AT_@ -110,7 +101,43 @@ writertctm(struct tm *tm, int fd)
 }
 
 static void
-writetime(char *dev)
+show(char *dev)
+{
+ struct tm tm;
+ time_t t;
+ int fd;
+
+ fd = open(dev, O_RDONLY);
+ if (fd < 0)
+ eprintf("open %s:", dev);
+ readrtctm(&tm, fd);
+ t = mktime(&tm);
+ printf("%s", asctime(localtime(&t)));
+ close(fd);
+}
+
+static void
+hctosys(char *dev)
+{
+ struct timeval tv;
+ struct tm tm;
+ int r;
+ int fd;
+
+ fd = open(dev, O_RDONLY);
+ if (fd < 0)
+ eprintf("open %s:", dev);
+ readrtctm(&tm, fd);
+ tv.tv_sec = mktime(&tm);
+ tv.tv_usec = 0;
+ r = settimeofday(&tv, NULL);
+ if (r < 0)
+ eprintf("settimeofday:");
+ close(fd);
+}
+
+static void
+systohc(char *dev)
 {
         struct timeval tv;
         struct tm *tm;
Received on Tue Jun 03 2014 - 16:58:12 CEST

This archive was generated by hypermail 2.3.0 : Tue Jun 03 2014 - 17:00:09 CEST