Xinqi Bao's Git

Format error messages properly
authorAaron Marcher <[email protected]>
Wed, 28 Mar 2018 17:46:27 +0000 (19:46 +0200)
committerAaron Marcher <[email protected]>
Wed, 28 Mar 2018 17:46:27 +0000 (19:46 +0200)
Make use of strerror(errno) and format all errors equally:
function ['parameters']: error message

15 files changed:
components/battery.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/uptime.c
components/user.c
components/volume.c
components/wifi.c
util.c

index cd1169d..75f2945 100644 (file)
@@ -1,5 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 #if defined(__linux__)
 #include <limits.h>
 #include <string.h>
 #if defined(__linux__)
 #include <limits.h>
 #include <string.h>
@@ -28,12 +30,12 @@ battery_perc(const char *bat)
 
        fd = open("/dev/apm", O_RDONLY);
        if (fd < 0) {
 
        fd = open("/dev/apm", O_RDONLY);
        if (fd < 0) {
-               fprintf(stderr, "Failed to open file /dev/apm");
+               fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
                return NULL;
        }
 
        if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
                return NULL;
        }
 
        if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
-               fprintf(stderr, "Failed to get battery info");
+               fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno));
                close(fd);
                return NULL;
        }
                close(fd);
                return NULL;
        }
index 3d8140e..8a9caa1 100644 (file)
@@ -1,5 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/statvfs.h>
 
 #include "../util.h"
 #include <sys/statvfs.h>
 
 #include "../util.h"
@@ -10,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, "Failed to get filesystem info");
+               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -24,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, "Failed to get filesystem info");
+               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -39,7 +41,7 @@ disk_total(const char *mnt)
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
        struct statvfs fs;
 
        if (statvfs(mnt, &fs) < 0) {
-               fprintf(stderr, "Failed to get filesystem info");
+               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -52,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, "Failed to get filesystem info");
+               fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
index 45dbb5b..d7c10c8 100644 (file)
@@ -1,5 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "../util.h"
 #include <unistd.h>
 
 #include "../util.h"
@@ -8,7 +10,7 @@ const char *
 hostname(void)
 {
        if (gethostname(buf, sizeof(buf)) == -1) {
 hostname(void)
 {
        if (gethostname(buf, sizeof(buf)) == -1) {
-               fprintf(stderr, "gethostbyname failed");
+               fprintf(stderr, "gethostbyname: %s\n", strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
index c46ec9e..686344b 100644 (file)
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
+#include <errno.h>
 #include <ifaddrs.h>
 #include <netdb.h>
 #include <stdio.h>
 #include <ifaddrs.h>
 #include <netdb.h>
 #include <stdio.h>
@@ -15,7 +16,7 @@ ipv4(const char *iface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
-               fprintf(stderr, "Failed to get IPv4 address for interface %s", iface);
+               fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -26,7 +27,7 @@ ipv4(const char *iface)
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
-                               fprintf(stderr, "Failed to get IPv4 address for interface %s", iface);
+                               fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
                                return NULL;
                        }
                        return bprintf("%s", host);
                                return NULL;
                        }
                        return bprintf("%s", host);
@@ -46,7 +47,7 @@ ipv6(const char *iface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
-               fprintf(stderr, "Failed to get IPv6 address for interface %s", iface);
+               fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -57,7 +58,7 @@ ipv6(const char *iface)
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) {
                        if (s != 0) {
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) {
                        if (s != 0) {
-                               fprintf(stderr, "Failed to get IPv6 address for interface %s", iface);
+                               fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
                                return NULL;
                        }
                        return bprintf("%s", host);
                                return NULL;
                        }
                        return bprintf("%s", host);
index f539b6a..4e67a28 100644 (file)
@@ -1,6 +1,8 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <sys/utsname.h>
 #include <stdio.h>
 #include <sys/utsname.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "../util.h"
 
 
 #include "../util.h"
 
@@ -10,6 +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));
                return NULL;
        }
 
                return NULL;
        }
 
index 47b52c6..76cf17e 100644 (file)
@@ -11,7 +11,7 @@ keyboard_indicators(void)
        XKeyboardState state;
 
        if (dpy == NULL) {
        XKeyboardState state;
 
        if (dpy == NULL) {
-               fprintf(stderr, "Cannot open display");
+               fprintf(stderr, "Cannot open display\n");
                return NULL;
        }
        XGetKeyboardControl(dpy, &state);
                return NULL;
        }
        XGetKeyboardControl(dpy, &state);
index 5e1571d..526dc71 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, "Failed to get the load avg");
+               fprintf(stderr, "getloadavg: Could not obtain load average.\n");
                return NULL;
        }
 
                return NULL;
        }
 
