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
This commit is contained in:
Ulrich Spörlein 2010-08-03 16:02:57 +00:00
parent 04f3205755
commit 1c9d5fc21b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=210809

View File

@ -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();