-
Notifications
You must be signed in to change notification settings - Fork 11
/
snore.c
138 lines (123 loc) · 2.78 KB
/
snore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* See LICENSE for license details. */
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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')
typedef struct symbol_t {
char sym;
int mult;
int precision;
} Symbol;
void die(const char *errstr, ...);
int sleepu(double usec);
double time_to_sec(char *s);
void time_print(double tm);
/* must be in ascending order */
static Symbol symbols[] = {
/* symbol multiplier precision */
{ 's', 1, 3 }, /* first is default (if no suffix) */
{ 'm', 60, 0 },
{ 'h', 3600, 0 },
{ 'd', 86400, 0 }, /* last is default (if no arguments) */
};
void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(1);
}
int
sleepu(double usec) {
struct timespec req, rem;
int r;
req.tv_sec = 0;
req.tv_nsec = usec * 1000;
while((r = nanosleep(&req, &rem)) == -1 && errno == EINTR)
req = rem;
return r;
}
double
time_to_sec(char *s) {
double calculated = 0.0, part;
char *parse_end, *string_end;
int j;
string_end = s + strlen(s);
while(s < string_end) {
part = strtod(s, &parse_end);
if(parse_end == s) {
/* error parsing float */
return -1;
}
s = parse_end;
if(s < string_end && ISCHR(s[0])) {
for(j = 0; j < LENGTH(symbols); ++j) {
if(s[0] == symbols[j].sym) {
part *= symbols[j].mult;
if(part >= INT_MAX)
return -1;
s++;
break;
}
}
}
calculated += part;
}
return calculated;
}
void
time_print(double tm) {
double piece;
int i;
for(i = LENGTH(symbols) - 1; i >= 0; --i) {
piece = tm / symbols[i].mult;
if(!symbols[i].precision)
piece = (int)piece;
printf("%.*f%c%s", symbols[i].precision, piece, symbols[i].sym, i ? " " : "");
tm -= piece * symbols[i].mult;
}
}
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) {
endtm = symbols[LENGTH(symbols) - 1].mult;
}
else {
for(i = 1; i < argc; ++i) {
tm = time_to_sec(argv[i]);
if(tm < 0)
die("%s: wrong time\n", argv[i]);
endtm += tm;
if(endtm >= INT_MAX)
die("%s: time too large\n", argv[0]);
}
}
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");
return 0;
}