chiark / gitweb /
Slightly more robust state string parsing.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 24 Nov 2018 10:34:31 +0000 (10:34 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 8 Dec 2018 22:30:09 +0000 (22:30 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index b2ce67c07ea0db07576dc6ef4a843a3a33a47bc3..853299d359ed57180d7f56b64dbd0f2002092af6 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -158,9 +158,15 @@ pulse()
 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");
+               errx(1, "Bad state string '%s'", str);
+       if (displayed.tm_hour < 0 || displayed.tm_hour > 23 ||
+           displayed.tm_min  < 0 || displayed.tm_min  > 59 ||
+           displayed.tm_sec  < 0 || displayed.tm_sec  > 59)
+               errx(1, "Bad state string '%s'", str);
+       displayed.tm_hour %= 12;
 }
 
 static void
@@ -176,6 +182,7 @@ init_statefile(char const *statefilename)
        if (len == -1)
                err(1, "read %s", statefilename);
        if (len > 0) {
+               while (len > 0 && buf[len - 1] == '\n') len--;
                buf[len] = '\0';
                init_statestring(buf);
        }