Xinqi Bao's Git

Add warn() and die()
authorLaslo Hunhold <[email protected]>
Fri, 18 May 2018 08:59:05 +0000 (10:59 +0200)
committerAaron Marcher <[email protected]>
Fri, 18 May 2018 09:13:05 +0000 (11:13 +0200)
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.

20 files changed:
components/battery.c
components/cpu.c
components/datetime.c
components/disk.c
components/hostname.c
components/ip.c
components/kernel_release.c
components/keyboard_indicators.c
components/load_avg.c
components/num_files.c
components/run_command.c
components/swap.c
components/temperature.c
components/uptime.c
components/user.c
components/volume.c
components/wifi.c
slstatus.c
util.c
util.h

index 49f43e6..807a7e6 100644 (file)
 
                fd = open("/dev/apm", O_RDONLY);
                if (fd < 0) {
 
                fd = open("/dev/apm", O_RDONLY);
                if (fd < 0) {
-                       fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
+                       warn("open '/dev/apm':");
                        return NULL;
                }
 
                if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
                        return NULL;
                }
 
                if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
-                       fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
-                               strerror(errno));
+                       warn("ioctl 'APM_IOC_GETPOWER':");
                        close(fd);
                        return NULL;
                }
                        close(fd);
                        return NULL;
                }
 
                fd = open("/dev/apm", O_RDONLY);
                if (fd < 0) {
 
                fd = open("/dev/apm", O_RDONLY);
                if (fd < 0) {
-                       fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
+                       warn("open '/dev/apm':");
                        return NULL;
                }
 
                if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
                        return NULL;
                }
 
                if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
-                       fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
-                               strerror(errno));
+                       warn("ioctl 'APM_IOC_GETPOWER':");
                        close(fd);
                        return NULL;
                }
                        close(fd);
                        return NULL;
                }
index 02e7671..b9b3924 100644 (file)
@@ -57,7 +57,7 @@
                size = sizeof(freq);
 
                if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
                size = sizeof(freq);
 
                if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
-                       fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno));
+                       warn("sysctl 'HW_CPUSPEED':");
                        return NULL;
                }
 
                        return NULL;
                }
 
@@ -80,7 +80,7 @@
 
                memcpy(b, a, sizeof(b));
                if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
 
                memcpy(b, a, sizeof(b));
                if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
-                       fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno));
+                       warn("sysctl 'KERN_CPTIME':");
                        return NULL;
                }
                if (!valid) {
                        return NULL;
                }
                if (!valid) {
index 12d7717..c3efae3 100644 (file)
@@ -11,7 +11,7 @@ datetime(const char *fmt)
 
        t = time(NULL);
        if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) {
 
        t = time(NULL);
        if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) {
-               fprintf(stderr, "strftime: Result string exceeds buffer size\n");
+               warn("strftime: Result string exceeds buffer size");
                return NULL;
        }
 
                return NULL;
        }
 
index bf69875..f4031ea 100644 (file)
@@ -12,7 +12,7 @@ disk_free(const char *mnt)
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
-               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+               warn("statvfs '%s':", mnt);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -26,7 +26,7 @@ disk_perc(const char *mnt)
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
-               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+               warn("statvfs '%s':", mnt);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -40,7 +40,7 @@ disk_total(const char *mnt)
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
-               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+               warn("statvfs '%s':", mnt);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -54,7 +54,7 @@ disk_used(const char *mnt)
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
-               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+               warn("statvfs '%s':", mnt);
                return NULL;
        }
 
                return NULL;
        }
 
index d41465f..dc3bbf1 100644 (file)
@@ -10,7 +10,7 @@ const char *
 hostname(void)
 {
        if (gethostname(buf, sizeof(buf)) < 0) {
 hostname(void)
 {
        if (gethostname(buf, sizeof(buf)) < 0) {
-               fprintf(stderr, "gethostbyname: %s\n", strerror(errno));
+               warn("gethostbyname:");
                return NULL;
        }
 
                return NULL;
        }
 
index 85ac15e..fce2b66 100644 (file)
@@ -19,7 +19,7 @@ ipv4(const char *iface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) < 0) {
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) < 0) {
-               fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
+               warn("getifaddrs:");
                return NULL;
        }
 
                return NULL;
        }
 
@@ -32,7 +32,7 @@ ipv4(const char *iface)
                if (!strcmp(ifa->ifa_name, iface) &&
                    (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
                if (!strcmp(ifa->ifa_name, iface) &&
                    (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
-                               fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
+                               warn("getnameinfo: %s", gai_strerror(s));
                                return NULL;
                        }
                        return bprintf("%s", host);
                                return NULL;
                        }
                        return bprintf("%s", host);
@@ -52,7 +52,7 @@ ipv6(const char *iface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) < 0) {
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) < 0) {
-               fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
+               warn("getifaddrs:");
                return NULL;
        }
 
                return NULL;
        }
 
