diff options
author | Peter Wu <peter@lekensteyn.nl> | 2018-12-06 12:27:04 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2018-12-06 12:27:04 +0100 |
commit | 4bae61731dd6290c7b15027ebe272d953ee57850 (patch) | |
tree | 6e7ae52edc1e08429256eee97558cbe2477959c5 /timeadd.c | |
parent | a77f2fc042a3bd84d8cadae6e877d928e91db3c7 (diff) | |
download | c-files-master.tar.gz |
Diffstat (limited to 'timeadd.c')
-rw-r--r-- | timeadd.c | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -14,7 +14,8 @@ static void print_usage(const char *program_name) { fprintf(stderr, "Usage: %s [-d]\n", program_name); fputs("Options:\n" - " -d Display time difference instead of a monotonic clock\n", + " -d Display time difference instead of a monotonic clock\n" + " -z Start the monotonic clock at zero.\n", stderr); } @@ -40,12 +41,16 @@ int main(int argc, char **argv) { int tp_i = 0; int opt; bool diff = false; + bool zero = false; - while ((opt = getopt(argc, argv, "d")) != -1) { + while ((opt = getopt(argc, argv, "dz")) != -1) { switch (opt) { case 'd': diff = true; break; + case 'z': + zero = true; + break; default: print_usage(argv[0]); return 1; @@ -58,6 +63,11 @@ int main(int argc, char **argv) { out_format = "[%6llu.%06lu] %s\n"; } + if (zero) { + /* Use zero as reference. */ + clock_gettime(CLOCK_MONOTONIC, &tp[1]); + } + while (fgets(buf, sizeof(buf), stdin) != NULL) { char *newline = strrchr(buf, '\n'); @@ -74,6 +84,8 @@ int main(int argc, char **argv) { * inverse "tp_i" points to the new time (in the next * iteration it becomes the old time) */ tp_i ^= 1; + } else if (zero) { + tp[tp_i] = timediff(tp[tp_i ^ 1], tp[tp_i]); } printf(out_format, |