Flush the output stream instead of arbitrarily disabling buffering
authorQuentin Rameau <quinq@fifth.space>
Tue, 29 May 2018 19:32:29 +0000 (21:32 +0200)
committerAaron Marcher <me@drkhsh.at>
Tue, 29 May 2018 20:26:05 +0000 (22:26 +0200)
This is (arguably) more elegant and it matches Xlib output logic.
Also use puts instead of printf as we don't do formating there.

slstatus.c

index 7776fd5a4115f33d1db1c4dde447d8eeb64bf170..e6aa90b654c0c7ea33d817741410c9e0b071c1b0 100644 (file)
@@ -73,10 +73,6 @@ main(int argc, char *argv[])
        sigaction(SIGINT,  &act, NULL);
        sigaction(SIGTERM, &act, NULL);
 
-       if (sflag) {
-               setbuf(stdout, NULL);
-       }
-
        if (!sflag && !(dpy = XOpenDisplay(NULL))) {
                die("XOpenDisplay: Failed to open display");
        }
@@ -99,9 +95,10 @@ 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) {
                                die("XStoreName: Allocation failed");