From 90b59c4aeeabf47ea0977872e982fac515e47e32 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 20 Mar 2022 18:01:24 -0400 Subject: main: Replace strtol() calls with sscanf() --- diff --git a/TODO b/TODO index cb5596e..f59891c 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ spaces between specifiers in strptime() formats -- strftime()? format printing: copy format to another buffer, skipping certain char test with other libc's (newlib? musl? fbsd? obsd?) `./@ 7:00 PM` doesn't work with musl strptime() -maybe sscanf() instead of strtol() formats test test other times "." in date/time formats diff --git a/configure.ac b/configure.ac index 335b927..bd957da 100644 --- a/configure.ac +++ b/configure.ac @@ -70,7 +70,7 @@ if test x"${tests}" = x'yes'; then AC_DEFINE([ENABLE_TESTS], [1], [Define to 1 to include test suite support]) AC_CHECK_FUNCS( - [strtol], + [sscanf], [], [AC_MSG_ERROR([required functions are missing])]) else diff --git a/src/main.c b/src/main.c index 572daf6..01a0a4c 100644 --- a/src/main.c +++ b/src/main.c @@ -84,34 +84,17 @@ static struct tm * _set_now(char buf[]) { struct tm tm; - char *endptr; time_t t; struct tm *tm_p; - if (strlen(buf) != 19) { - return NULL; - } - if (buf[4] != '-' || buf[7] != '-' || buf[10] != ' ' || - buf[13] != ':' || buf[16] != ':') { - return NULL; - } - buf[4] = '\0', buf[7] = '\0', buf[10] = '\0'; - buf[13] = '\0', buf[16] = '\0'; - tm.tm_sec = strtol(buf + 17, &endptr, 10); - if (endptr[0] != '\0') { return NULL; } - tm.tm_min = strtol(buf + 14, &endptr, 10); - if (endptr[0] != '\0') { return NULL; } - tm.tm_hour = strtol(buf + 11, &endptr, 10); - if (endptr[0] != '\0') { return NULL; } - tm.tm_mday = strtol(buf + 8, &endptr, 10); - if (endptr[0] != '\0') { return NULL; } - tm.tm_mon = strtol(buf + 5, &endptr, 10) - 1; - if (endptr[0] != '\0') { return NULL; } - tm.tm_year = strtol(buf + 0, &endptr, 10) - 1900; - if (endptr[0] != '\0') { return NULL; } - tm.tm_wday = -1; - tm.tm_yday = -1; - tm.tm_isdst = -1; + sscanf(buf, "%04d-%02d-%02d %02d:%02d:%02d", + &tm.tm_year, &tm.tm_mon, &tm.tm_mday, + &tm.tm_hour, &tm.tm_min, &tm.tm_sec); + tm.tm_year -= 1900; + tm.tm_mon -= 1; + tm.tm_wday = -1; + tm.tm_yday = -1; + tm.tm_isdst = -1; t = mktime(&tm); tm_p = localtime(&t); -- cgit v0.9.1