diff options
-rw-r--r-- | components/battery.c | 3 | ||||
-rw-r--r-- | components/cpu.c | 7 | ||||
-rw-r--r-- | components/datetime.c | 3 | ||||
-rw-r--r-- | components/disk.c | 10 | ||||
-rw-r--r-- | components/hostname.c | 2 | ||||
-rw-r--r-- | components/ip.c | 20 | ||||
-rw-r--r-- | components/keyboard_indicators.c | 16 | ||||
-rw-r--r-- | components/num_files.c | 7 | ||||
-rw-r--r-- | components/run_command.c | 6 | ||||
-rw-r--r-- | components/swap.c | 47 | ||||
-rw-r--r-- | components/temperature.c | 2 | ||||
-rw-r--r-- | components/uptime.c | 2 | ||||
-rw-r--r-- | components/volume.c | 13 | ||||
-rw-r--r-- | components/wifi.c | 16 | ||||
-rw-r--r-- | util.c | 3 |
15 files changed, 91 insertions, 66 deletions
diff --git a/components/battery.c b/components/battery.c index fe17a68..a63b9d0 100644 --- a/components/battery.c +++ b/components/battery.c @@ -16,8 +16,7 @@ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity"); - return (pscanf(path, "%i", &perc) == 1) ? - bprintf("%d", perc) : NULL; + return (pscanf(path, "%i", &perc) == 1) ? bprintf("%d", perc) : NULL; } const char * diff --git a/components/cpu.c b/components/cpu.c index c8299f9..3b29b8b 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -58,9 +58,8 @@ size = sizeof(freq); - if (sysctl(mib, 2, &freq, &size, NULL, 0) == -1) { - fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", - strerror(errno)); + if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) { + fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno)); return NULL; } @@ -82,7 +81,7 @@ size = sizeof(a); memcpy(b, a, sizeof(b)); - if (sysctl(mib, 2, &a, &size, NULL, 0) == -1) { + if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) { fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno)); return NULL; } diff --git a/components/datetime.c b/components/datetime.c index 0816923..713b0fb 100644 --- a/components/datetime.c +++ b/components/datetime.c @@ -9,8 +9,9 @@ datetime(const char *fmt) time_t t; t = time(NULL); - if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0) + if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) { return NULL; + } return buf; } diff --git a/components/disk.c b/components/disk.c index 8a9caa1..2ce260b 100644 --- a/components/disk.c +++ b/components/disk.c @@ -16,7 +16,8 @@ disk_free(const char *mnt) return NULL; } - return bprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); + return bprintf("%f", + (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); } const char * @@ -45,7 +46,8 @@ disk_total(const char *mnt) return NULL; } - return bprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); + return bprintf("%f", + (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); } const char * @@ -58,5 +60,7 @@ disk_used(const char *mnt) return NULL; } - return bprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); + return bprintf("%f", + (float)fs.f_bsize * ((float)fs.f_blocks - + (float)fs.f_bfree) / 1024 / 1024 / 1024); } diff --git a/components/hostname.c b/components/hostname.c index d7c10c8..d41465f 100644 --- a/components/hostname.c +++ b/components/hostname.c @@ -9,7 +9,7 @@ const char * hostname(void) { - if (gethostname(buf, sizeof(buf)) == -1) { + if (gethostname(buf, sizeof(buf)) < 0) { fprintf(stderr, "gethostbyname: %s\n", strerror(errno)); return NULL; } diff --git a/components/ip.c b/components/ip.c index b1f8a05..2d2cbde 100644 --- a/components/ip.c +++ b/components/ip.c @@ -18,17 +18,19 @@ ipv4(const char *iface) int s; char host[NI_MAXHOST]; - if (getifaddrs(&ifaddr) == -1) { + if (getifaddrs(&ifaddr) < 0) { fprintf(stderr, "getifaddrs: %s\n", strerror(errno)); return NULL; } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) { + if (!ifa->ifa_addr) { continue; } - 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)) { + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, + NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if (!strcmp(ifa->ifa_name, iface) && + (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); return NULL; @@ -49,17 +51,19 @@ ipv6(const char *iface) int s; char host[NI_MAXHOST]; - if (getifaddrs(&ifaddr) == -1) { + if (getifaddrs(&ifaddr) < 0) { fprintf(stderr, "getifaddrs: %s\n", strerror(errno)); return NULL; } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) { + if (!ifa->ifa_addr) { continue; } - 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)) { + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, + NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if (!strcmp(ifa->ifa_name, iface) && + (ifa->ifa_addr->sa_family == AF_INET6)) { if (s != 0) { fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); return NULL; diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c index ed4bc17..2f831c8 100644 --- a/components/keyboard_indicators.c +++ b/components/keyboard_indicators.c @@ -18,13 +18,13 @@ keyboard_indicators(void) XCloseDisplay(dpy); switch (state.led_mask) { - case 1: - return "c"; - case 2: - return "n"; - case 3: - return "cn"; - default: - return ""; + case 1: + return "c"; + case 2: + return "n"; + case 3: + return "cn"; + default: + return ""; } } diff --git a/components/num_files.c b/components/num_files.c index acf9dfa..1a918ac 100644 --- a/components/num_files.c +++ b/components/num_files.c @@ -13,14 +13,15 @@ num_files(const char *dir) DIR *fd; int num = 0; - if ((fd = opendir(dir)) == NULL) { + if (!(fd = opendir(dir))) { fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno)); return NULL; } - while ((dp = readdir(fd)) != NULL) { - if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) + while ((dp = readdir(fd))) { + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) { continue; /* skip self and parent */ + } num++; } diff --git a/components/run_command.c b/components/run_command.c index e356620..1aaae20 100644 --- a/components/run_command.c +++ b/components/run_command.c @@ -17,10 +17,12 @@ run_command(const char *cmd) } p = fgets(buf, sizeof(buf) - 1, fp); pclose(fp); - if (!p) + if (!p) { return NULL; - if ((p = strrchr(buf, '\n')) != NULL) + } + if ((p = strrchr(buf, '\n'))) { p[0] = '\0'; + } return buf[0] ? buf : NULL; } diff --git a/components/swap.c b/components/swap.c index f6f8993..031b713 100644 --- a/components/swap.c +++ b/components/swap.c @@ -13,13 +13,11 @@ size_t bytes_read; if (!(fp = fopen(path, "r"))) { - fprintf(stderr, "fopen '%s': %s\n", path, - strerror(errno)); + fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno)); return 0; } - if ((bytes_read = fread(buf, sizeof(char), bufsiz, fp)) == 0) { - fprintf(stderr, "fread '%s': %s\n", path, - strerror(errno)); + if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) { + fprintf(stderr, "fread '%s': %s\n", path, strerror(errno)); fclose(fp); return 0; } @@ -40,12 +38,14 @@ return NULL; } - if ((match = strstr(buf, "SwapTotal")) == NULL) + if (!(match = strstr(buf, "SwapTotal"))) { return NULL; + } sscanf(match, "SwapTotal: %ld kB\n", &total); - if ((match = strstr(buf, "SwapFree")) == NULL) + if (!(match = strstr(buf, "SwapFree"))) { return NULL; + } sscanf(match, "SwapFree: %ld kB\n", &free); return bprintf("%f", (float)free / 1024 / 1024); @@ -61,16 +61,19 @@ return NULL; } - if ((match = strstr(buf, "SwapTotal")) == NULL) + if (!(match = strstr(buf, "SwapTotal"))) { return NULL; + } sscanf(match, "SwapTotal: %ld kB\n", &total); - if ((match = strstr(buf, "SwapCached")) == NULL) + if (!(match = strstr(buf, "SwapCached"))) { return NULL; + } sscanf(match, "SwapCached: %ld kB\n", &cached); - if ((match = strstr(buf, "SwapFree")) == NULL) + if (!(match = strstr(buf, "SwapFree"))) { return NULL; + } sscanf(match, "SwapFree: %ld kB\n", &free); return bprintf("%d", 100 * (total - free - cached) / total); @@ -86,8 +89,9 @@ return NULL; } - if ((match = strstr(buf, "SwapTotal")) == NULL) + if (!(match = strstr(buf, "SwapTotal"))) { return NULL; + } sscanf(match, "SwapTotal: %ld kB\n", &total); return bprintf("%f", (float)total / 1024 / 1024); @@ -103,16 +107,19 @@ return NULL; } - if ((match = strstr(buf, "SwapTotal")) == NULL) + if (!(match = strstr(buf, "SwapTotal"))) { return NULL; + } sscanf(match, "SwapTotal: %ld kB\n", &total); - if ((match = strstr(buf, "SwapCached")) == NULL) + if (!(match = strstr(buf, "SwapCached"))) { return NULL; + } sscanf(match, "SwapCached: %ld kB\n", &cached); - if ((match = strstr(buf, "SwapFree")) == NULL) + if (!(match = strstr(buf, "SwapFree"))) { return NULL; + } sscanf(match, "SwapFree: %ld kB\n", &free); return bprintf("%f", (float)(total - free - cached) / 1024 / 1024); @@ -133,19 +140,23 @@ int rnswap, nswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); - if (nswap < 1) + if (nswap < 1) { fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno)); + } fsep = sep = calloc(nswap, sizeof(*sep)); - if (sep == NULL) + if (!sep) { fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno)); + } rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); - if (rnswap < 0) + if (rnswap < 0) { fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno)); + } - if (nswap != rnswap) + if (nswap != rnswap) { fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n"); + } *total = 0; *used = 0; diff --git a/components/temperature.c b/components/temperature.c index 1353993..720f1b7 100644 --- a/components/temperature.c +++ b/components/temperature.c @@ -35,7 +35,7 @@ size = sizeof(temp); - if (sysctl(mib, 5, &temp, &size, NULL, 0) == -1) { + if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) { fprintf(stderr, "sysctl 'SENSOR_TEMP': %s\n", strerror(errno)); return NULL; diff --git a/components/uptime.c b/components/uptime.c index f662b40..981f3cd 100644 --- a/components/uptime.c +++ b/components/uptime.c @@ -49,7 +49,7 @@ format(int uptime) size = sizeof(boottime); - if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) { + if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) { fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno)); return NULL; } diff --git a/components/volume.c b/components/volume.c index 41a112d..b2ffb40 100644 --- a/components/volume.c +++ b/components/volume.c @@ -20,21 +20,22 @@ vol_perc(const char *card) int v, afd, devmask; char *vnames[] = SOUND_DEVICE_NAMES; - afd = open(card, O_RDONLY | O_NONBLOCK); - if (afd == -1) { + if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) { fprintf(stderr, "open '%s': %s\n", card, strerror(errno)); return NULL; } - if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { - fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n", strerror(errno)); + if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { + 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) { - fprintf(stderr, "ioctl 'MIXER_READ(%d)': %s\n", i, strerror(errno)); + if (ioctl(afd, MIXER_READ(i), &v) < 0) { + fprintf(stderr, "ioctl 'MIXER_READ(%d)': %s\n", i, + strerror(errno)); close(afd); return NULL; } diff --git a/components/wifi.c b/components/wifi.c index 7cd2702..c209598 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -47,11 +47,13 @@ break; } fclose(fp); - if (i < 2 || !p) + if (i < 2 || !p) { return NULL; + } - if ((datastart = strstr(buf, iface)) == NULL) + if (!(datastart = strstr(buf, iface))) { return NULL; + } datastart = (datastart+(strlen(iface)+1)); sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " @@ -73,23 +75,23 @@ wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); - if (sockfd == -1) { + if (sockfd < 0) { fprintf(stderr, "socket 'AF_INET': %s\n", strerror(errno)); return NULL; } wreq.u.essid.pointer = id; - if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", - strerror(errno)); + if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) { + fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno)); close(sockfd); return NULL; } close(sockfd); - if (strcmp(id, "") == 0) + if (!strcmp(id, "")) { return NULL; + } return id; } @@ -16,8 +16,9 @@ bprintf(const char *fmt, ...) len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap); va_end(ap); - if (len >= sizeof(buf)) + if (len >= sizeof(buf)) { buf[sizeof(buf)-1] = '\0'; + } return buf; } |