From 7c961933f9705b732372b22799f56f96b3864e58 Mon Sep 17 00:00:00 2001 From: uqs Date: Mon, 2 Aug 2010 12:15:22 +0000 Subject: grdc(6): fix timekeeping for user-supplied value n - Keep timespec 'now' and 'delay' separate to avoid confusion - Increase user-supplied n to run _at least_ n seconds, not max n PR: bin/149130 (based on) Submitted by: Andy Farkas MFC after: 2 weeks --- games/grdc/grdc.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'games') diff --git a/games/grdc/grdc.c b/games/grdc/grdc.c index 7586be6..99960c8 100644 --- a/games/grdc/grdc.c +++ b/games/grdc/grdc.c @@ -29,7 +29,7 @@ #define YDEPTH 7 /* it won't be */ -time_t now; /* yeah! */ +struct timespec now; /* yeah! */ struct tm *tm; short disp[11] = { @@ -57,7 +57,7 @@ sighndl(int signo) int main(int argc, char *argv[]) { - struct timespec ts; + struct timespec delay; long t, a; int i, j, s, k; int n; @@ -89,7 +89,7 @@ main(int argc, char *argv[]) } if (argc > 0) - n = atoi(*argv); + n = atoi(*argv) + 1; else n = 0; @@ -135,10 +135,10 @@ main(int argc, char *argv[]) attrset(COLOR_PAIR(2)); } - time(&now); + clock_gettime(CLOCK_REALTIME_FAST, &now); do { mask = 0; - tm = localtime(&now); + tm = localtime(&now.tv_sec); set(tm->tm_sec%10, 0); set(tm->tm_sec/10, 4); set(tm->tm_min%10, 10); @@ -192,19 +192,16 @@ main(int argc, char *argv[]) } movto(6, 0); refresh(); - clock_gettime(CLOCK_REALTIME_FAST, &ts); - if (ts.tv_sec == now) { - if (ts.tv_nsec > 0) { - ts.tv_sec = 0; - ts.tv_nsec = 1000000000 - ts.tv_nsec; - } else { - ts.tv_sec = 1; - ts.tv_nsec = 0; - } - nanosleep(&ts, NULL); - now = ts.tv_sec + 1; - } else - now = ts.tv_sec; + clock_gettime(CLOCK_REALTIME_FAST, &now); + if (delay.tv_nsec > 0) { + delay.tv_sec = 0; + delay.tv_nsec = 1000000000 - now.tv_nsec; + } else { + delay.tv_sec = 1; + delay.tv_nsec = 0; + } + nanosleep(&delay, NULL); + now.tv_sec++; if (sigtermed) { standend(); clear(); -- cgit v1.1