commit ade73bc35850f795c2e04562155f648f4ab83e7c
parent f9f3d13b1913b8895c34b16dea1b19f120753b63
Author: Claudio Alessi <smoppy@gmail.com>
Date: Fri, 17 Sep 2021 18:33:12 +0200
Merge pull request #15 from bsprober/clock-gettime+const-tick
employ clock_gettime to calculate elapsed time.
Diffstat:
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/snore.c b/snore.c
@@ -7,8 +7,8 @@
#include <string.h>
#include <time.h>
-#define TICK 0250000
-#define DELTA ((double)TICK / 1000000)
+#define BILLION 1000000000.0;
+#define TICK 10000
#define CLEAR "\33[2K\r"
#define LENGTH(X) (sizeof X / sizeof X[0])
#define ISCHR(c) (c >= 'a' && c <= 'z')
@@ -103,9 +103,11 @@ time_print(double tm) {
int
main(int argc, char *argv[]) {
+ struct timespec start, current;
double endtm = 0, tm;
int i;
+ clock_gettime(CLOCK_REALTIME, &start);
if(argc == 2 && !strcmp("-v", argv[1]))
die("snore-"VERSION"\n");
if(argc == 1) {
@@ -121,13 +123,16 @@ main(int argc, char *argv[]) {
die("%s: time too large\n", argv[0]);
}
}
- for(tm = 0; tm < endtm; tm += DELTA) {
+
+ for(tm = 0; tm < endtm; ) {
time_print(tm); /* ascending */
printf(" | ");
time_print(endtm - tm); /* descending */
fflush(stdout);
sleepu(TICK);
printf(CLEAR);
+ clock_gettime(CLOCK_REALTIME, ¤t);
+ tm = (current.tv_sec - start.tv_sec) + (current.tv_nsec - start.tv_nsec) / BILLION;
}
time_print(tm);
printf("\n");