Following the worm(6) bug fix and improvement, make robots(6) do it as
well.

Changes:
 * Replace timeval with timespec structs;
 * Use clock_gettime(CLOCK_UPTIME) instead of gettimeofday();
 * Use timespec*() instead of manual math operations;
 * Use ppoll() instead of poll() + math operations;

No functionality changed, except that with this diff robots(6) won't
suffer with clock changes and suspend.

ok?

Index: extern.c
===================================================================
RCS file: /cvs/src/games/robots/extern.c,v
retrieving revision 1.6
diff -u -p -r1.6 extern.c
--- extern.c    3 Nov 2014 22:14:54 -0000       1.6
+++ extern.c    25 Aug 2015 01:22:36 -0000
@@ -62,7 +62,7 @@ int   Score;                  /* Current score */
 int    Start_level = 1;        /* Level on which to start */
 int    Wait_bonus;             /* bonus for waiting */
 
-struct timeval tv;             /* how long to wait; could be an option */
+struct timespec        tv;             /* how long to wait; could be an option 
*/
 
 COORD  Max;                    /* Max area robots take up */
 COORD  Min;                    /* Min area robots take up */
Index: main.c
===================================================================
RCS file: /cvs/src/games/robots/main.c,v
retrieving revision 1.19
diff -u -p -r1.19 main.c
--- main.c      3 Nov 2014 22:14:54 -0000       1.19
+++ main.c      25 Aug 2015 01:22:53 -0000
@@ -70,7 +70,6 @@ main(int ac, char *av[])
                        Real_time = TRUE;
                        /* Could be a command-line option */
                        tv.tv_sec = 3;
-                       tv.tv_usec = 0;
                        break;
                case 'a':
                        Start_level = 4;
Index: move.c
===================================================================
RCS file: /cvs/src/games/robots/move.c,v
retrieving revision 1.10
diff -u -p -r1.10 move.c
--- move.c      3 Nov 2014 22:14:54 -0000       1.10
+++ move.c      25 Aug 2015 02:01:19 -0000
@@ -43,8 +43,7 @@ get_move(void)
 {
        int     c;
        int retval;
-       struct timeval t, tod;
-       struct timezone tz;
+       struct timespec t, tn;
 #ifdef FANCY
        int lastmove;
 #endif
@@ -61,9 +60,8 @@ get_move(void)
        }
 #endif
        if (Real_time) {
-               t.tv_sec = tv.tv_sec;
-               t.tv_usec = tv.tv_usec;
-               (void)gettimeofday(&tod, &tz);
+               t = tv;
+               clock_gettime(CLOCK_UPTIME, &tn);
        }
        for (;;) {
                if (Teleport && must_telep())
@@ -94,8 +92,7 @@ over:
 
                                pfd[0].fd = STDIN_FILENO;
                                pfd[0].events = POLLIN;
-                               retval = poll(pfd, 1,
-                                   t.tv_sec * 1000 + t.tv_usec / 1000);
+                               retval = ppoll(pfd, 1, &t, NULL);
                                if (retval > 0)
                                        c = getchar();
                                else    /* Don't move if timed out or error */
@@ -203,15 +200,16 @@ teleport:
                        break;
                }
                if (Real_time) {
-                       (void)gettimeofday(&t, &tz);
-                       t.tv_sec = tod.tv_sec + tv.tv_sec - t.tv_sec;
-                       t.tv_usec = tod.tv_usec + tv.tv_usec - t.tv_usec;
-                       if (t.tv_usec < 0) {
-                               t.tv_sec--;
-                               t.tv_usec += 1000000;   /* Now it must be > 0 */
-                       }
-                       if (t.tv_sec < 0)
+                       /* Update current time. */
+                       clock_gettime(CLOCK_UPTIME, &t);
+
+                       /* Check whether tv time has passed. */
+                       timespecadd(&tn, &tv, &tn);
+                       if (timespeccmp(&tn, &t, <))
                                goto ret;
+
+                       /* Keep the difference otherwise. */
+                       timespecsub(&tn, &t, &t);
                }
        }
 ret:
Index: robots.h
===================================================================
RCS file: /cvs/src/games/robots/robots.h,v
retrieving revision 1.8
diff -u -p -r1.8 robots.h
--- robots.h    16 Nov 2014 04:49:48 -0000      1.8
+++ robots.h    25 Aug 2015 02:04:58 -0000
@@ -107,7 +107,7 @@ extern char Cnt_move, Field[Y_FIELDSIZE]
 extern int     Count, Level, Num_robots, Num_scores, Score,
                Start_level, Wait_bonus;
 
-extern struct timeval  tv;
+extern struct timespec tv;
 
 extern COORD   Max, Min, My_pos, Robots[];
 

Reply via email to