From 0b03b7d584e6bdcf601ba735d079bdc07ee87399 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 11 Nov 2018 18:24:37 +0000 Subject: [PATCH] Implement reading from state file. --- clunk.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/clunk.c b/clunk.c index 0ca065a..cbb8efb 100644 --- a/clunk.c +++ b/clunk.c @@ -80,6 +80,30 @@ pulse() dummy_out(false); } +static void +init_statestring(char const *str) +{ + if (sscanf(str, "%d:%d:%d", &displayed.tm_hour, + &displayed.tm_min, &displayed.tm_sec) != 3) + errx(1, "usage"); +} + +static void +init_statefile(char const *statefilename) +{ + char buf[10]; + ssize_t len; + + statefd = open(optarg, O_RDWR | O_CREAT | O_DSYNC, 0666); + if (statefd == -1) + err(1, "%s", optarg); + len = pread(statefd, buf, 9, 0); + if (len == -1) + err(1, "read %s", optarg); + buf[len] = '\0'; + init_statestring(buf); +} + static void init(int argc, char **argv) { @@ -95,15 +119,10 @@ init(int argc, char **argv) while ((opt = getopt(argc, argv, "f:s:")) != -1) { switch (opt) { case 'f': - statefd = - open(optarg, O_RDWR | O_CREAT | O_DSYNC, 0666); - if (statefd == -1) - err(1, "%s", optarg); + init_statefile(optarg); break; case 's': - if (sscanf(optarg, "%d:%d:%d", &displayed.tm_hour, - &displayed.tm_min, &displayed.tm_sec) != 3) - errx(1, "usage"); + init_statestring(optarg); break; } } -- 2.30.2