summaryrefslogtreecommitdiff
path: root/src/clock/s6-clockadd.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-15 23:08:59 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-15 23:08:59 +0000
commite0fc82203d677a6f1e808e9a1a46176c109d89be (patch)
treee9609209b755e3f7a8480aea86601ffe9d4ca540 /src/clock/s6-clockadd.c
downloads6-networking-e0fc82203d677a6f1e808e9a1a46176c109d89be.tar.xz
Initial commit
Diffstat (limited to 'src/clock/s6-clockadd.c')
-rw-r--r--src/clock/s6-clockadd.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/clock/s6-clockadd.c b/src/clock/s6-clockadd.c
new file mode 100644
index 0000000..990f3ba
--- /dev/null
+++ b/src/clock/s6-clockadd.c
@@ -0,0 +1,58 @@
+/* ISC license. */
+
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/tai.h>
+
+#define USAGE "s6-clockadd [ -f ] [ -e errmax ]"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+int main (int argc, char const *const *argv)
+{
+ tain_t now, adj ;
+ unsigned int emax = 2000 ;
+ int flagforce = 0 ;
+ PROG = "s6-clockadd" ;
+ {
+ subgetopt_t l = SUBGETOPT_ZERO ;
+ for (;;)
+ {
+ register int opt = subgetopt_r(argc, argv, "fe:", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'f' : flagforce = 1 ; break ;
+ case 'e' : if (!uint0_scan(l.arg, &emax)) dieusage() ; break ;
+ default : dieusage() ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ }
+ {
+ char buf[TAIN_PACK] ;
+ if (allread(0, buf, TAIN_PACK) < TAIN_PACK)
+ strerr_diefu1sys(111, "read 16 bytes from stdin") ;
+ tain_unpack(buf, &adj) ;
+ }
+ tain_from_millisecs(&now, emax) ;
+ if (tain_less(&now, &adj))
+ {
+ tain_t tmp = TAIN_ZERO ;
+ tain_sub(&tmp, &tmp, &adj) ;
+ if (tain_less(&now, &tmp))
+ {
+ char fmt[UINT_FMT] ;
+ fmt[uint_fmt(fmt, emax)] = 0 ;
+ if (flagforce)
+ strerr_warnw3x("time discrepancy bigger than ", fmt, " milliseconds") ;
+ else
+ strerr_dief3x(1, "time discrepancy bigger than ", fmt, " milliseconds") ;
+ }
+ }
+ if (!sysclock_get(&now)) strerr_diefu1sys(111, "sysclock_get") ;
+ tain_add(&now, &now, &adj) ;
+ if (!sysclock_set(&now)) strerr_diefu1sys(111, "sysclock_set") ;
+ return 0 ;
+}