commit e7c2240215c4c4d83bb1a1bc6b243585eb24b6fb
parent bcf25d598ae5a40ce9cbf6599e8ba3c1c24f79dc
Author: Claudio Alessi <smoppy@gmail.com>
Date: Tue, 26 Jan 2016 23:35:49 +0100
Minor clean up.
Diffstat:
M | snore.c | | | 32 | ++++++++++++++++---------------- |
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/snore.c b/snore.c
@@ -9,6 +9,7 @@
#define DELTA ((double)TICK / 1000000)
#define SCLEAR "\r \r"
#define LENGTH(X) (sizeof X / sizeof X[0])
+#define ISCHR(c) (c >= 'a' && c <= 'z')
typedef struct symbol_t {
char sym;
@@ -26,7 +27,7 @@ static Symbol symbols[] = {
{ 's', 1, 3 }, /* first is default (if no suffix) */
{ 'm', 60, 0 },
{ 'h', 3600, 0 },
- { 'd', 86400, 0 },
+ { 'd', 86400, 0 }, /* last is default (if no arguments) */
};
void
@@ -46,7 +47,7 @@ time_to_sec(char *s) {
len = strlen(s);
for(i = 0; i < len; ++i) {
- if(s[i] < 'a' || s[i] > 'z')
+ if(!ISCHR(s[i]))
continue;
for(j = 0; j < LENGTH(symbols); ++j) {
if(s[i] == symbols[j].sym) {
@@ -70,7 +71,6 @@ time_print(double tm) {
int i;
char buf[10], *p;
- /* high to low */
for(i = LENGTH(symbols) - 1; i >= 0; --i) {
piece = tm / symbols[i].mult;
snprintf(buf, sizeof buf, "%f", piece);
@@ -91,23 +91,23 @@ main(int argc, char *argv[]) {
if(argc == 2 && !strcmp("-v", argv[1]))
die("snore-"VERSION", © 2016 Claudio Alessi, see LICENSE for details\n");
- endtm = 0;
- for(i = 1; i < argc; ++i) {
- tm = time_to_sec(argv[i]);
- if(tm < 0)
- die("%s: wrong time\n", argv[i]);
- endtm += time_to_sec(argv[i]);
- }
-
- if(argc == 1 && !endtm)
+ if(argc == 1) {
endtm = symbols[LENGTH(symbols) - 1].mult;
+ }
+ else {
+ endtm = 0;
+ for(i = 1; i < argc; ++i) {
+ tm = time_to_sec(argv[i]);
+ if(tm < 0)
+ die("%s: wrong time\n", argv[i]);
+ endtm += time_to_sec(argv[i]);
+ }
+ }
for(tm = 0; tm < endtm; tm += DELTA) {
time_print(tm); /* ascending */
- if(endtm) {
- printf(" | ");
- time_print(endtm - tm); /* descending */
- }
+ printf(" | ");
+ time_print(endtm - tm); /* descending */
fflush(stdout);
usleep(TICK);
printf("%s", SCLEAR);