index c471400..acf9dfa 100644 (file)
@@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <dirent.h>
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
 #include <stdio.h>
 #include <string.h>
@@ -13,7 +14,7 @@ num_files(const char *dir)
        int num = 0;
 
        if ((fd = opendir(dir)) == NULL) {
        int num = 0;
 
        if ((fd = opendir(dir)) == NULL) {
-               fprintf(stderr, "Failed to get number of files in directory %s", dir);
+               fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
index 4a63896..2041ae4 100644 (file)
@@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <string.h>
 
@@ -12,7 +13,7 @@ run_command(const char *cmd)
 
        fp = popen(cmd, "r");
        if (fp == NULL) {
 
        fp = popen(cmd, "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to get command output for %s", cmd);
+               fprintf(stderr, "popen '%s': %s\n", cmd, strerror(errno));
                return NULL;
        }
        p = fgets(buf, sizeof(buf) - 1, fp);
                return NULL;
        }
        p = fgets(buf, sizeof(buf) - 1, fp);
index b82ff46..f3bbeb2 100644 (file)
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <string.h>
 
@@ -15,12 +16,12 @@ swap_free(void)
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file /proc/meminfo");
+               fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
                return NULL;
        }
 
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                return NULL;
        }
 
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
-               fprintf(stderr, "swap_free: read error");
+               fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
                fclose(fp);
                return NULL;
        }
                fclose(fp);
                return NULL;
        }
@@ -47,12 +48,12 @@ swap_perc(void)
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file /proc/meminfo");
+               fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
                return NULL;
        }
 
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                return NULL;
        }
 
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
-               fprintf(stderr, "swap_perc: read error");
+               fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
                fclose(fp);
                return NULL;
        }
                fclose(fp);
                return NULL;
        }
@@ -83,11 +84,11 @@ swap_total(void)
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file /proc/meminfo");
+               fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
                return NULL;
        }
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                return NULL;
        }
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
-               fprintf(stderr, "swap_total: read error");
+               fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
                fclose(fp);
                return NULL;
        }
                fclose(fp);
                return NULL;
        }
@@ -110,11 +111,11 @@ swap_used(void)
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
 
        fp = fopen("/proc/meminfo", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file /proc/meminfo");
+               fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
                return NULL;
        }
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                return NULL;
        }
        if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
-               fprintf(stderr, "swap_used: read error");
+               fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
                fclose(fp);
                return NULL;
        }
                fclose(fp);
                return NULL;
        }
index 8a04b92..a3082f2 100644 (file)
@@ -1,5 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 #if defined(__linux__)
 #include <sys/sysinfo.h>
 #elif defined(__OpenBSD__)
 #if defined(__linux__)
 #include <sys/sysinfo.h>
 #elif defined(__OpenBSD__)
@@ -35,8 +37,10 @@ uptime(void)
 
        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
                uptime = now - boottime.tv_sec;
 
        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
                uptime = now - boottime.tv_sec;
-       else
+       else {
+               fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
                return NULL;
                return NULL;
+       }
 #endif
        h = uptime / 3600;
        m = (uptime - h * 3600) / 60;
 #endif
        h = uptime / 3600;
        m = (uptime - h * 3600) / 60;
