chiark / gitweb /
Implement reading from state file.
authorBen Harris <bjh21@bjh21.me.uk>
Sun, 11 Nov 2018 18:24:37 +0000 (18:24 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sun, 11 Nov 2018 18:24:37 +0000 (18:24 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index 0ca065aab8306aaf2e0fbcea63376d3f75ef6369..cbb8efb33f4cb6b660b81cfab543c01d90d43d2a 100644 (file)
--- 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;
                }
        }