Fix coding style - Use block for single statement ifs - Keep lines to reasonable length (current debate as to reasonable) - When functions return -1 for error test against 0 not -1 - Do not indent cases another level - Do not test against NULL and 0 explicitly - Use tabs for indentation, use spaces for alignment
jump to
@@ -16,8 +16,7 @@ char path[PATH_MAX];
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 *
@@ -58,9 +58,8 @@ mib[1] = HW_CPUSPEED;
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; }
@@ -9,8 +9,9 @@ {
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; }
@@ -16,7 +16,8 @@ fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
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 @@ fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
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 @@ fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
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); }
@@ -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; }
@@ -18,17 +18,19 @@ struct ifaddrs *ifaddr, *ifa;
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 @@ struct ifaddrs *ifaddr, *ifa;
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;
@@ -18,13 +18,13 @@ XGetKeyboardControl(dpy, &state);
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 ""; } }
@@ -13,14 +13,15 @@ struct dirent *dp;
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++; }
@@ -17,10 +17,12 @@ return NULL;
} 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; }
@@ -13,13 +13,11 @@ FILE *fp;
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 @@ if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
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 @@ if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
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 @@ if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
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 @@ if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
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 @@ struct swapent *sep, *fsep;
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;
@@ -35,7 +35,7 @@ mib[4] = 0; /* temp0 */
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;
@@ -49,7 +49,7 @@ mib[1] = KERN_BOOTTIME;
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; }
@@ -20,21 +20,22 @@ unsigned int i;
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; }
@@ -47,11 +47,13 @@ if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
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 @@ memset(&wreq, 0, sizeof(struct iwreq));
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; }