summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-10-22 19:59:42 -0400
committerTavian Barnes <tavianator@tavianator.com>2016-10-24 10:01:18 -0400
commit087b29c53e13299e195942e48ae309817f4f1d93 (patch)
tree6776cff9657f3affe0b05a423339ea76cf7bcbfe
parentdd3c2a552725362cc03d7f7f631a0c24618cf4d2 (diff)
downloadbfs-087b29c53e13299e195942e48ae309817f4f1d93.tar.xz
Check for POSIX timers before using them.
-rw-r--r--eval.c28
-rw-r--r--parse.c27
2 files changed, 48 insertions, 7 deletions
diff --git a/eval.c b/eval.c
index d66c9f6..4778b5d 100644
--- a/eval.c
+++ b/eval.c
@@ -777,6 +777,27 @@ bool eval_xtype(const struct expr *expr, struct eval_state *state) {
return false;
}
+#if _POSIX_MONOTONIC_CLOCK > 0
+# define BFS_CLOCK CLOCK_MONOTONIC
+#elif _POSIX_TIMERS > 0
+# define BFS_CLOCK CLOCK_REALTIME
+#endif
+
+/**
+ * Call clock_gettime(), if available.
+ */
+static int eval_gettime(struct timespec *ts) {
+#ifdef BFS_CLOCK
+ int ret = clock_gettime(BFS_CLOCK, ts);
+ if (ret != 0) {
+ perror("clock_gettime()");
+ }
+ return ret;
+#else
+ return -1;
+#endif
+}
+
/**
* Record the time that elapsed evaluating an expression.
*/
@@ -799,8 +820,7 @@ static bool eval_expr(struct expr *expr, struct eval_state *state) {
struct timespec start, end;
bool time = state->cmdline->debug & DEBUG_RATES;
if (time) {
- if (clock_gettime(CLOCK_MONOTONIC, &start) != 0) {
- perror("clock_gettime()");
+ if (eval_gettime(&start) != 0) {
time = false;
}
}
@@ -808,10 +828,8 @@ static bool eval_expr(struct expr *expr, struct eval_state *state) {
bool ret = expr->eval(expr, state);
if (time) {
- if (clock_gettime(CLOCK_MONOTONIC, &end) == 0) {
+ if (eval_gettime(&end) == 0) {
add_elapsed(expr, &start, &end);
- } else {
- perror("clock_gettime()");
}
}
diff --git a/parse.c b/parse.c
index 5761dc3..023d414 100644
--- a/parse.c
+++ b/parse.c
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
@@ -1955,6 +1956,29 @@ void dump_cmdline(const struct cmdline *cmdline, bool verbose) {
}
/**
+ * 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.
*/
struct cmdline *parse_cmdline(int argc, char *argv[]) {
@@ -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;
}