@@ -65,7 +65,7 @@ ipv6(const char *iface)
                if (!strcmp(ifa->ifa_name, iface) &&
                    (ifa->ifa_addr->sa_family == AF_INET6)) {
                        if (s != 0) {
                if (!strcmp(ifa->ifa_name, iface) &&
                    (ifa->ifa_addr->sa_family == AF_INET6)) {
                        if (s != 0) {
-                               fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
+                               warn("getnameinfo: %s", gai_strerror(s));
                                return NULL;
                        }
                        return bprintf("%s", host);
                                return NULL;
                        }
                        return bprintf("%s", host);
index 4e67a28..531014c 100644 (file)
@@ -12,7 +12,7 @@ kernel_release(void)
        struct utsname udata;
 
        if (uname(&udata) < 0) {
        struct utsname udata;
 
        if (uname(&udata) < 0) {
-               fprintf(stderr, "uname: %s\n", strerror(errno));
+               warn("uname:");
                return NULL;
        }
 
                return NULL;
        }
 
index 2f831c8..73ba32e 100644 (file)
@@ -11,7 +11,7 @@ keyboard_indicators(void)
        XKeyboardState state;
 
        if (!(dpy = XOpenDisplay(NULL))) {
        XKeyboardState state;
 
        if (!(dpy = XOpenDisplay(NULL))) {
-               fprintf(stderr, "Cannot open display\n");
+               warn("XOpenDisplay: Failed to open display");
                return NULL;
        }
        XGetKeyboardControl(dpy, &state);
                return NULL;
        }
        XGetKeyboardControl(dpy, &state);
index 526dc71..ea7bc2c 100644 (file)
@@ -10,7 +10,7 @@ load_avg(const char *fmt)
        double avgs[3];
 
        if (getloadavg(avgs, 3) < 0) {
        double avgs[3];
 
        if (getloadavg(avgs, 3) < 0) {
-               fprintf(stderr, "getloadavg: Could not obtain load average.\n");
+               warn("getloadavg: Failed to obtain load average");
                return NULL;
        }
 
                return NULL;
        }
 
index 327a64d..9179037 100644 (file)
@@ -14,7 +14,7 @@ num_files(const char *dir)
        int num;
 
        if (!(fd = opendir(dir))) {
        int num;
 
        if (!(fd = opendir(dir))) {
-               fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
+               warn("opendir '%s':", dir);
                return NULL;
        }
 
                return NULL;
        }
 
index 1aaae20..b5eeff0 100644 (file)
@@ -12,7 +12,7 @@ run_command(const char *cmd)
        FILE *fp;
 
        if (!(fp = popen(cmd, "r"))) {
        FILE *fp;
 
        if (!(fp = popen(cmd, "r"))) {
-               fprintf(stderr, "popen '%s': %s\n", cmd, strerror(errno));
+               warn("popen '%s':", cmd);
                return NULL;
        }
        p = fgets(buf, sizeof(buf) - 1, fp);
                return NULL;
        }
        p = fgets(buf, sizeof(buf) - 1, fp);
index caa4788..234e7d1 100644 (file)
                size_t bytes_read;
 
                if (!(fp = fopen(path, "r"))) {
                size_t bytes_read;
 
                if (!(fp = fopen(path, "r"))) {
-                       fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
+                       warn("fopen '%s':", path);
                        return 0;
                }
                if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
                        return 0;
                }
                if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
-                       fprintf(stderr, "fread '%s': %s\n", path, strerror(errno));
+                       warn("fread '%s':", path);
                        fclose(fp);
                        return 0;
                }
                        fclose(fp);
                        return 0;
                }
 
                nswap = swapctl(SWAP_NSWAP, 0, 0);
                if (nswap < 1) {
 
                nswap = swapctl(SWAP_NSWAP, 0, 0);
                if (nswap < 1) {
-                       fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno));
+                       warn("swaptctl 'SWAP_NSWAP':");
                }
 
                fsep = sep = calloc(nswap, sizeof(*sep));
                if (!sep) {
                }
 
                fsep = sep = calloc(nswap, sizeof(*sep));
                if (!sep) {
-                       fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
+                       warn("calloc 'nswap':");
                }
 
                rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
                if (rnswap < 0) {
                }
 
                rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
                if (rnswap < 0) {
-                       fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno));
+                       warn("swapctl 'SWAP_STATA':");
                }
 
                if (nswap != rnswap) {
                }
 
                if (nswap != rnswap) {
-                       fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
+                       warn("getstats: SWAP_STATS != SWAP_NSWAP");
                }
 
                *total = 0;
                }
 
                *total = 0;
