The code can be simplified by using clock_gettime(2)'s CLOCK_REALTIME
authortb <tb@openbsd.org>
Mon, 11 Dec 2017 23:33:44 +0000 (23:33 +0000)
committertb <tb@openbsd.org>
Mon, 11 Dec 2017 23:33:44 +0000 (23:33 +0000)
instead of gettimeofday(2).

From Scott Cheloha, ok jca

games/grdc/grdc.c

index 9c9ef05..d6a02e7 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: grdc.c,v 1.27 2017/07/13 02:57:52 tb Exp $    */
+/*     $OpenBSD: grdc.c,v 1.28 2017/12/11 23:33:44 tb Exp $    */
 /*
  *
  * Copyright 2002 Amos Shapir.  Public domain.
@@ -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();