chiark / gitweb /
Experimental use of structure literals.
authorBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 23:04:24 +0000 (23:04 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 23:04:24 +0000 (23:04 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index e149156bd1f94c357dc94f46de7a1f33d17aecfd..0da249cf740f68e7024f333c8e1e4ff28e803b12 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -137,6 +137,7 @@ init(int argc, char **argv)
        struct timespec ts;
        struct sigevent sev;
        struct sigaction sa;
+       char *statefile = NULL, *statestr = NULL;
        int opt;
 
        tzset();
@@ -162,13 +163,18 @@ init(int argc, char **argv)
        while ((opt = getopt(argc, argv, "f:s:")) != -1) {
                switch (opt) {
                case 'f':
-                       init_statefile(optarg);
+                       statefile = optarg;
                        break;
                case 's':
-                       init_statestring(optarg);
+                       statestr = optarg;
                        break;
                }
        }
+       if (statefile != NULL)
+               init_statefile(statefile);
+       /* Allow -s to override state read from file. */
+       if (statestr != NULL)
+               init_statestring(statestr);
 }
 
 enum need_adjust { STOP, TICK, ADVANCE };
@@ -201,7 +207,13 @@ static void
 run()
 {
        struct timespec ts;
-       struct itimerspec its;
+       const struct itimerspec its = {
+               .it_value    = { .tv_sec = 35, .tv_nsec = 0},
+               .it_interval = { .tv_sec = 1,  .tv_nsec = 0},
+       };
+       const struct itimerspec its_disarm = {
+               .it_value =  { .tv_sec = 0, .tv_nsec = 0 },
+       };
        struct tm tm;
        int tick;
 
@@ -228,18 +240,13 @@ run()
                /* Choose when next tick will be. */
                ts.tv_nsec = 1000000000 - pulsewidth;
                ts.tv_sec += tick - 1;
-               its.it_value.tv_sec = 35;
-               its.it_value.tv_nsec = 0;
-               its.it_interval.tv_sec = 1;
-               its.it_interval.tv_nsec = 0;
                if (timer_settime(fallback_timer, 0, &its, NULL) != 0)
                        err(1, "timer_settime (arm)");
                errno = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME,
                                        &ts, NULL);
-               its.it_value.tv_sec = 0;
                if (errno != 0 && errno != EINTR)
                        err(1, "clock_nanosleep");
-               if (timer_settime(fallback_timer, 0, &its, NULL) != 0)
+               if (timer_settime(fallback_timer, 0, &its_disarm, NULL) != 0)
                        err(1, "timer_settime (disarm)");
        }
 }