summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-05-18 10:59:05 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-18 11:13:05 +0200
commit80fc20d1d69b14f36ad9bb64d8af38481cbf1ff5 (patch)
treeecd06a739fc89f6041aa2d84073f5bc1e0a0bad9 /components
parenta4fe8c97414f07dd8b891e0d325dd2733195151d (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 'components')
-rw-r--r--components/battery.c10
-rw-r--r--components/cpu.c4
-rw-r--r--components/datetime.c2
-rw-r--r--components/disk.c8
-rw-r--r--components/hostname.c2
-rw-r--r--components/ip.c8
-rw-r--r--components/kernel_release.c2
-rw-r--r--components/keyboard_indicators.c2
-rw-r--r--components/load_avg.c2
-rw-r--r--components/num_files.c2
-rw-r--r--components/run_command.c2
-rw-r--r--components/swap.c12
-rw-r--r--components/temperature.c3
-rw-r--r--components/uptime.c2
-rw-r--r--components/user.c2
-rw-r--r--components/volume.c8
-rw-r--r--components/wifi.c20
17 files changed, 40 insertions, 51 deletions
diff --git a/components/battery.c b/components/battery.c
index 49f43e6..807a7e6 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -59,13 +59,12 @@
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) {
- fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
- strerror(errno));
+ warn("ioctl 'APM_IOC_GETPOWER':");
close(fd);
return NULL;
}
@@ -90,13 +89,12 @@
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) {
- fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
- strerror(errno));
+ warn("ioctl 'APM_IOC_GETPOWER':");
close(fd);
return NULL;
}
diff --git a/components/cpu.c b/components/cpu.c
index 02e7671..b9b3924 100644
--- a/components/cpu.c
+++ b/components/cpu.c
@@ -57,7 +57,7 @@
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;
}
@@ -80,7 +80,7 @@
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) {
diff --git a/components/datetime.c b/components/datetime.c
index 12d7717..c3efae3 100644
--- a/components/datetime.c
+++ b/components/datetime.c
@@ -11,7 +11,7 @@ datetime(const char *fmt)
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;
}
diff --git a/components/disk.c b/components/disk.c
index bf69875..f4031ea 100644
--- a/components/disk.c
+++ b/components/disk.c
@@ -12,7 +12,7 @@ disk_free(const char *mnt)
struct statvfs fs;
if (statvfs(mnt, &fs) < 0) {
- fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+ warn("statvfs '%s':", mnt);
return NULL;
}
@@ -26,7 +26,7 @@ disk_perc(const char *mnt)
struct statvfs fs;
if (statvfs(mnt, &fs) < 0) {
- fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+ warn("statvfs '%s':", mnt);
return NULL;
}
@@ -40,7 +40,7 @@ disk_total(const char *mnt)
struct statvfs fs;
if (statvfs(mnt, &fs) < 0) {
- fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+ warn("statvfs '%s':", mnt);
return NULL;
}
@@ -54,7 +54,7 @@ disk_used(const char *mnt)
struct statvfs fs;
if (statvfs(mnt, &fs) < 0) {
- fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
+ warn("statvfs '%s':", mnt);
return NULL;
}
diff --git a/components/hostname.c b/components/hostname.c
index d41465f..dc3bbf1 100644
--- a/components/hostname.c
+++ b/components/hostname.c
@@ -10,7 +10,7 @@ const char *
hostname(void)
{
if (gethostname(buf, sizeof(buf)) < 0) {
- fprintf(stderr, "gethostbyname: %s\n", strerror(errno));
+ warn("gethostbyname:");
return NULL;
}
diff --git a/components/ip.c b/components/ip.c
index 85ac15e..fce2b66 100644
--- a/components/ip.c
+++ b/components/ip.c
@@ -19,7 +19,7 @@ ipv4(const char *iface)
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) < 0) {
- fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
+ warn("getifaddrs:");
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) {
- fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
+ warn("getnameinfo: %s", gai_strerror(s));
return NULL;
}
return bprintf("%s", host);
@@ -52,7 +52,7 @@ ipv6(const char *iface)
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) < 0) {
- fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
+ warn("getifaddrs:");
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) {
- fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
+ warn("getnameinfo: %s", gai_strerror(s));
return NULL;
}
return bprintf("%s", host);
diff --git a/components/kernel_release.c b/components/kernel_release.c
index 4e67a28..531014c 100644
--- a/components/kernel_release.c
+++ b/components/kernel_release.c
@@ -12,7 +12,7 @@ kernel_release(void)
struct utsname udata;
if (uname(&udata) < 0) {
- fprintf(stderr, "uname: %s\n", strerror(errno));
+ warn("uname:");
return NULL;
}
diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c
index 2f831c8..73ba32e 100644
--- a/components/keyboard_indicators.c
+++ b/components/keyboard_indicators.c
@@ -11,7 +11,7 @@ keyboard_indicators(void)
XKeyboardState state;
if (!(dpy = XOpenDisplay(NULL))) {
- fprintf(stderr, "Cannot open display\n");
+ warn("XOpenDisplay: Failed to open display");
return NULL;
}
XGetKeyboardControl(dpy, &state);
diff --git a/components/load_avg.c b/components/load_avg.c
index 526dc71..ea7bc2c 100644
--- a/components/load_avg.c
+++ b/components/load_avg.c
@@ -10,7 +10,7 @@ load_avg(const char *fmt)
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;
}
diff --git a/components/num_files.c b/components/num_files.c
index 327a64d..9179037 100644
--- a/components/num_files.c
+++ b/components/num_files.c
@@ -14,7 +14,7 @@ num_files(const char *dir)
int num;
if (!(fd = opendir(dir))) {
- fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
+ warn("opendir '%s':", dir);
return NULL;
}
diff --git a/components/run_command.c b/components/run_command.c
index 1aaae20..b5eeff0 100644
--- a/components/run_command.c
+++ b/components/run_command.c
@@ -12,7 +12,7 @@ run_command(const char *cmd)
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);
diff --git a/components/swap.c b/components/swap.c
index caa4788..234e7d1 100644
--- a/components/swap.c
+++ b/components/swap.c
@@ -13,11 +13,11 @@
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))) {
- fprintf(stderr, "fread '%s': %s\n", path, strerror(errno));
+ warn("fread '%s':", path);
fclose(fp);
return 0;
}
@@ -139,21 +139,21 @@
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) {
- fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
+ warn("calloc 'nswap':");
}
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) {
- fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
+ warn("getstats: SWAP_STATS != SWAP_NSWAP");
}
*total = 0;
diff --git a/components/temperature.c b/components/temperature.c
index 812ae77..64199f5 100644
--- a/components/temperature.c
+++ b/components/temperature.c
@@ -36,8 +36,7 @@
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;
}
diff --git a/components/uptime.c b/components/uptime.c
index 981f3cd..45e12db 100644
--- a/components/uptime.c
+++ b/components/uptime.c
@@ -50,7 +50,7 @@ format(int uptime)
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;
}
diff --git a/components/user.c b/components/user.c
index 8ab6db9..cd503f6 100644
--- a/components/user.c
+++ b/components/user.c
@@ -20,7 +20,7 @@ username(void)
struct passwd *pw;
if (!(pw = getpwuid(geteuid()))) {
- fprintf(stderr, "getpwuid '%d': %s\n", geteuid(), strerror(errno));
+ warn("getpwuid '%d':", geteuid());
return NULL;
}
diff --git a/components/volume.c b/components/volume.c
index aa46446..8674211 100644
--- a/components/volume.c
+++ b/components/volume.c
@@ -21,21 +21,19 @@ vol_perc(const char *card)
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) {
- 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) {
- fprintf(stderr, "ioctl 'MIXER_READ(%ld)': %s\n", i,
- strerror(errno));
+ warn("ioctl 'MIXER_READ(%ld)':", i);
close(afd);
return NULL;
}
diff --git a/components/wifi.c b/components/wifi.c
index 414d533..591f6ad 100644
--- a/components/wifi.c
+++ b/components/wifi.c
@@ -26,8 +26,7 @@
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);
@@ -37,8 +36,7 @@
}
if (!(fp = fopen("/proc/net/wireless", "r"))) {
- fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
- strerror(errno));
+ warn("fopen '/proc/net/wireless':");
return NULL;
}
@@ -74,13 +72,12 @@
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) {
- fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
+ warn("ioctl 'SIOCGIWESSID':");
close(sockfd);
return NULL;
}
@@ -111,22 +108,19 @@
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) {
- 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) {
- fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
- strerror(errno));
+ warn("ioctl 'SIOCG80211NODE':");
close(sockfd);
return 0;
}