index 812ae77..64199f5 100644 (file)
@@ -36,8 +36,7 @@
                size = sizeof(temp);
 
                if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
                size = sizeof(temp);
 
                if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
-                       fprintf(stderr, "sysctl 'SENSOR_TEMP': %s\n",
-                               strerror(errno));
+                       warn("sysctl 'SENSOR_TEMP':");
                        return NULL;
                }
 
                        return NULL;
                }
 
index 981f3cd..45e12db 100644 (file)
@@ -50,7 +50,7 @@ format(int uptime)
                size = sizeof(boottime);
 
                if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
                size = sizeof(boottime);
 
                if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
-                       fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
+                       warn("sysctl 'KERN_BOOTTIME':");
                        return NULL;
                }
 
                        return NULL;
                }
 
index 8ab6db9..cd503f6 100644 (file)
@@ -20,7 +20,7 @@ username(void)
        struct passwd *pw;
 
        if (!(pw = getpwuid(geteuid()))) {
        struct passwd *pw;
 
        if (!(pw = getpwuid(geteuid()))) {
-               fprintf(stderr, "getpwuid '%d': %s\n", geteuid(), strerror(errno));
+               warn("getpwuid '%d':", geteuid());
                return NULL;
        }
 
                return NULL;
        }
 
index aa46446..8674211 100644 (file)
@@ -21,21 +21,19 @@ vol_perc(const char *card)
        char *vnames[] = SOUND_DEVICE_NAMES;
 
        if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
        char *vnames[] = SOUND_DEVICE_NAMES;
 
        if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
-               fprintf(stderr, "open '%s': %s\n", card, strerror(errno));
+               warn("open '%s':", card);
                return NULL;
        }
 
        if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
                return NULL;
        }
 
        if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
-               fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n",
-                       strerror(errno));
+               warn("ioctl 'SOUND_MIXER_READ_DEVMASK':");
                close(afd);
                return NULL;
        }
        for (i = 0; i < LEN(vnames); i++) {
                if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
                        if (ioctl(afd, MIXER_READ(i), &v) < 0) {
                close(afd);
                return NULL;
        }
        for (i = 0; i < LEN(vnames); i++) {
                if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
                        if (ioctl(afd, MIXER_READ(i), &v) < 0) {
-                               fprintf(stderr, "ioctl 'MIXER_READ(%ld)': %s\n", i,
-                                       strerror(errno));
+                               warn("ioctl 'MIXER_READ(%ld)':", i);
                                close(afd);
                                return NULL;
                        }
                                close(afd);
                                return NULL;
                        }
index 414d533..591f6ad 100644 (file)
@@ -26,8 +26,7 @@
                snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
                         "/operstate");
                if (!(fp = fopen(path, "r"))) {
                snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
                         "/operstate");
                if (!(fp = fopen(path, "r"))) {
-                       fprintf(stderr, "fopen '%s': %s\n", path,
-                               strerror(errno));
+                       warn("fopen '%s':", path);
                        return NULL;
                }
                p = fgets(status, 5, fp);
                        return NULL;
                }
                p = fgets(status, 5, fp);
@@ -37,8 +36,7 @@
                }
 
                if (!(fp = fopen("/proc/net/wireless", "r"))) {
                }
 
                if (!(fp = fopen("/proc/net/wireless", "r"))) {
-                       fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
-                               strerror(errno));
+                       warn("fopen '/proc/net/wireless':");
                        return NULL;
                }
 
                        return NULL;
                }
 
                snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
 
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
 
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-                       fprintf(stderr, "socket 'AF_INET': %s\n",
-                               strerror(errno));
+                       warn("socket 'AF_INET':");
                        return NULL;
                }
                wreq.u.essid.pointer = id;
                if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
                        return NULL;
                }
                wreq.u.essid.pointer = id;
                if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
-                       fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
+                       warn("ioctl 'SIOCGIWESSID':");
                        close(sockfd);
                        return NULL;
                }
                        close(sockfd);
                        return NULL;
                }
                memset(&bssid, 0, sizeof(bssid));
                memset(nr, 0, sizeof(struct ieee80211_nodereq));
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                memset(&bssid, 0, sizeof(bssid));
                memset(nr, 0, sizeof(struct ieee80211_nodereq));
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-                       fprintf(stderr, "socket 'AF_INET': %s\n",
-                               strerror(errno));
+                       warn("socket 'AF_INET':");
                        return 0;
                }
                strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
                if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
                        return 0;
                }
                strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
                if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
-                       fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
-                               strerror(errno));
+                       warn("ioctl 'SIOCG80211BSSID':");
                        close(sockfd);
                        return 0;
                }
                strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
                memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
                if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
                        close(sockfd);
                        return 0;
                }
                strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
                memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
                if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
