X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/9c9ce8a125f35bab61c829bdd58bdf77b8352d5e..dd5bfc3e518a705aba95569537e785bb0d6d7005:/slstatus.c?ds=sidebyside diff --git a/slstatus.c b/slstatus.c index 9bd9e81..64da5cb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -18,7 +18,7 @@ struct arg { }; char buf[1024]; -static int done; +static volatile sig_atomic_t done; static Display *dpy; #include "config.h" @@ -26,9 +26,8 @@ static Display *dpy; static void terminate(const int signo) { - (void)signo; - - done = 1; + if (signo != SIGUSR1) + done = 1; } static void @@ -36,13 +35,13 @@ 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 + - (a->tv_nsec < b->tv_nsec) * 1000000000; + (a->tv_nsec < b->tv_nsec) * 1E9; } static void usage(void) { - die("usage: %s [-so]", argv0); + die("usage: %s [-s] [-1]", argv0); } int @@ -51,18 +50,18 @@ main(int argc, char *argv[]) struct sigaction act; struct timespec start, current, diff, intspec, wait; size_t i, len; - int sflag, oflag, ret; + int sflag, ret; char status[MAXLEN]; const char *res; - sflag = oflag = 0; + sflag = 0; ARGBEGIN { + case '1': + done = 1; + /* fallthrough */ case 's': sflag = 1; break; - case 'o': - oflag = 1; - break; default: usage(); } ARGEND @@ -75,16 +74,14 @@ main(int argc, char *argv[]) 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"); } - while (!done) { + do { if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) { die("clock_gettime:"); } @@ -102,20 +99,18 @@ main(int argc, char *argv[]) } if (sflag) { - if (printf("%s\n", status) < 0) { - die("printf:"); - } + puts(status); + fflush(stdout); + if (ferror(stdout)) + die("puts:"); } else { - if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) { + if (XStoreName(dpy, DefaultRootWindow(dpy), status) + < 0) { die("XStoreName: Allocation failed"); } XFlush(dpy); } - if (oflag) { - done = 1; - } - if (!done) { if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0) { die("clock_gettime:"); @@ -123,7 +118,7 @@ main(int argc, char *argv[]) difftimespec(&diff, ¤t, &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) { @@ -133,7 +128,7 @@ main(int argc, char *argv[]) } } } - } + } while (!done); if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL);