From 3139cbc56a08ac76bccfe223dd2669f3f080c927 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 7 Nov 2022 13:04:11 -0500 Subject: xtime: Move parse_gettime() to xgettime() --- src/parse.c | 26 ++------------------------ src/xtime.c | 16 ++++++++++++++++ src/xtime.h | 10 ++++++++++ 3 files changed, 28 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/parse.c b/src/parse.c index ef52cbe..a4cb4fc 100644 --- a/src/parse.c +++ b/src/parse.c @@ -3822,29 +3822,6 @@ static void dump_costs(const struct bfs_ctx *ctx) { bfs_debug(ctx, DEBUG_COST, "Probability: ~${ylw}%g%%${rs}\n", 100.0*expr->probability); } -/** - * Get the current time. - */ -static int parse_gettime(const struct bfs_ctx *ctx, struct timespec *ts) { -#if _POSIX_TIMERS > 0 - int ret = clock_gettime(CLOCK_REALTIME, ts); - if (ret != 0) { - bfs_perror(ctx, "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 { - bfs_perror(ctx, "gettimeofday()"); - } - return ret; -#endif -} - struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { struct bfs_ctx *ctx = bfs_ctx_new(); if (!ctx) { @@ -3933,7 +3910,8 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { ctx->strategy = BFTW_DFS; } - if (parse_gettime(ctx, &state.now) != 0) { + if (xgettime(&state.now) != 0) { + parse_perror(&state, "xgettime()"); goto fail; } diff --git a/src/xtime.c b/src/xtime.c index bcd4e66..153b267 100644 --- a/src/xtime.c +++ b/src/xtime.c @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include /** Whether tzset() has been called. */ static bool tz_is_set = false; @@ -321,3 +323,17 @@ invalid: error: return -1; } + +int xgettime(struct timespec *result) { +#if _POSIX_TIMERS > 0 + return clock_gettime(CLOCK_REALTIME, result); +#else + struct timeval tv; + int ret = gettimeofday(&tv, NULL); + if (ret == 0) { + result->tv_sec = tv.tv_sec; + result->tv_nsec = tv.tv_usec * 1000L; + } + return ret; +#endif +} diff --git a/src/xtime.h b/src/xtime.h index cca1428..b49cd04 100644 --- a/src/xtime.h +++ b/src/xtime.h @@ -83,4 +83,14 @@ int xtimegm(struct tm *tm, time_t *timep); */ int xgetdate(const char *str, struct timespec *result); +/** + * Get the current time. + * + * @param[out] result + * A pointer to the result. + * @return + * 0 on success, -1 on failure. + */ +int xgettime(struct timespec *result); + #endif // BFS_XTIME_H -- cgit v1.2.3