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
This commit is contained in:
Aaron Marcher 2018-05-06 22:28:56 +02:00
parent 66a7fb16fc
commit ee5ec75621
15 changed files with 91 additions and 66 deletions

View File

@ -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 *

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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 "";
}
}

View File

@ -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++;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

3
util.c
View File

@ -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;
}