-                       fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
-                               strerror(errno));
+                       warn("ioctl 'SIOCG80211NODE':");
                        close(sockfd);
                        return 0;
                }
                        close(sockfd);
                        return 0;
                }
index 7ff323f..0c4605f 100644 (file)
@@ -17,7 +17,6 @@ struct arg {
        const char *args;
 };
 
        const char *args;
 };
 
-char *argv0;
 char buf[1024];
 static int done;
 static Display *dpy;
 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)
 {
 static void
 usage(void)
 {
-       fprintf(stderr, "usage: %s [-s]\n", argv0);
-       exit(1);
+       die("usage: %s [-s]", argv0);
 }
 
 int
 }
 
 int
@@ -80,14 +78,12 @@ main(int argc, char *argv[])
        }
 
        if (!sflag && !(dpy = XOpenDisplay(NULL))) {
        }
 
        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) {
        }
 
        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';
                }
 
                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) {
                        }
                        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) {
                                break;
                        } else if ((size_t)ret >= sizeof(status) - len) {
-                               fprintf(stderr, "snprintf: Output truncated\n");
+                               warn("snprintf: Output truncated");
                                break;
                        }
                        len += ret;
                                break;
                        }
                        len += ret;
@@ -111,18 +106,14 @@ main(int argc, char *argv[])
                        printf("%s\n", status);
                } else {
                        if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) {
                        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, &current) < 0) {
                        }
                        XFlush(dpy);
                }
 
                if (!done) {
                        if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
-                               fprintf(stderr, "clock_gettime: %s\n",
-                                       strerror(errno));
-                               return 1;
+                               die("clock_gettime:");
                        }
                        difftimespec(&diff, &current, &start);
 
                        }
                        difftimespec(&diff, &current, &start);
 
@@ -133,9 +124,7 @@ main(int argc, char *argv[])
                        if (wait.tv_sec >= 0) {
                                if (nanosleep(&wait, NULL) < 0 &&
                                    errno != EINTR) {
                        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) {
        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");
                }
        }
 
                }
        }
 
diff --git a/util.c b/util.c
index 6113049..08f14ff 100644 (file)
--- a/util.c
+++ b/util.c
@@ -2,10 +2,52 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "util.h"
 
 #include <string.h>
 
 #include "util.h"
 
+char *argv0;
+
+static void
+verr(const char *fmt, va_list ap)
+{
+       if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) {
+               fprintf(stderr, "%s: ", argv0);
+       }
+
+       vfprintf(stderr, fmt, ap);
+
+       if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
+               fputc(' ', stderr);
+               perror(NULL);
+       } else {
+               fputc('\n', stderr);
+       }
+}
+
+void
+warn(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       verr(fmt, ap);
+       va_end(ap);
+}
+
+void
+die(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       verr(fmt, ap);
+       va_end(ap);
+
+       exit(1);
+}
+
 const char *
 bprintf(const char *fmt, ...)
 {
 const char *
 bprintf(const char *fmt, ...)
 {
@@ -14,9 +56,9 @@ bprintf(const char *fmt, ...)
 
        va_start(ap, fmt);
        if ((ret = vsnprintf(buf, sizeof(buf), fmt, ap)) < 0) {
 
        va_start(ap, fmt);
        if ((ret = vsnprintf(buf, sizeof(buf), fmt, ap)) < 0) {
-               fprintf(stderr, "vsnprintf: %s\n", strerror(errno));
+               warn("vsnprintf:");
        } else if ((size_t)ret >= sizeof(buf)) {
        } else if ((size_t)ret >= sizeof(buf)) {
-               fprintf(stderr, "vsnprintf: Output truncated\n");
+               warn("vsnprintf: Output truncated");
        }
        va_end(ap);
 
        }
        va_end(ap);
 
@@ -31,7 +73,7 @@ pscanf(const char *path, const char *fmt, ...)
        int n;
 
        if (!(fp = fopen(path, "r"))) {
        int n;
 
        if (!(fp = fopen(path, "r"))) {
-               fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
+               warn("fopen '%s':", path);
                return -1;
        }
        va_start(ap, fmt);
                return -1;
        }
        va_start(ap, fmt);
diff --git a/util.h b/util.h
index e90c29d..a73b103 100644 (file)
--- a/util.h
+++ b/util.h
@@ -3,5 +3,10 @@ extern char buf[1024];
 
 #define LEN(x) (sizeof (x) / sizeof *(x))
 
 
 #define LEN(x) (sizeof (x) / sizeof *(x))
 
+extern char *argv0;
+
+void warn(const char *, ...);
+void die(const char *, ...);
+
 const char *bprintf(const char *fmt, ...);
 int pscanf(const char *path, const char *fmt, ...);
 const char *bprintf(const char *fmt, ...);
 int pscanf(const char *path, const char *fmt, ...);