diff options
author | Laslo Hunhold <dev@frign.de> | 2018-05-18 10:59:05 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-18 11:13:05 +0200 |
commit | 80fc20d1d69b14f36ad9bb64d8af38481cbf1ff5 (patch) | |
tree | ecd06a739fc89f6041aa2d84073f5bc1e0a0bad9 /slstatus.c | |
parent | a4fe8c97414f07dd8b891e0d325dd2733195151d (diff) |
Add warn() and die()
Given slstatus is a tool that runs in the background, most likely run
from .xinitrc, it's important to prepend the name of the tool to error
messages so it becomes clear where the error is coming from.
To make this much more consistent, this commit adds warn() and die()
utility functions consistent with other suckless projects and adapts all
calls to fprintf(stderr, *) to the warn() and die() functions, greatly
increasing the readability of the code.
Diffstat (limited to 'slstatus.c')
-rw-r--r-- | slstatus.c | 31 |
1 files changed, 9 insertions, 22 deletions
@@ -17,7 +17,6 @@ struct arg { const char *args; }; -char *argv0; char buf[1024]; static int done; static Display *dpy; @@ -43,8 +42,7 @@ difftimespec(struct timespec *res, struct timespec *a, struct timespec *b) static void usage(void) { - fprintf(stderr, "usage: %s [-s]\n", argv0); - exit(1); + die("usage: %s [-s]", argv0); } int @@ -80,14 +78,12 @@ main(int argc, char *argv[]) } if (!sflag && !(dpy = XOpenDisplay(NULL))) { - fprintf(stderr, "XOpenDisplay: Failed to open display\n"); - return 1; + die("XOpenDisplay: Failed to open display"); } while (!done) { if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) { - fprintf(stderr, "clock_gettime: %s\n", strerror(errno)); - return 1; + die("clock_gettime:"); } status[0] = '\0'; @@ -97,11 +93,10 @@ main(int argc, char *argv[]) } if ((ret = snprintf(status + len, sizeof(status) - len, args[i].fmt, res)) < 0) { - fprintf(stderr, "snprintf: %s\n", - strerror(errno)); + warn("snprintf:"); break; } else if ((size_t)ret >= sizeof(status) - len) { - fprintf(stderr, "snprintf: Output truncated\n"); + warn("snprintf: Output truncated"); break; } len += ret; @@ -111,18 +106,14 @@ main(int argc, char *argv[]) printf("%s\n", status); } else { if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) { - fprintf(stderr, - "XStoreName: Allocation failed\n"); - return 1; + die("XStoreName: Allocation failed"); } XFlush(dpy); } if (!done) { if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0) { - fprintf(stderr, "clock_gettime: %s\n", - strerror(errno)); - return 1; + die("clock_gettime:"); } difftimespec(&diff, ¤t, &start); @@ -133,9 +124,7 @@ main(int argc, char *argv[]) if (wait.tv_sec >= 0) { if (nanosleep(&wait, NULL) < 0 && errno != EINTR) { - fprintf(stderr, "nanosleep: %s\n", - strerror(errno)); - return 1; + die("nanosleep:"); } } } @@ -144,9 +133,7 @@ main(int argc, char *argv[]) if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL); if (XCloseDisplay(dpy) < 0) { - fprintf(stderr, - "XCloseDisplay: Failed to close display\n"); - return 1; + die("XCloseDisplay: Failed to close display"); } } |