diff options
author | uqs <uqs@FreeBSD.org> | 2010-08-02 12:15:22 +0000 |
---|---|---|
committer | uqs <uqs@FreeBSD.org> | 2010-08-02 12:15:22 +0000 |
commit | 7c961933f9705b732372b22799f56f96b3864e58 (patch) | |
tree | 1506f21ae3677c5a6e37d0b3c1a6d283cb4c42ba /games | |
parent | ad97a57b125bacb5e8e039f1b807fc65d734b3fa (diff) | |
download | FreeBSD-src-7c961933f9705b732372b22799f56f96b3864e58.zip FreeBSD-src-7c961933f9705b732372b22799f56f96b3864e58.tar.gz |
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
Diffstat (limited to 'games')
-rw-r--r-- | games/grdc/grdc.c | 33 |
1 files changed, 15 insertions, 18 deletions
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(); |