1
0
Fork 0

got rid of conditional assignments && improved the error messages to output more info && added bound checks for fscanf() in hostname() (a dirty hack) && fixed a bug or two && some tiny style corrections

This commit is contained in:
Ali H. Fardan 2016-08-30 21:50:40 +03:00
parent 5829cee24e
commit 0aefd57f9f

View file

@ -105,7 +105,7 @@ battery_perc(const char *battery)
int now, full, perc; int now, full, perc;
char batterynowfile[64]; char batterynowfile[64];
char batteryfullfile[64]; char batteryfullfile[64];
FILE *fp; FILE *fp = fopen(batterynowfile, "r");
strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile));
strlcat(batterynowfile, battery, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile));
@ -117,16 +117,20 @@ battery_perc(const char *battery)
strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, "/", sizeof(batteryfullfile));
strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile));
if (!(fp = fopen(batterynowfile, "r"))) { if (fp == NULL ) {
fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); fprintf(stderr, "Error opening battery file: %s: %s\n",
batterynowfile,
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
fscanf(fp, "%i", &now); fscanf(fp, "%i", &now);
fclose(fp); fclose(fp);
if (!(fp = fopen(batteryfullfile, "r"))) { fp = fopen(batteryfullfile, "r");
fprintf(stderr, "Error opening battery file.\n"); if (fp == NULL) {
fprintf(stderr, "Error opening battery file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -143,10 +147,11 @@ cpu_perc(void)
{ {
int perc; int perc;
long double a[4], b[4]; long double a[4], b[4];
FILE *fp; FILE *fp = fopen("/proc/stat","r");
if (!(fp = fopen("/proc/stat","r"))) { if (fp == NULL) {
fprintf(stderr, "Error opening stat file.\n"); fprintf(stderr, "Error opening stat file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -156,8 +161,10 @@ cpu_perc(void)
/* wait a second (for avg values) */ /* wait a second (for avg values) */
sleep(1); sleep(1);
if (!(fp = fopen("/proc/stat","r"))) { fp = fopen("/proc/stat","r");
fprintf(stderr, "Error opening stat file.\n"); if (fp == NULL) {
fprintf(stderr, "Error opening stat file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -186,7 +193,8 @@ 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.\n"); fprintf(stderr, "Could not get filesystem info: %s\n",
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);
@ -199,7 +207,8 @@ 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.\n"); fprintf(stderr, "Could not get filesystem info: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -213,7 +222,8 @@ 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.\n"); fprintf(stderr, "Could not get filesystem info: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -226,7 +236,8 @@ 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.\n"); fprintf(stderr, "Could not get filesystem info: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -237,9 +248,9 @@ static char *
entropy(void) entropy(void)
{ {
int entropy = 0; int entropy = 0;
FILE *fp; FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { if (fp == NULL) {
fprintf(stderr, "Could not open entropy file.\n"); fprintf(stderr, "Could not open entropy file.\n");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -260,14 +271,18 @@ static char *
hostname(void) hostname(void)
{ {
char hostname[HOST_NAME_MAX]; char hostname[HOST_NAME_MAX];
FILE *fp; FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { if (fp == NULL) {
fprintf(stderr, "Could not open hostname file.\n"); fprintf(stderr, "Could not open hostname file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
fscanf(fp, "%s\n", hostname); fgets(hostname, sizeof(hostname), fp);
/* FIXME: needs improvement */
memset(&hostname[strlen(hostname)-1], '\0',
sizeof(hostname) - strlen(hostname));
fclose(fp); fclose(fp);
return smprintf("%s", hostname); return smprintf("%s", hostname);
} }
@ -280,7 +295,8 @@ 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.\n"); fprintf(stderr, "Error getting IP address: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -289,7 +305,8 @@ ip(const char *interface)
if (ifa->ifa_addr == NULL) if (ifa->ifa_addr == NULL)
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, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
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) {
@ -323,10 +340,11 @@ static char *
ram_free(void) ram_free(void)
{ {
long free; long free;
FILE *fp; FILE *fp = fopen("/proc/meminfo", "r");
if (!(fp = fopen("/proc/meminfo", "r"))) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file.\n"); fprintf(stderr, "Error opening meminfo file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -340,10 +358,11 @@ ram_perc(void)
{ {
int perc; int perc;
long total, free, buffers, cached; long total, free, buffers, cached;
FILE *fp; FILE *fp = fopen("/proc/meminfo", "r");
if (!(fp = fopen("/proc/meminfo", "r"))) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file.\n"); fprintf(stderr, "Error opening meminfo file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -361,10 +380,11 @@ static char *
ram_total(void) ram_total(void)
{ {
long total; long total;
FILE *fp; FILE *fp = fopen("/proc/meminfo", "r");
if (!(fp = fopen("/proc/meminfo", "r"))) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file.\n"); fprintf(stderr, "Error opening meminfo file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -377,10 +397,11 @@ static char *
ram_used(void) ram_used(void)
{ {
long free, total, buffers, cached, used; long free, total, buffers, cached, used;
FILE *fp; FILE *fp = fopen("/proc/meminfo", "r");
if (!(fp = fopen("/proc/meminfo", "r"))) { if (fp == NULL) {
fprintf(stderr, "Error opening meminfo file.\n"); fprintf(stderr, "Error opening meminfo file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -398,11 +419,12 @@ static char *
run_command(const char* command) run_command(const char* command)
{ {
int good; int good;
FILE *fp; FILE *fp = popen(command, "r");
char buffer[64]; char buffer[64];
if (!(fp = popen(command, "r"))) { if (fp == NULL) {
fprintf(stderr, "Could not get command output for: %s.\n", command); fprintf(stderr, "Could not get command output for: %s: %s\n",
command, strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -423,10 +445,11 @@ static char *
temp(const char *file) temp(const char *file)
{ {
int temperature; int temperature;
FILE *fp; FILE *fp = fopen(file, "r");
if (!(fp = fopen(file, "r"))) { if (fp == NULL) {
fprintf(stderr, "Could not open temperature file.\n"); fprintf(stderr, "Could not open temperature file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -461,7 +484,8 @@ username(void)
if (pw) if (pw)
return smprintf("%s", pw->pw_name); return smprintf("%s", pw->pw_name);
else { else {
fprintf(stderr, "Could not get username.\n"); fprintf(stderr, "Could not get username: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -471,6 +495,7 @@ username(void)
static char * static char *
uid(void) uid(void)
{ {
/* FIXME: WHY USE register modifier? */
register uid_t uid; register uid_t uid;
uid = geteuid(); uid = geteuid();
@ -531,22 +556,21 @@ vol_perc(const char *soundcard)
static char * static char *
wifi_perc(const char *wificard) wifi_perc(const char *wificard)
{ {
int bufsize = 255;
int strength; int strength;
char buf[bufsize]; char buf[255];
char *datastart; char *datastart;
char path[64]; char path[64];
char status[5]; char status[5];
char needle[sizeof wificard + 1]; char needle[strlen(wificard)+2];
FILE *fp; FILE *fp = fopen(path, "r");
memset(path, 0, sizeof path); strlcpy(path, "/sys/class/net/", sizeof(path));
strlcat(path, "/sys/class/net/", sizeof(path));
strlcat(path, wificard, sizeof(path)); strlcat(path, wificard, sizeof(path));
strlcat(path, "/operstate", sizeof(path)); strlcat(path, "/operstate", sizeof(path));
if(!(fp = fopen(path, "r"))) { if(fp == NULL) {
fprintf(stderr, "Error opening wifi operstate file.\n"); fprintf(stderr, "Error opening wifi operstate file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -555,17 +579,21 @@ wifi_perc(const char *wificard)
if(strcmp(status, "up\n") != 0) if(strcmp(status, "up\n") != 0)
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
if (!(fp = fopen("/proc/net/wireless", "r"))) { fp = fopen("/proc/net/wireless", "r");
fprintf(stderr, "Error opening wireless file.\n"); if (fp == NULL) {
fprintf(stderr, "Error opening wireless file: %s\n",
strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
strlcpy(needle, wificard, sizeof(needle)); strlcpy(needle, wificard, sizeof(needle));
strlcat(needle, ":", sizeof(needle)); strlcat(needle, ":", sizeof(needle));
fgets(buf, bufsize, fp); fgets(buf, sizeof(buf), fp);
fgets(buf, bufsize, fp); fgets(buf, sizeof(buf), fp);
fgets(buf, bufsize, fp); fgets(buf, sizeof(buf), fp);
if ((datastart = strstr(buf, needle)) != NULL) {
datastart = strstr(buf, needle);
if (datastart != NULL) {
datastart = strstr(buf, ":"); datastart = strstr(buf, ":");
sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength); sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
} }
@ -578,19 +606,21 @@ static char *
wifi_essid(const char *wificard) wifi_essid(const char *wificard)
{ {
char id[IW_ESSID_MAX_SIZE+1]; char id[IW_ESSID_MAX_SIZE+1];
int sockfd; int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct iwreq wreq; struct iwreq wreq;
memset(&wreq, 0, sizeof(struct iwreq)); memset(&wreq, 0, sizeof(struct iwreq));
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 = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { if(sockfd == -1) {
fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); fprintf(stderr, "Cannot open socket for interface: %s: %s\n",
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\n", wificard); fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n",
wificard, strerror(errno));
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
@ -608,7 +638,8 @@ main(void)
char *res, *element; char *res, *element;
struct arg argument; struct arg argument;
if (!(dpy = XOpenDisplay(0x0))) { dpy = XOpenDisplay(0x0);
if (!dpy) {
fprintf(stderr, "Cannot open display!\n"); fprintf(stderr, "Cannot open display!\n");
exit(1); exit(1);
} }