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/", snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
bat, "/capacity"); bat, "/capacity");
return (pscanf(path, "%i", &perc) == 1) ? return (pscanf(path, "%i", &perc) == 1) ? bprintf("%d", perc) : NULL;
bprintf("%d", perc) : NULL;
} }
const char * const char *

View file

@ -58,9 +58,8 @@
size = sizeof(freq); size = sizeof(freq);
if (sysctl(mib, 2, &freq, &size, NULL, 0) == -1) { if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", fprintf(stderr, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno));
strerror(errno));
return NULL; return NULL;
} }
@ -82,7 +81,7 @@
size = sizeof(a); size = sizeof(a);
memcpy(b, a, sizeof(b)); 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)); fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno));
return NULL; return NULL;
} }

View file

@ -9,8 +9,9 @@ datetime(const char *fmt)
time_t t; time_t t;
t = time(NULL); t = time(NULL);
if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0) if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) {
return NULL; return NULL;
}
return buf; return buf;
} }

View file

@ -16,7 +16,8 @@ disk_free(const char *mnt)
return NULL; 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 * const char *
@ -45,7 +46,8 @@ disk_total(const char *mnt)
return NULL; 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 * const char *
@ -58,5 +60,7 @@ disk_used(const char *mnt)
return NULL; 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 * const char *
hostname(void) hostname(void)
{ {
if (gethostname(buf, sizeof(buf)) == -1) { if (gethostname(buf, sizeof(buf)) < 0) {
fprintf(stderr, "gethostbyname: %s\n", strerror(errno)); fprintf(stderr, "gethostbyname: %s\n", strerror(errno));
return NULL; return NULL;
} }

View file

@ -18,17 +18,19 @@ ipv4(const char *iface)
int s; int s;
char host[NI_MAXHOST]; char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) < 0) {
fprintf(stderr, "getifaddrs: %s\n", strerror(errno)); fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
return NULL; return NULL;
} }
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) { if (!ifa->ifa_addr) {
continue; continue;
} }
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, iface) &&
(ifa->ifa_addr->sa_family == AF_INET)) {
if (s != 0) { if (s != 0) {
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
return NULL; return NULL;
@ -49,17 +51,19 @@ ipv6(const char *iface)
int s; int s;
char host[NI_MAXHOST]; char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) < 0) {
fprintf(stderr, "getifaddrs: %s\n", strerror(errno)); fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
return NULL; return NULL;
} }
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) { if (!ifa->ifa_addr) {
continue; continue;
} }
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host,
if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) { NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, iface) &&
(ifa->ifa_addr->sa_family == AF_INET6)) {
if (s != 0) { if (s != 0) {
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
return NULL; return NULL;

View file

@ -13,14 +13,15 @@ num_files(const char *dir)
DIR *fd; DIR *fd;
int num = 0; int num = 0;
if ((fd = opendir(dir)) == NULL) { if (!(fd = opendir(dir))) {
fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno)); fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
return NULL; return NULL;
} }
while ((dp = readdir(fd)) != NULL) { while ((dp = readdir(fd))) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
continue; /* skip self and parent */ continue; /* skip self and parent */
}
num++; num++;
} }

View file

@ -17,10 +17,12 @@ run_command(const char *cmd)
} }
p = fgets(buf, sizeof(buf) - 1, fp); p = fgets(buf, sizeof(buf) - 1, fp);
pclose(fp); pclose(fp);
if (!p) if (!p) {
return NULL; return NULL;
if ((p = strrchr(buf, '\n')) != NULL) }
if ((p = strrchr(buf, '\n'))) {
p[0] = '\0'; p[0] = '\0';
}
return buf[0] ? buf : NULL; return buf[0] ? buf : NULL;
} }

View file

