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.
This commit is contained in:

committed by
Aaron Marcher

parent
a4fe8c9741
commit
80fc20d1d6
31
slstatus.c
31
slstatus.c
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user