1
0

use warn[x]() instead of long fprintf()s

This commit is contained in:
Ali H. Fardan 2016-09-08 04:45:00 +03:00
parent 2afea97987
commit 9b2dc25348

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <limits.h> #include <limits.h>
@ -100,9 +101,7 @@ battery_perc(const char *battery)
fp = fopen(concat, "r"); fp = fopen(concat, "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening battery file: %s: %s\n", warn("Error opening battery file: %s", concat);
concat,
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -113,9 +112,7 @@ battery_perc(const char *battery)
fp = fopen(concat, "r"); fp = fopen(concat, "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening battery file: %s: %s\n", warn("Error opening battery file: %s", concat);
concat,
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -135,8 +132,7 @@ cpu_perc(void)
FILE *fp = fopen("/proc/stat","r"); FILE *fp = fopen("/proc/stat","r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening stat file: %s\n", warn("Error opening stat file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -147,8 +143,7 @@ cpu_perc(void)
fp = fopen("/proc/stat","r"); fp = fopen("/proc/stat","r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening stat file: %s\n", warn("Error opening stat file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -177,8 +172,7 @@ disk_free(const char *mountpoint)
struct statvfs fs; struct statvfs fs;
if (statvfs(mountpoint, &fs) < 0) { if (statvfs(mountpoint, &fs) < 0) {
fprintf(stderr, "Could not get filesystem info: %s\n", warn("Could not get filesystem info");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
@ -191,8 +185,7 @@ disk_perc(const char *mountpoint)
struct statvfs fs; struct statvfs fs;
if (statvfs(mountpoint, &fs) < 0) { if (statvfs(mountpoint, &fs) < 0) {
fprintf(stderr, "Could not get filesystem info: %s\n", warn("Could not get filesystem info");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -206,8 +199,7 @@ disk_total(const char *mountpoint)
struct statvfs fs; struct statvfs fs;
if (statvfs(mountpoint, &fs) < 0) { if (statvfs(mountpoint, &fs) < 0) {
fprintf(stderr, "Could not get filesystem info: %s\n", warn("Could not get filesystem info");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -220,8 +212,7 @@ disk_used(const char *mountpoint)
struct statvfs fs; struct statvfs fs;
if (statvfs(mountpoint, &fs) < 0) { if (statvfs(mountpoint, &fs) < 0) {
fprintf(stderr, "Could not get filesystem info: %s\n", warn("Could not get filesystem info");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -235,8 +226,7 @@ entropy(void)
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Could not open entropy file: %s\n", warn("Could not open entropy file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -258,8 +248,7 @@ hostname(void)
FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Could not open hostname file: %s\n", warn("Could not open hostname file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -279,8 +268,7 @@ ip(const char *interface)
char host[NI_MAXHOST]; char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) == -1) {
fprintf(stderr, "Error getting IP address: %s\n", warn("Error getting IP address");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -294,7 +282,7 @@ ip(const char *interface)
if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
if (s != 0) { if (s != 0) {
fprintf(stderr, "Error getting IP address.\n"); warnx("Error getting IP address.");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
return smprintf("%s", host); return smprintf("%s", host);
@ -313,7 +301,7 @@ load_avg(void)
double avgs[3]; double avgs[3];
if (getloadavg(avgs, 3) < 0) { if (getloadavg(avgs, 3) < 0) {
fprintf(stderr, "Error getting load avg.\n"); warnx("Error getting load avg.");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -327,8 +315,7 @@ ram_free(void)
FILE *fp = fopen("/proc/meminfo", "r"); FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file: %s\n", warn("Error opening meminfo file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -345,8 +332,7 @@ ram_perc(void)
FILE *fp = fopen("/proc/meminfo", "r"); FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file: %s\n", warn("Error opening meminfo file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -367,8 +353,7 @@ ram_total(void)
FILE *fp = fopen("/proc/meminfo", "r"); FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file: %s\n", warn("Error opening meminfo file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -384,8 +369,7 @@ ram_used(void)
FILE *fp = fopen("/proc/meminfo", "r"); FILE *fp = fopen("/proc/meminfo", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file: %s\n", warn("Error opening meminfo file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -407,8 +391,7 @@ run_command(const char* command)
char buffer[64]; char buffer[64];
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Could not get command output for: %s: %s\n", warn("Could not get command output for: %s", command);
command, strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -432,8 +415,7 @@ temp(const char *file)
FILE *fp = fopen(file, "r"); FILE *fp = fopen(file, "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Could not open temperature file: %s\n", warn("Could not open temperature file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -465,8 +447,7 @@ username(void)
if (pw == NULL) if (pw == NULL)
return smprintf("%s", pw->pw_name); return smprintf("%s", pw->pw_name);
fprintf(stderr, "Could not get username: %s\n", warn("Could not get username");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -524,8 +505,7 @@ wifi_perc(const char *wificard)
fp = fopen(concat, "r"); fp = fopen(concat, "r");
if(fp == NULL) { if(fp == NULL) {
fprintf(stderr, "Error opening wifi operstate file: %s\n", warn("Error opening wifi operstate file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -536,8 +516,7 @@ wifi_perc(const char *wificard)
fp = fopen("/proc/net/wireless", "r"); fp = fopen("/proc/net/wireless", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Error opening wireless file: %s\n", warn("Error opening wireless file");
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -567,14 +546,12 @@ wifi_essid(const char *wificard)
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
sprintf(wreq.ifr_name, wificard); sprintf(wreq.ifr_name, wificard);
if(sockfd == -1) { if(sockfd == -1) {
fprintf(stderr, "Cannot open socket for interface: %s: %s\n", warn("Cannot open socket for interface: %s", wificard);
wificard, strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
wreq.u.essid.pointer = id; wreq.u.essid.pointer = id;
if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", warn("Get ESSID ioctl failed for interface %s", wificard);
wificard, strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -605,7 +582,7 @@ main(void)
element = smprintf(argument.format, res); element = smprintf(argument.format, res);
if (element == NULL) { if (element == NULL) {
element = smprintf(UNKNOWN_STR); element = smprintf(UNKNOWN_STR);
fprintf(stderr, "Failed to format output.\n"); warnx("Failed to format output.");
} }
strlcat(status_string, element, sizeof(status_string)); strlcat(status_string, element, sizeof(status_string));
free(res); free(res);