@ -13,13 +13,11 @@
size_t bytes_read; size_t bytes_read;
if (!(fp = fopen(path, "r"))) { if (!(fp = fopen(path, "r"))) {
fprintf(stderr, "fopen '%s': %s\n", path, fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
strerror(errno));
return 0; return 0;
} }
if ((bytes_read = fread(buf, sizeof(char), bufsiz, fp)) == 0) { if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
fprintf(stderr, "fread '%s': %s\n", path, fprintf(stderr, "fread '%s': %s\n", path, strerror(errno));
strerror(errno));
fclose(fp); fclose(fp);
return 0; return 0;
} }
@ -40,12 +38,14 @@
return NULL; return NULL;
} }
if ((match = strstr(buf, "SwapTotal")) == NULL) if (!(match = strstr(buf, "SwapTotal"))) {
return NULL; return NULL;
}
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if ((match = strstr(buf, "SwapFree")) == NULL) if (!(match = strstr(buf, "SwapFree"))) {
return NULL; return NULL;
}
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);
return bprintf("%f", (float)free / 1024 / 1024); return bprintf("%f", (float)free / 1024 / 1024);
@ -61,16 +61,19 @@
return NULL; return NULL;
} }
if ((match = strstr(buf, "SwapTotal")) == NULL) if (!(match = strstr(buf, "SwapTotal"))) {
return NULL; return NULL;
}
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if ((match = strstr(buf, "SwapCached")) == NULL) if (!(match = strstr(buf, "SwapCached"))) {
return NULL; return NULL;
}
sscanf(match, "SwapCached: %ld kB\n", &cached); sscanf(match, "SwapCached: %ld kB\n", &cached);
if ((match = strstr(buf, "SwapFree")) == NULL) if (!(match = strstr(buf, "SwapFree"))) {
return NULL; return NULL;
}
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);
return bprintf("%d", 100 * (total - free - cached) / total); return bprintf("%d", 100 * (total - free - cached) / total);
@ -86,8 +89,9 @@
return NULL; return NULL;
} }
if ((match = strstr(buf, "SwapTotal")) == NULL) if (!(match = strstr(buf, "SwapTotal"))) {
return NULL; return NULL;
}
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
return bprintf("%f", (float)total / 1024 / 1024); return bprintf("%f", (float)total / 1024 / 1024);
@ -103,16 +107,19 @@
return NULL; return NULL;
} }
if ((match = strstr(buf, "SwapTotal")) == NULL) if (!(match = strstr(buf, "SwapTotal"))) {
return NULL; return NULL;
}
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if ((match = strstr(buf, "SwapCached")) == NULL) if (!(match = strstr(buf, "SwapCached"))) {
return NULL; return NULL;
}
sscanf(match, "SwapCached: %ld kB\n", &cached); sscanf(match, "SwapCached: %ld kB\n", &cached);
if ((match = strstr(buf, "SwapFree")) == NULL) if (!(match = strstr(buf, "SwapFree"))) {
return NULL; return NULL;
}
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);
return bprintf("%f", (float)(total - free - cached) / 1024 / 1024); return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
@ -133,19 +140,23 @@
int rnswap, nswap, i; int rnswap, nswap, i;
nswap = swapctl(SWAP_NSWAP, 0, 0); nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap < 1) if (nswap < 1) {
fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno)); fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno));
}
fsep = sep = calloc(nswap, sizeof(*sep)); fsep = sep = calloc(nswap, sizeof(*sep));
if (sep == NULL) if (!sep) {
fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno)); fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
}
rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
if (rnswap < 0) if (rnswap < 0) {
fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno)); fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno));
}
if (nswap != rnswap) if (nswap != rnswap) {
fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n"); fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
}
*total = 0; *total = 0;
*used = 0; *used = 0;

View file

@ -35,7 +35,7 @@
size = sizeof(temp); 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", fprintf(stderr, "sysctl 'SENSOR_TEMP': %s\n",
strerror(errno)); strerror(errno));
return NULL; return NULL;

View file

@ -49,7 +49,7 @@ format(int uptime)
size = sizeof(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)); fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
return NULL; return NULL;
} }

View file

@ -20,21 +20,22 @@ vol_perc(const char *card)
int v, afd, devmask; int v, afd, devmask;
char *vnames[] = SOUND_DEVICE_NAMES; char *vnames[] = SOUND_DEVICE_NAMES;
afd = open(card, O_RDONLY | O_NONBLOCK); if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) {
if (afd == -1) {
fprintf(stderr, "open '%s': %s\n", card, strerror(errno)); fprintf(stderr, "open '%s': %s\n", card, strerror(errno));
return NULL; return NULL;
} }
if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n", strerror(errno)); fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n",
strerror(errno));
close(afd); close(afd);
return NULL; return NULL;
} }
for (i = 0; i < LEN(vnames); i++) { for (i = 0; i < LEN(vnames); i++) {
if (devmask & (1 << i) && !strcmp("vol", vnames[i])) { if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
if (ioctl(afd, MIXER_READ(i), &v) == -1) { if (ioctl(afd, MIXER_READ(i), &v) < 0) {
fprintf(stderr, "ioctl 'MIXER_READ(%d)': %s\n", i, strerror(errno)); fprintf(stderr, "ioctl 'MIXER_READ(%d)': %s\n", i,
strerror(errno));
close(afd); close(afd);
return NULL; return NULL;
} }

View file

@ -47,11 +47,13 @@
break; break;
} }
fclose(fp); fclose(fp);
if (i < 2 || !p) if (i < 2 || !p) {
return NULL; return NULL;
}
if ((datastart = strstr(buf, iface)) == NULL) if (!(datastart = strstr(buf, iface))) {
return NULL; return NULL;
}
datastart = (datastart+(strlen(iface)+1)); datastart = (datastart+(strlen(iface)+1));
sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
@ -73,23 +75,23 @@
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
if (sockfd == -1) { if (sockfd < 0) {
fprintf(stderr, "socket 'AF_INET': %s\n", fprintf(stderr, "socket 'AF_INET': %s\n",
strerror(errno)); strerror(errno));
return NULL; return NULL;
} }
wreq.u.essid.pointer = id; wreq.u.essid.pointer = id;
if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
strerror(errno));
close(sockfd); close(sockfd);
return NULL; return NULL;
} }
close(sockfd); close(sockfd);
if (strcmp(id, "") == 0) if (!strcmp(id, "")) {
return NULL; return NULL;
}
return id; return id;
} }

3
util.c
View file

@ -16,8 +16,9 @@ bprintf(const char *fmt, ...)
len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap); len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
va_end(ap); va_end(ap);
if (len >= sizeof(buf)) if (len >= sizeof(buf)) {
buf[sizeof(buf)-1] = '\0'; buf[sizeof(buf)-1] = '\0';
}
return buf; return buf;
} }