diff options
author | uqs <uqs@FreeBSD.org> | 2010-08-03 16:02:57 +0000 |
---|---|---|
committer | uqs <uqs@FreeBSD.org> | 2010-08-03 16:02:57 +0000 |
commit | 5a360d60e9d1e62ae49d5ef46f3cb97bfd1644de (patch) | |
tree | 8b10f14f4628ad9740552c55d268d7c5d34f1876 /games | |
parent | c5df0ad3b83907de341d300f8bcc00054262fe57 (diff) | |
download | FreeBSD-src-5a360d60e9d1e62ae49d5ef46f3cb97bfd1644de.zip FreeBSD-src-5a360d60e9d1e62ae49d5ef46f3cb97bfd1644de.tar.gz |
grdc(6): Partial backout of r210755
The previous revision was flawed in numerous ways:
- the if condition would depend on garbage stack values
- grdc(6) would loop n times, not n seconds. This only makes
a difference if it takes more than 1 second to recalculate/redraw
the display.
- style(9) violations
The following change adds argument checking and tracks the elapsed time
between loops explicitly and will exit after roughly n seconds.
While here sort headers and remove bogus #ifndef
Submitted by: bde
MFC after: 2 weeks
Diffstat (limited to 'games')
-rw-r--r-- | games/grdc/grdc.c | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/games/grdc/grdc.c b/games/grdc/grdc.c index 99960c8..fa920da 100644 --- a/games/grdc/grdc.c +++ b/games/grdc/grdc.c @@ -15,21 +15,18 @@ */ #include <err.h> -#include <time.h> -#include <signal.h> #include <ncurses.h> +#include <signal.h> #include <stdlib.h> -#ifndef NONPOSIX +#include <time.h> #include <unistd.h> -#endif #define YBASE 10 #define XBASE 10 #define XLENGTH 58 #define YDEPTH 7 -/* it won't be */ -struct timespec now; /* yeah! */ +struct timespec now; struct tm *tm; short disp[11] = { @@ -88,9 +85,14 @@ main(int argc, char *argv[]) /* NOTREACHED */ } - if (argc > 0) + if (argc > 0) { n = atoi(*argv) + 1; - else + if (n < 1) { + warnx("number of seconds is out of range"); + usage(); + /* NOTREACHED */ + } + } else n = 0; initscr(); @@ -192,16 +194,20 @@ main(int argc, char *argv[]) } movto(6, 0); refresh(); - 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; + clock_gettime(CLOCK_REALTIME_FAST, &delay); + if (delay.tv_sec == now.tv_sec) { + if (delay.tv_nsec > 0) { + delay.tv_sec = 0; + delay.tv_nsec = 1000000000 - delay.tv_nsec; + } else { + delay.tv_sec = 1; + delay.tv_nsec = 0; + } + nanosleep(&delay, NULL); + clock_gettime(CLOCK_REALTIME_FAST, &delay); } - nanosleep(&delay, NULL); - now.tv_sec++; + n -= delay.tv_sec - now.tv_sec; + now.tv_sec = delay.tv_sec; if (sigtermed) { standend(); clear(); @@ -209,7 +215,7 @@ main(int argc, char *argv[]) endwin(); errx(1, "terminated by signal %d", (int)sigtermed); } - } while(--n); + } while (n); standend(); clear(); refresh(); |