On Thu, 01 Jul 2021 08:14:51 -0600, "Todd C. Miller" wrote:

> Aha, I see.  It is because delay can be a float.

Here's a diff to use nanosleep/setitimer instead of usleep/ualarm.
Is it worth it?

 - todd

Index: usr.bin/systat/engine.c
===================================================================
RCS file: /cvs/src/usr.bin/systat/engine.c,v
retrieving revision 1.28
diff -u -p -u -r1.28 engine.c
--- usr.bin/systat/engine.c     2 Jun 2021 08:32:22 -0000       1.28
+++ usr.bin/systat/engine.c     1 Jul 2021 14:49:41 -0000
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <term.h>
 #include <unistd.h>
+#include <math.h>
 #include <err.h>
 
 /* XXX These are defined in term.h and conflict with our variable names */
@@ -50,7 +51,9 @@ struct view_ent {
        TAILQ_ENTRY(view_ent) entries;
 };
 
-useconds_t udelay = 5000000;
+static struct timespec ts_delay = { 5, 0 };
+static struct itimerval it_delay = { { 0, 0 }, { 5, 0 } };
+
 int dispstart = 0;
 int humanreadable = 0;
 int interactive = 1;
@@ -1355,7 +1358,7 @@ engine_loop(int countmax)
                        read_view();
                        need_sort = 1;
                        gotsig_alarm = 0;
-                       ualarm(udelay, 0);
+                       setitimer(ITIMER_REAL, &it_delay, NULL);
                }
 
                if (need_sort) {
@@ -1366,7 +1369,7 @@ engine_loop(int countmax)
                        /* XXX if sort took too long */
                        if (gotsig_alarm) {
                                gotsig_alarm = 0;
-                               ualarm(udelay, 0);
+                               setitimer(ITIMER_REAL, &it_delay, NULL);
                        }
                }
 
@@ -1408,7 +1411,7 @@ engine_loop(int countmax)
                if (interactive && need_update == 0)
                        keyboard();
                else if (interactive == 0)
-                       usleep(udelay);
+                       nanosleep(&ts_delay, NULL);
        }
 
        if (rawmode == 0)
@@ -1456,4 +1459,17 @@ check_termcap(void)
                return(1);
 
        return(0);
+}
+
+void
+refresh_delay(double delay)
+{
+       double secs, frac;
+
+       frac = modf(delay, &secs);
+       ts_delay.tv_sec = secs;
+       ts_delay.tv_nsec = frac * 1000000000.0;
+       if (!timespecisset(&ts_delay))
+               ts_delay.tv_nsec = 1000000000;
+       TIMESPEC_TO_TIMEVAL(&it_delay.it_value, &ts_delay);
 }
Index: usr.bin/systat/engine.h
===================================================================
RCS file: /cvs/src/usr.bin/systat/engine.h,v
retrieving revision 1.13
diff -u -p -u -r1.13 engine.h
--- usr.bin/systat/engine.h     2 Jun 2021 08:32:22 -0000       1.13
+++ usr.bin/systat/engine.h     1 Jul 2021 14:30:40 -0000
@@ -145,6 +145,7 @@ void show_order(void);
 
 void setup_term(int maxpr);
 int check_termcap(void);
+void refresh_delay(double delay);
 
 void engine_initialize(void);
 void engine_loop(int countmax);
@@ -156,7 +157,6 @@ const char *message_set(const char *msg)
 void foreach_view(void (*callback)(field_view *));
 
 extern int sortdir;
-extern useconds_t udelay;
 extern int dispstart;
 extern int humanreadable;
 extern int interactive;
Index: usr.bin/systat/main.c
===================================================================
RCS file: /cvs/src/usr.bin/systat/main.c,v
retrieving revision 1.74
diff -u -p -u -r1.74 main.c
--- usr.bin/systat/main.c       2 Jun 2021 08:32:22 -0000       1.74
+++ usr.bin/systat/main.c       1 Jul 2021 14:43:30 -0000
@@ -332,7 +332,7 @@ cmd_delay(const char *buf)
        if (errstr != NULL)
                error("s: \"%s\": delay value is %s", buf, errstr);
        else {
-               udelay = (useconds_t)(del * 1000000);
+               refresh_delay(del);
                gotsig_alarm = 1;
                naptime = del;
        }
@@ -557,11 +557,8 @@ main(int argc, char *argv[])
                        errx(1, "\"%s\": delay value is %s", argv[1], errstr);
        }
 
-       udelay = (useconds_t)(delay * 1000000.0);
-       if (udelay < 1)
-               udelay = 1;
-
-       naptime = (double)udelay / 1000000.0;
+       refresh_delay(delay);
+       naptime = delay;
 
        gethostname(hostname, sizeof (hostname));
        gethz();

Reply via email to