Hi,

This program can be simplified if we keep everything in a timespec and
use clock_gettime(2)'s CLOCK_REALTIME clock instead of gettimeofday(2).

The "now" timespec could be moved into main(), but I think that change
belongs alongside other general refactoring/tidying in a separate diff.

--
Scott Cheloha

Index: games/grdc/grdc.c
===================================================================
RCS file: /cvs/src/games/grdc/grdc.c,v
retrieving revision 1.27
diff -u -p -r1.27 grdc.c
--- games/grdc/grdc.c   13 Jul 2017 02:57:52 -0000      1.27
+++ games/grdc/grdc.c   10 Dec 2017 02:10:05 -0000
@@ -18,6 +18,7 @@
 #include <limits.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <time.h>
 #include <unistd.h>
 
 #define XLENGTH 58
@@ -61,8 +62,7 @@ main(int argc, char *argv[])
        int i, j, s, k;
        int scrol;
        int n = 0;
-       struct timeval nowtv, endtv;
-       struct timespec delay;
+       struct timespec delay, end;
        const char *errstr;
        long scroldelay = 50000000;
        int xbase;
@@ -119,10 +119,9 @@ main(int argc, char *argv[])
        curs_set(0);
        sigwinched = 1; /* force initial sizing */
 
-       gettimeofday(&nowtv, NULL);
-       TIMEVAL_TO_TIMESPEC(&nowtv, &now);
+       clock_gettime(CLOCK_REALTIME, &now);
        if (n)
-               endtv.tv_sec = nowtv.tv_sec + n - 1;
+               end.tv_sec = now.tv_sec + n - 1;
        do {
                if (sigwinched) {
                        sigwinched = 0;
@@ -205,8 +204,7 @@ main(int argc, char *argv[])
                                }
                        }
                        if (scrol && k <= 4) {
-                               gettimeofday(&nowtv, NULL);
-                               TIMEVAL_TO_TIMESPEC(&nowtv, &now);
+                               clock_gettime(CLOCK_REALTIME, &now);
                                delay.tv_sec = 0;
                                delay.tv_nsec = 1000000000 - now.tv_nsec
                                    - (4-k) * scroldelay;
@@ -217,8 +215,7 @@ main(int argc, char *argv[])
                }
                move(6, 0);
                refresh();
-               gettimeofday(&nowtv, NULL);
-               TIMEVAL_TO_TIMESPEC(&nowtv, &now);
+               clock_gettime(CLOCK_REALTIME, &now);
                delay.tv_sec = 0;
                delay.tv_nsec = (1000000000 - now.tv_nsec);
                /* want scrolling to END on the second */
@@ -234,7 +231,7 @@ main(int argc, char *argv[])
                        endwin();
                        errx(1, "terminated by signal %d", sigtermed);
                }
-       } while (n == 0 || nowtv.tv_sec < endtv.tv_sec);
+       } while (n == 0 || now.tv_sec < end.tv_sec);
        standend();
        clear();
        refresh();

Reply via email to