From 5a360d60e9d1e62ae49d5ef46f3cb97bfd1644de Mon Sep 17 00:00:00 2001 From: uqs Date: Tue, 3 Aug 2010 16:02:57 +0000 Subject: 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 --- games/grdc/grdc.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'games/grdc') 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 -#include -#include #include +#include #include -#ifndef NONPOSIX +#include #include -#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(); -- cgit v1.1