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
This commit is contained in:
Ulrich Spörlein 2010-08-02 12:15:22 +00:00
parent 67a54762a1
commit de1070d4d8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=210755

View file

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