commit 9cd2003347a39ce91ed82a85aadb3268746ce500
parent 0cbde419d1c1143b053305d75be37d3926e72116
Author: Claudio Alessi <smoppy@gmail.com>
Date: Tue, 2 May 2017 23:29:17 +0200
Restore everything. Just replace usleep() with a nanosleep()-based implementation.
Diffstat:
M | snore.c | | | 34 | +++++++++++++--------------------- |
1 file changed, 13 insertions(+), 21 deletions(-)
diff --git a/snore.c b/snore.c
@@ -6,8 +6,8 @@
#include <string.h>
#include <time.h>
-#define TICK 1000
-#define DELTA ((double)TICK / 10000000)
+#define TICK 0250000
+#define DELTA ((double)TICK / 1000000)
#define CLEAR "\33[2K\r"
#define LENGTH(X) (sizeof X / sizeof X[0])
#define ISCHR(c) (c >= 'a' && c <= 'z')
@@ -19,8 +19,7 @@ typedef struct symbol_t {
} Symbol;
void die(const char *errstr, ...);
-int nsleep(long nsec);
-void snore(double usec);
+int sleepu(double usec);
double time_to_sec(char *s);
void time_print(double tm);
@@ -44,31 +43,17 @@ die(const char *errstr, ...) {
}
int
-nsleep(long nsec) {
+sleepu(double usec) {
struct timespec req, rem;
int r;
req.tv_sec = 0;
- req.tv_nsec = nsec;
+ req.tv_nsec = usec * 1000;
while((r = nanosleep(&req, &rem)) == -1 && errno == EINTR)
req = rem;
return r;
}
-void
-snore(double usec) {
- double tm;
-
- for(tm = 0; tm < usec; tm += DELTA) {
- time_print(tm); /* ascending */
- printf(" | ");
- time_print(usec - tm); /* descending */
- fflush(stdout);
- nsleep(TICK);
- printf("%s", CLEAR);
- }
-}
-
double
time_to_sec(char *s) {
double calculated = 0.0, part;
@@ -130,7 +115,14 @@ main(int argc, char *argv[]) {
}
if(!endtm)
endtm = symbols[LENGTH(symbols) - 1].mult;
- snore(endtm);
+ for(tm = 0; tm < endtm; tm += DELTA) {
+ time_print(tm); /* ascending */
+ printf(" | ");
+ time_print(endtm - tm); /* descending */
+ fflush(stdout);
+ sleepu(TICK);
+ printf("%s", CLEAR);
+ }
printf("\a%s elapsed\n", argv[1]);
return 0;
}