From: Ben Harris Date: Sun, 11 Nov 2018 18:24:37 +0000 (+0000) Subject: Implement reading from state file. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=0b03b7d584e6bdcf601ba735d079bdc07ee87399;p=clunk.git Implement reading from state file. --- 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; } }