Xinqi Bao's Git

Handle SIGUSR1 for forced refreshes
[slstatus.git] / slstatus.c
index 4f7a2fc..0806773 100644 (file)
@@ -18,7 +18,7 @@ struct arg {
 };
 
 char buf[1024];
 };
 
 char buf[1024];
-static int done;
+static volatile sig_atomic_t done;
 static Display *dpy;
 
 #include "config.h"
 static Display *dpy;
 
 #include "config.h"
@@ -26,9 +26,8 @@ static Display *dpy;
 static void
 terminate(const int signo)
 {
 static void
 terminate(const int signo)
 {
-       (void)signo;
-
-       done = 1;
+       if (signo != SIGUSR1)
+               done = 1;
 }
 
 static void
 }
 
 static void
@@ -36,7 +35,7 @@ difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
 {
        res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
        res->tv_nsec = a->tv_nsec - b->tv_nsec +
 {
        res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
        res->tv_nsec = a->tv_nsec - b->tv_nsec +
-                      (a->tv_nsec < b->tv_nsec) * 1000000000;
+                      (a->tv_nsec < b->tv_nsec) * 1E9;
 }
 
 static void
 }
 
 static void
@@ -72,10 +71,8 @@ main(int argc, char *argv[])
        act.sa_handler = terminate;
        sigaction(SIGINT,  &act, NULL);
        sigaction(SIGTERM, &act, NULL);
        act.sa_handler = terminate;
        sigaction(SIGINT,  &act, NULL);
        sigaction(SIGTERM, &act, NULL);
-
-       if (sflag) {
-               setbuf(stdout, NULL);
-       }
+       act.sa_flags |= SA_RESTART;
+       sigaction(SIGUSR1, &act, NULL);
 
        if (!sflag && !(dpy = XOpenDisplay(NULL))) {
                die("XOpenDisplay: Failed to open display");
 
        if (!sflag && !(dpy = XOpenDisplay(NULL))) {
                die("XOpenDisplay: Failed to open display");
@@ -99,11 +96,13 @@ main(int argc, char *argv[])
                }
 
                if (sflag) {
                }
 
                if (sflag) {
-                       if (printf("%s\n", status) < 0) {
-                               die("printf:");
-                       }
+                       puts(status);
+                       fflush(stdout);
+                       if (ferror(stdout))
+                               die("puts:");
                } else {
                } else {
-                       if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) {
+                       if (XStoreName(dpy, DefaultRootWindow(dpy), status)
+                            < 0) {
                                die("XStoreName: Allocation failed");
                        }
                        XFlush(dpy);
                                die("XStoreName: Allocation failed");
                        }
                        XFlush(dpy);
@@ -116,7 +115,7 @@ main(int argc, char *argv[])
                        difftimespec(&diff, &current, &start);
 
                        intspec.tv_sec = interval / 1000;
                        difftimespec(&diff, &current, &start);
 
                        intspec.tv_sec = interval / 1000;
-                       intspec.tv_nsec = (interval % 1000) * 1000000;
+                       intspec.tv_nsec = (interval % 1000) * 1E6;
                        difftimespec(&wait, &intspec, &diff);
 
                        if (wait.tv_sec >= 0) {
                        difftimespec(&wait, &intspec, &diff);
 
                        if (wait.tv_sec >= 0) {