index ffbd945..b335dc3 100644 (file)
@@ -1,6 +1,8 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <pwd.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -18,7 +20,7 @@ username(void)
        struct passwd *pw = getpwuid(geteuid());
 
        if (pw == NULL) {
        struct passwd *pw = getpwuid(geteuid());
 
        if (pw == NULL) {
-               fprintf(stderr, "Failed to get username");
+               fprintf(stderr, "getpwuid '%d': %s\n", geteuid(), strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
index ec653a2..4c9a6ac 100644 (file)
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
+#include <errno.h>
 #include <fcntl.h>
 #include <sys/soundcard.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <sys/soundcard.h>
 #include <sys/ioctl.h>
@@ -18,19 +19,19 @@ vol_perc(const char *card)
 
        afd = open(card, O_RDONLY | O_NONBLOCK);
        if (afd == -1) {
 
        afd = open(card, O_RDONLY | O_NONBLOCK);
        if (afd == -1) {
-               fprintf(stderr, "Cannot open %s", card);
+               fprintf(stderr, "open '%s': %s\n", card, strerror(errno));
                return NULL;
        }
 
        if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
                return NULL;
        }
 
        if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
-               fprintf(stderr, "Cannot get volume for %s", card);
+               fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n", strerror(errno));
                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) == -1) {
                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) == -1) {
-                               fprintf(stderr, "vol_perc: ioctl");
+                               fprintf(stderr, "ioctl 'MIXER_READ(%d)': %s\n", i, strerror(errno));
                                close(afd);
                                return NULL;
                        }
                                close(afd);
                                return NULL;
                        }
index 500332e..388a30d 100644 (file)
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
 /* See LICENSE file for copyright and license details. */
 #if defined(__linux__)
+#include <errno.h>
 #include <ifaddrs.h>
 #include <linux/wireless.h>
 #include <sys/socket.h>
 #include <ifaddrs.h>
 #include <linux/wireless.h>
 #include <sys/socket.h>
@@ -25,7 +26,7 @@ wifi_perc(const char *iface)
        snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
        fp = fopen(path, "r");
        if (fp == NULL) {
        snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
        fp = fopen(path, "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file %s", path);
+               fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
                return NULL;
        }
        p = fgets(status, 5, fp);
                return NULL;
        }
        p = fgets(status, 5, fp);
@@ -36,7 +37,7 @@ wifi_perc(const char *iface)
 
        fp = fopen("/proc/net/wireless", "r");
        if (fp == NULL) {
 
        fp = fopen("/proc/net/wireless", "r");
        if (fp == NULL) {
-               fprintf(stderr, "Failed to open file /proc/net/wireless");
+               fprintf(stderr, "fopen '/proc/net/wireless': %s\n", strerror(errno));
                return NULL;
        }
 
                return NULL;
        }
 
@@ -71,12 +72,12 @@ wifi_essid(const char *iface)
        snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
 
        if (sockfd == -1) {
        snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
 
        if (sockfd == -1) {
-               fprintf(stderr, "Failed to get ESSID for interface %s", iface);
+               fprintf(stderr, "socket 'AF_INET': %s\n", strerror(errno));
                return NULL;
        }
        wreq.u.essid.pointer = id;
        if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
                return NULL;
        }
        wreq.u.essid.pointer = id;
        if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
-               fprintf(stderr, "Failed to get ESSID for interface %s", iface);
+               fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
                close(sockfd);
                return NULL;
        }
                close(sockfd);
                return NULL;
        }
diff --git a/util.c b/util.c
index cd3524b..6008ffc 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 /* See LICENSE file for copyright and license details. */
+#include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
@@ -29,7 +30,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 for %s failed", path);
+               fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
                return -1;
        }
        va_start(ap, fmt);
                return -1;
        }
        va_start(ap, fmt);