From 087b29c53e13299e195942e48ae309817f4f1d93 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 22 Oct 2016 19:59:42 -0400 Subject: Check for POSIX timers before using them. --- parse.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'parse.c') diff --git a/parse.c b/parse.c index 5761dc3..023d414 100644 --- a/parse.c +++ b/parse.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1954,6 +1955,29 @@ void dump_cmdline(const struct cmdline *cmdline, bool verbose) { fputs("\n", stderr); } +/** + * Get the current time. + */ +static int parse_gettime(struct timespec *ts) { +#if _POSIX_TIMERS > 0 + int ret = clock_gettime(CLOCK_REALTIME, ts); + if (ret != 0) { + perror("clock_gettime()"); + } + return ret; +#else + struct timeval tv; + int ret = gettimeofday(&tv, NULL); + if (ret == 0) { + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000L; + } else { + perror("gettimeofday()"); + } + return ret; +#endif +} + /** * Parse the command line. */ @@ -1987,8 +2011,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { .just_info = false, }; - if (clock_gettime(CLOCK_REALTIME, &state.now) != 0) { - perror("clock_gettime()"); + if (parse_gettime(&state.now) != 0) { goto fail; } -- cgit v1.2.3