From e1ae7d3be6faa710b3a711ed317cfe45d90f28bc Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:00:23 +0300 Subject: added bounds checking via secure strl*() routines --- slstatus.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 0dee744..823b8eb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -24,6 +24,12 @@ #include #include +#undef strlcat +#undef strlcpy + +#include "strlcat.h" +#include "strlcpy.h" + /* statusbar configuration type and struct */ typedef char *(*op_fun) (const char *); struct arg { @@ -101,16 +107,16 @@ battery_perc(const char *battery) FILE *fp; /* generate battery nowfile path */ - strcat(batterynowfile, batterypath); - strcat(batterynowfile, battery); - strcat(batterynowfile, "/"); - strcat(batterynowfile, batterynow); + strlcat(batterynowfile, batterypath, sizeof(batterynowfile)); + strlcat(batterynowfile, battery, sizeof(batterynowfile)); + strlcat(batterynowfile, "/", sizeof(batterynowfile)); + strlcat(batterynowfile, batterynow, sizeof(batterynowfile)); /* generate battery fullfile path */ - strcat(batteryfullfile, batterypath); - strcat(batteryfullfile, battery); - strcat(batteryfullfile, "/"); - strcat(batteryfullfile, batteryfull); + strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile)); + strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); + strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); + strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile)); /* open battery now file */ if (!(fp = fopen(batterynowfile, "r"))) { @@ -688,9 +694,9 @@ wifi_perc(const char *wificard) /* generate the path name */ memset(path, 0, sizeof path); - strcat(path, "/sys/class/net/"); - strcat(path, wificard); - strcat(path, "/operstate"); + strlcat(path, "/sys/class/net/", sizeof(path)); + strlcat(path, wificard, sizeof(path)); + strlcat(path, "/operstate", sizeof(path)); /* open wifi file */ if(!(fp = fopen(path, "r"))) { @@ -716,8 +722,8 @@ wifi_perc(const char *wificard) } /* extract the signal strength */ - strcpy(needle, wificard); - strcat(needle, ":"); + strlcpy(needle, wificard, sizeof(needle)); + strlcat(needle, ":", sizeof(needle)); fgets(buf, bufsize, fp); fgets(buf, bufsize, fp); fgets(buf, bufsize, fp); @@ -794,7 +800,7 @@ main(void) element = smprintf(unknowntext); fprintf(stderr, "Failed to format output.\n"); } - strcat(status_string, element); + strlcat(status_string, element, sizeof(status_string)); free(res); free(element); } -- cgit v1.2.3 From 1853884520ddca4237dd0534256da0c8bf628251 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:28:36 +0300 Subject: braces are unneeded for one-liner if()/while() --- slstatus.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 823b8eb..f58291b 100644 --- a/slstatus.c +++ b/slstatus.c @@ -89,9 +89,8 @@ smprintf(const char *fmt, ...) char *ret = NULL; va_start(fmtargs, fmt); - if (vasprintf(&ret, fmt, fmtargs) < 0) { + if (vasprintf(&ret, fmt, fmtargs) < 0) return NULL; - } va_end(fmtargs); return ret; @@ -366,9 +365,8 @@ ip(const char *interface) /* get the ip address */ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) { + if (ifa->ifa_addr == NULL) continue; - } s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); @@ -535,9 +533,8 @@ run_command(const char* command) break; } } - if (good) { + if (good) buffer[strlen(buffer) - 1] = '\0'; - } /* return the output */ return smprintf("%s", buffer); @@ -595,9 +592,9 @@ username(const char *null) pw = getpwuid(uid); /* if it worked, return */ - if (pw) { + if (pw) return smprintf("%s", pw->pw_name); - } else { + else { fprintf(stderr, "Could not get username.\n"); return smprintf(unknowntext); } @@ -615,9 +612,9 @@ uid(const char *null) uid = geteuid(); /* if it worked, return */ - if (uid) { + if (uid) return smprintf("%d", uid); - } else { + else { fprintf(stderr, "Could not get uid.\n"); return smprintf(unknowntext); } @@ -661,22 +658,18 @@ vol_perc(const char *soundcard) snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); /* clean up */ - if (vol_info) { + if (vol_info) snd_mixer_selem_id_free(vol_info); - } - if (mute_info) { + if (mute_info) snd_mixer_selem_id_free(mute_info); - } - if (handle) { + if (handle) snd_mixer_close(handle); - } /* return the string (mute) */ - if (!mute) { + if (!mute) return smprintf("mute"); - } else { + else return smprintf("%d%%", (vol * 100) / max); - } } /* wifi percentage */ @@ -711,9 +704,8 @@ wifi_perc(const char *wificard) fclose(fp); /* check if interface down */ - if(strcmp(status, "up\n") != 0) { + if(strcmp(status, "up\n") != 0) return smprintf(unknowntext); - } /* open wifi file */ if (!(fp = fopen("/proc/net/wireless", "r"))) { @@ -766,11 +758,10 @@ wifi_essid(const char *wificard) } /* return the essid */ - if (strcmp((char *)wreq.u.essid.pointer, "") == 0) { + if (strcmp((char *)wreq.u.essid.pointer, "") == 0) return smprintf(unknowntext); - } else { + else return smprintf("%s", (char *)wreq.u.essid.pointer); - } } /* main function */ -- cgit v1.2.3 From 2e1eb518afea7c93e3ba750804f1f4c4d0a51af4 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:10:14 +0300 Subject: the code describes itself, there is no need to write stories in /* */ --- slstatus.c | 223 ++++++++----------------------------------------------------- 1 file changed, 28 insertions(+), 195 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index f58291b..3ea29d1 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -/* global libraries */ #include #include #include @@ -30,7 +29,6 @@ #include "strlcat.h" #include "strlcpy.h" -/* statusbar configuration type and struct */ typedef char *(*op_fun) (const char *); struct arg { op_fun func; @@ -38,41 +36,37 @@ struct arg { const char *args; }; -/* function declarations */ -void setstatus(const char *str); -char *smprintf(const char *fmt, ...); -char *battery_perc(const char *battery); -char *cpu_perc(const char *null); -char *datetime(const char *timeformat); -char *disk_free(const char *mountpoint); -char *disk_perc(const char *mountpoint); -char *disk_total(const char *mountpoint); -char *disk_used(const char *mountpoint); -char *entropy(const char *null); -char *gid(const char *null); -char *hostname(const char *null); -char *ip(const char *interface); -char *load_avg(const char *null); -char *ram_free(const char *null); -char *ram_perc(const char *null); -char *ram_used(const char *null); -char *ram_total(const char *null); -char *run_command(const char *command); -char *temp(const char *file); -char *uid(const char *null); -char *uptime(const char *null); -char *username(const char *null); -char *vol_perc(const char *soundcard); -char *wifi_perc(const char *wificard); -char *wifi_essid(const char *wificard); - -/* global variables */ +void setstatus(const char *); +char *smprintf(const char *, ...); +char *battery_perc(const char *); +char *cpu_perc(const char *); +char *datetime(const char *); +char *disk_free(const char *); +char *disk_perc(const char *); +char *disk_total(const char *); +char *disk_used(const char *); +char *entropy(const char *); +char *gid(const char *); +char *hostname(const char *); +char *ip(const char *); +char *load_avg(const char *); +char *ram_free(const char *); +char *ram_perc(const char *); +char *ram_used(const char *); +char *ram_total(const char *); +char *run_command(const char *); +char *temp(const char *); +char *uid(const char *); +char *uptime(const char *); +char *username(const char *); +char *vol_perc(const char *); +char *wifi_perc(const char *); +char *wifi_essid(const char *); + static Display *dpy; -/* configuration header */ #include "config.h" -/* set statusbar */ void setstatus(const char *str) { @@ -81,7 +75,6 @@ setstatus(const char *str) XSync(dpy, False); } -/* smprintf function */ char * smprintf(const char *fmt, ...) { @@ -96,7 +89,6 @@ smprintf(const char *fmt, ...) return ret; } -/* battery percentage */ char * battery_perc(const char *battery) { @@ -105,50 +97,37 @@ battery_perc(const char *battery) char batteryfullfile[64] = ""; FILE *fp; - /* generate battery nowfile path */ strlcat(batterynowfile, batterypath, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); strlcat(batterynowfile, "/", sizeof(batterynowfile)); strlcat(batterynowfile, batterynow, sizeof(batterynowfile)); - /* generate battery fullfile path */ strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile)); strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile)); - /* open battery now file */ if (!(fp = fopen(batterynowfile, "r"))) { fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); return smprintf(unknowntext); } - /* read value */ fscanf(fp, "%i", &now); - - /* close battery now file */ fclose(fp); - /* open battery full file */ if (!(fp = fopen(batteryfullfile, "r"))) { fprintf(stderr, "Error opening battery file.\n"); return smprintf(unknowntext); } - /* read value */ fscanf(fp, "%i", &full); - - /* close battery full file */ fclose(fp); - /* calculate percent */ perc = now / (full / 100); - /* return perc as string */ return smprintf("%d%%", perc); } -/* cpu percentage */ char * cpu_perc(const char *null) { @@ -156,41 +135,28 @@ cpu_perc(const char *null) long double a[4], b[4]; FILE *fp; - /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); return smprintf(unknowntext); } - /* read values */ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); - - /* close stat file */ fclose(fp); /* wait a second (for avg values) */ sleep(1); - /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); return smprintf(unknowntext); } - /* read values */ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]); - - /* close stat file */ fclose(fp); - - /* calculate avg in this second */ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); - - /* return perc as string */ return smprintf("%d%%", perc); } -/* date and time */ char * datetime(const char *timeformat) { @@ -202,7 +168,6 @@ datetime(const char *timeformat) return smprintf(unknowntext); } - /* get time in format */ time(&tm); setlocale(LC_TIME, ""); if (!strftime(buf, bufsize, timeformat, localtime(&tm))) { @@ -213,104 +178,80 @@ datetime(const char *timeformat) } setlocale(LC_TIME, "C"); - /* return time */ char *ret = smprintf("%s", buf); free(buf); return ret; } -/* disk free */ char * disk_free(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - - /* return free */ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); } -/* disk usage percentage */ char * disk_perc(const char *mountpoint) { int perc = 0; struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* calculate percent */ perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); - - /* return perc */ return smprintf("%d%%", perc); } -/* disk total */ char * disk_total(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* return total */ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); } -/* disk used */ char * disk_used(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* return used */ return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); } -/* entropy available */ char * entropy(const char *null) { int entropy = 0; FILE *fp; - /* open entropy file */ if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { fprintf(stderr, "Could not open entropy file.\n"); return smprintf(unknowntext); } - /* extract entropy */ fscanf(fp, "%d", &entropy); - - /* close entropy file */ fclose(fp); - - /* return entropy */ return smprintf("%d", entropy); } -/* gid */ char * gid(const char *null) { @@ -319,37 +260,27 @@ gid(const char *null) if ((gid = getgid()) < 0) { fprintf(stderr, "Could no get gid.\n"); return smprintf(unknowntext); - } else { + } else return smprintf("%d", gid); - } - return smprintf(unknowntext); } -/* hostname */ char * hostname(const char *null) { char hostname[HOST_NAME_MAX]; FILE *fp; - /* open hostname file */ if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { fprintf(stderr, "Could not open hostname file.\n"); return smprintf(unknowntext); } - /* extract hostname */ fscanf(fp, "%s\n", hostname); - - /* close hostname file */ fclose(fp); - - /* return entropy */ return smprintf("%s", hostname); } -/* ip address */ char * ip(const char *interface) { @@ -357,7 +288,6 @@ ip(const char *interface) int s; char host[NI_MAXHOST]; - /* check if getting ip address works */ if (getifaddrs(&ifaddr) == -1) { fprintf(stderr, "Error getting IP address.\n"); return smprintf(unknowntext); @@ -385,46 +315,35 @@ ip(const char *interface) return smprintf(unknowntext); } -/* load avg */ char * load_avg(const char *null) { double avgs[3]; - /* try to get load avg */ if (getloadavg(avgs, 3) < 0) { fprintf(stderr, "Error getting load avg.\n"); return smprintf(unknowntext); } - /* return it */ return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); } -/* ram free */ char * ram_free(const char *null) { long free; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemFree: %ld kB\n", &free); - - /* close meminfo file */ fclose(fp); - - /* return free ram as string */ return smprintf("%f", (float)free / 1024 / 1024); } -/* ram percentage */ char * ram_perc(const char *null) { @@ -432,81 +351,58 @@ ram_perc(const char *null) long total, free, buffers, cached; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers); fscanf(fp, "Cached: %ld kB\n", &cached); - /* close meminfo file */ fclose(fp); - - /* calculate percentage */ perc = 100 * ((total - free) - (buffers + cached)) / total; - - /* return perc as string */ return smprintf("%d%%", perc); } -/* ram total */ char * ram_total(const char *null) { long total; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); - - /* close meminfo file */ fclose(fp); - - /* return total ram as string */ return smprintf("%f", (float)total / 1024 / 1024); } -/* ram used */ char * ram_used(const char *null) { long free, total, buffers, cached, used; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers); fscanf(fp, "Cached: %ld kB\n", &cached); - /* close meminfo file */ fclose(fp); - - /* calculate used */ used = total - free - buffers - cached; - - /* return used ram as string */ return smprintf("%f", (float)used / 1024 / 1024); } -/* custom shell command */ char * run_command(const char* command) { @@ -514,19 +410,13 @@ run_command(const char* command) FILE *fp; char buffer[64]; - /* try to open the command output */ if (!(fp = popen(command, "r"))) { fprintf(stderr, "Could not get command output for: %s.\n", command); return smprintf(unknowntext); } - /* get command output text, save it to buffer */ fgets(buffer, sizeof(buffer) - 1, fp); - - /* close it again */ pclose(fp); - - /* add nullchar at the end */ for (int i = 0 ; i != sizeof(buffer); i++) { if (buffer[i] == '\0') { good = 1; @@ -535,35 +425,25 @@ run_command(const char* command) } if (good) buffer[strlen(buffer) - 1] = '\0'; - - /* return the output */ return smprintf("%s", buffer); } -/* temperature */ char * temp(const char *file) { int temperature; FILE *fp; - /* open temperature file */ if (!(fp = fopen(file, "r"))) { fprintf(stderr, "Could not open temperature file.\n"); return smprintf(unknowntext); } - /* extract temperature */ fscanf(fp, "%d", &temperature); - - /* close temperature file */ fclose(fp); - - /* return temperature in degrees */ return smprintf("%d°C", temperature / 1000); } -/* uptime */ char * uptime(const char *null) { @@ -571,27 +451,22 @@ uptime(const char *null) int hours = 0; int minutes = 0; - /* get info */ sysinfo(&info); hours = info.uptime / 3600; minutes = (info.uptime - hours * 3600 ) / 60; - /* return it */ return smprintf("%dh %dm", hours, minutes); } -/* username */ char * username(const char *null) { register struct passwd *pw; register uid_t uid; - /* get the values */ uid = geteuid(); pw = getpwuid(uid); - /* if it worked, return */ if (pw) return smprintf("%s", pw->pw_name); else { @@ -602,16 +477,13 @@ username(const char *null) return smprintf(unknowntext); } -/* uid */ char * uid(const char *null) { register uid_t uid; - /* get the values */ uid = geteuid(); - /* if it worked, return */ if (uid) return smprintf("%d", uid); else { @@ -623,7 +495,6 @@ uid(const char *null) } -/* alsa volume percentage */ char * vol_perc(const char *soundcard) { @@ -633,16 +504,13 @@ vol_perc(const char *soundcard) snd_mixer_elem_t *pcm_mixer, *mas_mixer; snd_mixer_selem_id_t *vol_info, *mute_info; - /* open everything */ snd_mixer_open(&handle, 0); snd_mixer_attach(handle, soundcard); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); - /* prepare everything */ snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); - /* check */ if (vol_info == NULL || mute_info == NULL) { fprintf(stderr, "Could not get alsa volume.\n"); return smprintf(unknowntext); @@ -652,12 +520,10 @@ vol_perc(const char *soundcard) pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); - /* get the info */ snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); - /* clean up */ if (vol_info) snd_mixer_selem_id_free(vol_info); if (mute_info) @@ -665,14 +531,12 @@ vol_perc(const char *soundcard) if (handle) snd_mixer_close(handle); - /* return the string (mute) */ if (!mute) return smprintf("mute"); else return smprintf("%d%%", (vol * 100) / max); } -/* wifi percentage */ char * wifi_perc(const char *wificard) { @@ -685,35 +549,26 @@ wifi_perc(const char *wificard) char needle[sizeof wificard + 1]; FILE *fp; - /* generate the path name */ memset(path, 0, sizeof path); strlcat(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); - /* open wifi file */ if(!(fp = fopen(path, "r"))) { fprintf(stderr, "Error opening wifi operstate file.\n"); return smprintf(unknowntext); } - /* read the status */ fgets(status, 5, fp); - - /* close wifi file */ fclose(fp); - - /* check if interface down */ if(strcmp(status, "up\n") != 0) return smprintf(unknowntext); - /* open wifi file */ if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "Error opening wireless file.\n"); return smprintf(unknowntext); } - /* extract the signal strength */ strlcpy(needle, wificard, sizeof(needle)); strlcat(needle, ":", sizeof(needle)); fgets(buf, bufsize, fp); @@ -724,14 +579,10 @@ wifi_perc(const char *wificard) sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength); } - /* close wifi file */ fclose(fp); - - /* return strength in percent */ return smprintf("%d%%", strength); } -/* wifi essid */ char * wifi_essid(const char *wificard) { @@ -739,14 +590,9 @@ wifi_essid(const char *wificard) int sockfd; struct iwreq wreq; - /* prepare */ memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; - - /* set the interface */ sprintf(wreq.ifr_name, wificard); - - /* check */ if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); return smprintf(unknowntext); @@ -757,32 +603,25 @@ wifi_essid(const char *wificard) return smprintf(unknowntext); } - /* return the essid */ if (strcmp((char *)wreq.u.essid.pointer, "") == 0) return smprintf(unknowntext); else return smprintf("%s", (char *)wreq.u.essid.pointer); } -/* main function */ int main(void) { char status_string[1024]; struct arg argument; - /* try to open display */ if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); exit(1); } - /* return status every interval */ for (;;) { - /* clear the string */ memset(status_string, 0, sizeof(status_string)); - - /* generate status_string */ for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; char *res = argument.func(argument.args); @@ -796,16 +635,10 @@ main(void) free(element); } - /* return the statusbar */ setstatus(status_string); - - /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */ sleep(update_interval -1); } - /* close display */ XCloseDisplay(dpy); - - /* exit successfully */ return 0; } -- cgit v1.2.3 From 46b6876d7ec6e17bb51ee647f77bd238c864ca25 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:28:42 +0300 Subject: set local function as static --- slstatus.c | 104 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 3ea29d1..c03a1d2 100644 --- a/slstatus.c +++ b/slstatus.c @@ -36,38 +36,38 @@ struct arg { const char *args; }; -void setstatus(const char *); -char *smprintf(const char *, ...); -char *battery_perc(const char *); -char *cpu_perc(const char *); -char *datetime(const char *); -char *disk_free(const char *); -char *disk_perc(const char *); -char *disk_total(const char *); -char *disk_used(const char *); -char *entropy(const char *); -char *gid(const char *); -char *hostname(const char *); -char *ip(const char *); -char *load_avg(const char *); -char *ram_free(const char *); -char *ram_perc(const char *); -char *ram_used(const char *); -char *ram_total(const char *); -char *run_command(const char *); -char *temp(const char *); -char *uid(const char *); -char *uptime(const char *); -char *username(const char *); -char *vol_perc(const char *); -char *wifi_perc(const char *); -char *wifi_essid(const char *); +static void setstatus(const char *); +static char *smprintf(const char *, ...); +static char *battery_perc(const char *); +static char *cpu_perc(const char *); +static char *datetime(const char *); +static char *disk_free(const char *); +static char *disk_perc(const char *); +static char *disk_total(const char *); +static char *disk_used(const char *); +static char *entropy(const char *); +static char *gid(const char *); +static char *hostname(const char *); +static char *ip(const char *); +static char *load_avg(const char *); +static char *ram_free(const char *); +static char *ram_perc(const char *); +static char *ram_used(const char *); +static char *ram_total(const char *); +static char *run_command(const char *); +static char *temp(const char *); +static char *uid(const char *); +static char *uptime(const char *); +static char *username(const char *); +static char *vol_perc(const char *); +static char *wifi_perc(const char *); +static char *wifi_essid(const char *); static Display *dpy; #include "config.h" -void +static void setstatus(const char *str) { /* set WM_NAME via X11 */ @@ -75,7 +75,7 @@ setstatus(const char *str) XSync(dpy, False); } -char * +static char * smprintf(const char *fmt, ...) { va_list fmtargs; @@ -89,7 +89,7 @@ smprintf(const char *fmt, ...) return ret; } -char * +static char * battery_perc(const char *battery) { int now, full, perc; @@ -128,7 +128,7 @@ battery_perc(const char *battery) return smprintf("%d%%", perc); } -char * +static char * cpu_perc(const char *null) { int perc; @@ -157,7 +157,7 @@ cpu_perc(const char *null) return smprintf("%d%%", perc); } -char * +static char * datetime(const char *timeformat) { time_t tm; @@ -183,7 +183,7 @@ datetime(const char *timeformat) return ret; } -char * +static char * disk_free(const char *mountpoint) { struct statvfs fs; @@ -195,7 +195,7 @@ disk_free(const char *mountpoint) return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); } -char * +static char * disk_perc(const char *mountpoint) { int perc = 0; @@ -210,7 +210,7 @@ disk_perc(const char *mountpoint) return smprintf("%d%%", perc); } -char * +static char * disk_total(const char *mountpoint) { struct statvfs fs; @@ -223,7 +223,7 @@ disk_total(const char *mountpoint) return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); } -char * +static char * disk_used(const char *mountpoint) { struct statvfs fs; @@ -236,7 +236,7 @@ disk_used(const char *mountpoint) return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); } -char * +static char * entropy(const char *null) { int entropy = 0; @@ -252,7 +252,7 @@ entropy(const char *null) return smprintf("%d", entropy); } -char * +static char * gid(const char *null) { gid_t gid; @@ -265,7 +265,7 @@ gid(const char *null) return smprintf(unknowntext); } -char * +static char * hostname(const char *null) { char hostname[HOST_NAME_MAX]; @@ -281,7 +281,7 @@ hostname(const char *null) return smprintf("%s", hostname); } -char * +static char * ip(const char *interface) { struct ifaddrs *ifaddr, *ifa; @@ -315,7 +315,7 @@ ip(const char *interface) return smprintf(unknowntext); } -char * +static char * load_avg(const char *null) { double avgs[3]; @@ -328,7 +328,7 @@ load_avg(const char *null) return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); } -char * +static char * ram_free(const char *null) { long free; @@ -344,7 +344,7 @@ ram_free(const char *null) return smprintf("%f", (float)free / 1024 / 1024); } -char * +static char * ram_perc(const char *null) { int perc; @@ -366,7 +366,7 @@ ram_perc(const char *null) return smprintf("%d%%", perc); } -char * +static char * ram_total(const char *null) { long total; @@ -382,7 +382,7 @@ ram_total(const char *null) return smprintf("%f", (float)total / 1024 / 1024); } -char * +static char * ram_used(const char *null) { long free, total, buffers, cached, used; @@ -403,7 +403,7 @@ ram_used(const char *null) return smprintf("%f", (float)used / 1024 / 1024); } -char * +static char * run_command(const char* command) { int good; @@ -428,7 +428,7 @@ run_command(const char* command) return smprintf("%s", buffer); } -char * +static char * temp(const char *file) { int temperature; @@ -444,7 +444,7 @@ temp(const char *file) return smprintf("%d°C", temperature / 1000); } -char * +static char * uptime(const char *null) { struct sysinfo info; @@ -458,7 +458,7 @@ uptime(const char *null) return smprintf("%dh %dm", hours, minutes); } -char * +static char * username(const char *null) { register struct passwd *pw; @@ -477,7 +477,7 @@ username(const char *null) return smprintf(unknowntext); } -char * +static char * uid(const char *null) { register uid_t uid; @@ -495,7 +495,7 @@ uid(const char *null) } -char * +static char * vol_perc(const char *soundcard) { int mute = 0; @@ -537,7 +537,7 @@ vol_perc(const char *soundcard) return smprintf("%d%%", (vol * 100) / max); } -char * +static char * wifi_perc(const char *wificard) { int bufsize = 255; @@ -583,7 +583,7 @@ wifi_perc(const char *wificard) return smprintf("%d%%", strength); } -char * +static char * wifi_essid(const char *wificard) { char id[IW_ESSID_MAX_SIZE+1]; -- cgit v1.2.3 From b4f55e7170576a2dd800018c8690832bbc7f9bf0 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 12:00:51 +0300 Subject: (void)ed the prototypes --- slstatus.c | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index c03a1d2..40aaa19 100644 --- a/slstatus.c +++ b/slstatus.c @@ -39,26 +39,26 @@ struct arg { static void setstatus(const char *); static char *smprintf(const char *, ...); static char *battery_perc(const char *); -static char *cpu_perc(const char *); +static char *cpu_perc(void); static char *datetime(const char *); static char *disk_free(const char *); static char *disk_perc(const char *); static char *disk_total(const char *); static char *disk_used(const char *); -static char *entropy(const char *); -static char *gid(const char *); -static char *hostname(const char *); +static char *entropy(void); +static char *gid(void); +static char *hostname(void); static char *ip(const char *); -static char *load_avg(const char *); -static char *ram_free(const char *); -static char *ram_perc(const char *); -static char *ram_used(const char *); -static char *ram_total(const char *); +static char *load_avg(void); +static char *ram_free(void); +static char *ram_perc(void); +static char *ram_used(void); +static char *ram_total(void); static char *run_command(const char *); static char *temp(const char *); -static char *uid(const char *); -static char *uptime(const char *); -static char *username(const char *); +static char *uid(void); +static char *uptime(void); +static char *username(void); static char *vol_perc(const char *); static char *wifi_perc(const char *); static char *wifi_essid(const char *); @@ -129,7 +129,7 @@ battery_perc(const char *battery) } static char * -cpu_perc(const char *null) +cpu_perc(void) { int perc; long double a[4], b[4]; @@ -237,7 +237,7 @@ disk_used(const char *mountpoint) } static char * -entropy(const char *null) +entropy(void) { int entropy = 0; FILE *fp; @@ -253,7 +253,7 @@ entropy(const char *null) } static char * -gid(const char *null) +gid(void) { gid_t gid; @@ -266,7 +266,7 @@ gid(const char *null) } static char * -hostname(const char *null) +hostname(void) { char hostname[HOST_NAME_MAX]; FILE *fp; @@ -316,7 +316,7 @@ ip(const char *interface) } static char * -load_avg(const char *null) +load_avg(void) { double avgs[3]; @@ -329,7 +329,7 @@ load_avg(const char *null) } static char * -ram_free(const char *null) +ram_free(void) { long free; FILE *fp; @@ -345,7 +345,7 @@ ram_free(const char *null) } static char * -ram_perc(const char *null) +ram_perc(void) { int perc; long total, free, buffers, cached; @@ -367,7 +367,7 @@ ram_perc(const char *null) } static char * -ram_total(const char *null) +ram_total(void) { long total; FILE *fp; @@ -383,7 +383,7 @@ ram_total(const char *null) } static char * -ram_used(const char *null) +ram_used(void) { long free, total, buffers, cached, used; FILE *fp; @@ -445,7 +445,7 @@ temp(const char *file) } static char * -uptime(const char *null) +uptime(void) { struct sysinfo info; int hours = 0; @@ -459,7 +459,7 @@ uptime(const char *null) } static char * -username(const char *null) +username(void) { register struct passwd *pw; register uid_t uid; @@ -478,7 +478,7 @@ username(const char *null) } static char * -uid(const char *null) +uid(void) { register uid_t uid; @@ -624,7 +624,10 @@ main(void) memset(status_string, 0, sizeof(status_string)); for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; - char *res = argument.func(argument.args); + if (argument.args == NULL) + char *res = argument.func(); + else + char *res = argument.func(argument.args); char *element = smprintf(argument.format, res); if (element == NULL) { element = smprintf(unknowntext); -- cgit v1.2.3 From f65fb9bca18bb445de2c337ed9e4d84de5b631f7 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:19:45 +0300 Subject: fixed the code, works now --- slstatus.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 40aaa19..b0e3eef 100644 --- a/slstatus.c +++ b/slstatus.c @@ -29,7 +29,7 @@ #include "strlcat.h" #include "strlcpy.h" -typedef char *(*op_fun) (const char *); +typedef char *(*op_fun)(); struct arg { op_fun func; const char *format; @@ -255,14 +255,8 @@ entropy(void) static char * gid(void) { - gid_t gid; - - if ((gid = getgid()) < 0) { - fprintf(stderr, "Could no get gid.\n"); - return smprintf(unknowntext); - } else - return smprintf("%d", gid); - return smprintf(unknowntext); + gid_t gid = getgid(); + return smprintf("%d", gid); } static char * @@ -612,9 +606,21 @@ wifi_essid(const char *wificard) int main(void) { + size_t i; char status_string[1024]; + char *res, *element; struct arg argument; + /* get rid of unused functions warning */ + if (0) { setstatus(""); battery_perc(""); cpu_perc(); + datetime(""); disk_free(""); disk_perc(""); + disk_total(""); disk_used(""); entropy(); + gid(); hostname(); ip(""); load_avg(); + ram_free(); ram_perc(); ram_used(); ram_total(); + run_command(""); temp(""); uid(); uptime(); + username(); vol_perc(""); wifi_perc(""); + wifi_essid(""); } + if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); exit(1); @@ -622,13 +628,13 @@ main(void) for (;;) { memset(status_string, 0, sizeof(status_string)); - for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { + for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; if (argument.args == NULL) - char *res = argument.func(); + res = argument.func(); else - char *res = argument.func(argument.args); - char *element = smprintf(argument.format, res); + res = argument.func(argument.args); + element = smprintf(argument.format, res); if (element == NULL) { element = smprintf(unknowntext); fprintf(stderr, "Failed to format output.\n"); -- cgit v1.2.3 From 6f31ca6ce3ea722c7d1ecfe2099108133ce4215c Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:43:57 +0300 Subject: fixed compiler warnings in a better way --- slstatus.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index b0e3eef..7bd3b8c 100644 --- a/slstatus.c +++ b/slstatus.c @@ -611,16 +611,6 @@ main(void) char *res, *element; struct arg argument; - /* get rid of unused functions warning */ - if (0) { setstatus(""); battery_perc(""); cpu_perc(); - datetime(""); disk_free(""); disk_perc(""); - disk_total(""); disk_used(""); entropy(); - gid(); hostname(); ip(""); load_avg(); - ram_free(); ram_perc(); ram_used(); ram_total(); - run_command(""); temp(""); uid(); uptime(); - username(); vol_perc(""); wifi_perc(""); - wifi_essid(""); } - if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); exit(1); -- cgit v1.2.3 From ff013b5fd4e059ae7c9e07ff2ac0e669b7dac136 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 16:00:34 +0300 Subject: status_string can hold 4096 bytes now --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 7bd3b8c..60b5bbb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -607,7 +607,7 @@ int main(void) { size_t i; - char status_string[1024]; + char status_string[4096]; char *res, *element; struct arg argument; -- cgit v1.2.3 From f9f72a06f11de9ecd1b14811eba63c6e8c7a1165 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 13:27:42 +0300 Subject: removed unnecessary typecasts (might be a reason for snd_mixer_selem_get_playback_volume_range bug --- slstatus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 60b5bbb..5c7659c 100644 --- a/slstatus.c +++ b/slstatus.c @@ -514,8 +514,8 @@ vol_perc(const char *soundcard) pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); - snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); - snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); + snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max); + snd_mixer_selem_get_playback_volume(pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); if (vol_info) -- cgit v1.2.3 From 447283fda1505124f68f0cf74833f307cb630fa3 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 25 Aug 2016 23:26:17 +0300 Subject: removing typecasts for pcm_mixer cause more issues than before --- slstatus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 5c7659c..60b5bbb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -514,8 +514,8 @@ vol_perc(const char *soundcard) pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); - snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max); - snd_mixer_selem_get_playback_volume(pcm_mixer, SND_MIXER_SCHN_MONO, &vol); + snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); + snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); if (vol_info) -- cgit v1.2.3 From 47ddf9382e6a02bdfce9c78306b694b4a44203cb Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:20:50 +0300 Subject: used constant string literals && remote initialization to in battery_perc() && trying to fix possible buffer overflow --- slstatus.c | 93 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 45 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 60b5bbb..90534d9 100644 --- a/slstatus.c +++ b/slstatus.c @@ -79,13 +79,16 @@ static char * smprintf(const char *fmt, ...) { va_list fmtargs; + char tmp[120]; char *ret = NULL; va_start(fmtargs, fmt); - if (vasprintf(&ret, fmt, fmtargs) < 0) + snprintf(tmp, sizeof(tmp)-1, fmt, fmtargs); + tmp[sizeof(tmp)] = '\0'; + if (asprintf(&ret, "%s", tmp) < 0) return NULL; - va_end(fmtargs); + va_end(fmtargs); return ret; } @@ -93,23 +96,23 @@ static char * battery_perc(const char *battery) { int now, full, perc; - char batterynowfile[64] = ""; - char batteryfullfile[64] = ""; + char batterynowfile[64]; + char batteryfullfile[64]; FILE *fp; - strlcat(batterynowfile, batterypath, sizeof(batterynowfile)); + strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); strlcat(batterynowfile, "/", sizeof(batterynowfile)); - strlcat(batterynowfile, batterynow, sizeof(batterynowfile)); + strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile)); - strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile)); + strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile)); strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); - strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile)); + strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); if (!(fp = fopen(batterynowfile, "r"))) { fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%i", &now); @@ -117,7 +120,7 @@ battery_perc(const char *battery) if (!(fp = fopen(batteryfullfile, "r"))) { fprintf(stderr, "Error opening battery file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%i", &full); @@ -137,7 +140,7 @@ cpu_perc(void) if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); @@ -148,7 +151,7 @@ cpu_perc(void) if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]); @@ -165,7 +168,7 @@ datetime(const char *timeformat) char *buf = malloc(bufsize); if (buf == NULL) { fprintf(stderr, "Failed to get date/time.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } time(&tm); @@ -174,7 +177,7 @@ datetime(const char *timeformat) setlocale(LC_TIME, "C"); free(buf); fprintf(stderr, "Strftime failed.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } setlocale(LC_TIME, "C"); @@ -190,7 +193,7 @@ disk_free(const char *mountpoint) if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); } @@ -203,7 +206,7 @@ disk_perc(const char *mountpoint) if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); @@ -217,7 +220,7 @@ disk_total(const char *mountpoint) if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); @@ -230,7 +233,7 @@ disk_used(const char *mountpoint) if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); @@ -244,7 +247,7 @@ entropy(void) if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { fprintf(stderr, "Could not open entropy file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%d", &entropy); @@ -267,7 +270,7 @@ hostname(void) if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { fprintf(stderr, "Could not open hostname file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%s\n", hostname); @@ -284,7 +287,7 @@ ip(const char *interface) if (getifaddrs(&ifaddr) == -1) { fprintf(stderr, "Error getting IP address.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } /* get the ip address */ @@ -297,7 +300,7 @@ ip(const char *interface) if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { fprintf(stderr, "Error getting IP address.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%s", host); } @@ -306,7 +309,7 @@ ip(const char *interface) /* free the address */ freeifaddrs(ifaddr); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } static char * @@ -316,7 +319,7 @@ load_avg(void) if (getloadavg(avgs, 3) < 0) { fprintf(stderr, "Error getting load avg.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); @@ -330,7 +333,7 @@ ram_free(void) if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemFree: %ld kB\n", &free); @@ -347,7 +350,7 @@ ram_perc(void) if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -368,7 +371,7 @@ ram_total(void) if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -384,7 +387,7 @@ ram_used(void) if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -406,7 +409,7 @@ run_command(const char* command) if (!(fp = popen(command, "r"))) { fprintf(stderr, "Could not get command output for: %s.\n", command); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fgets(buffer, sizeof(buffer) - 1, fp); @@ -430,7 +433,7 @@ temp(const char *file) if (!(fp = fopen(file, "r"))) { fprintf(stderr, "Could not open temperature file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%d", &temperature); @@ -465,10 +468,10 @@ username(void) return smprintf("%s", pw->pw_name); else { fprintf(stderr, "Could not get username.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } static char * @@ -482,10 +485,10 @@ uid(void) return smprintf("%d", uid); else { fprintf(stderr, "Could not get uid.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } @@ -507,10 +510,10 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { fprintf(stderr, "Could not get alsa volume.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } - snd_mixer_selem_id_set_name(vol_info, channel); - snd_mixer_selem_id_set_name(mute_info, channel); + snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); + snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL); pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); @@ -550,17 +553,17 @@ wifi_perc(const char *wificard) if(!(fp = fopen(path, "r"))) { fprintf(stderr, "Error opening wifi operstate file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } fgets(status, 5, fp); fclose(fp); if(strcmp(status, "up\n") != 0) - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "Error opening wireless file.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } strlcpy(needle, wificard, sizeof(needle)); @@ -589,16 +592,16 @@ wifi_essid(const char *wificard) sprintf(wreq.ifr_name, wificard); if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { fprintf(stderr, "Get ESSID ioctl failed for interface %s\n", wificard); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } if (strcmp((char *)wreq.u.essid.pointer, "") == 0) - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); else return smprintf("%s", (char *)wreq.u.essid.pointer); } @@ -626,7 +629,7 @@ main(void) res = argument.func(argument.args); element = smprintf(argument.format, res); if (element == NULL) { - element = smprintf(unknowntext); + element = smprintf(UNKNOWN_STR); fprintf(stderr, "Failed to format output.\n"); } strlcat(status_string, element, sizeof(status_string)); @@ -635,7 +638,7 @@ main(void) } setstatus(status_string); - sleep(update_interval -1); + sleep(UPDATE_INTERVAL -1); } XCloseDisplay(dpy); -- cgit v1.2.3 From 25eb9ff35e76312b09ff5613c9a3cc1275938680 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:30:12 +0300 Subject: FIXME: buffer overflow warning --- slstatus.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 90534d9..4dbe650 100644 --- a/slstatus.c +++ b/slstatus.c @@ -78,17 +78,17 @@ setstatus(const char *str) static char * smprintf(const char *fmt, ...) { - va_list fmtargs; - char tmp[120]; + /* FIXME: This code should have + bound checks, it is vulnerable to + buffer overflows */ + va_list ap; char *ret = NULL; - va_start(fmtargs, fmt); - snprintf(tmp, sizeof(tmp)-1, fmt, fmtargs); - tmp[sizeof(tmp)] = '\0'; - if (asprintf(&ret, "%s", tmp) < 0) + va_start(ap, fmt); + if (vasprintf(&ret, fmt, ap) < 0) return NULL; - va_end(fmtargs); + va_end(ap); return ret; } -- cgit v1.2.3 From 1d257999ed6049dce4d1305c4dc3304ea9910ca7 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:39:04 +0300 Subject: removed heap dependency in datetime() and simplified the function --- slstatus.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 4dbe650..628c0f4 100644 --- a/slstatus.c +++ b/slstatus.c @@ -163,27 +163,14 @@ cpu_perc(void) static char * datetime(const char *timeformat) { - time_t tm; - size_t bufsize = 64; - char *buf = malloc(bufsize); - if (buf == NULL) { - fprintf(stderr, "Failed to get date/time.\n"); - return smprintf(UNKNOWN_STR); - } + time_t t; + char timestr[80]; - time(&tm); - setlocale(LC_TIME, ""); - if (!strftime(buf, bufsize, timeformat, localtime(&tm))) { - setlocale(LC_TIME, "C"); - free(buf); - fprintf(stderr, "Strftime failed.\n"); + t = time(NULL); + if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0) return smprintf(UNKNOWN_STR); - } - setlocale(LC_TIME, "C"); - char *ret = smprintf("%s", buf); - free(buf); - return ret; + return smprintf("%s", timestr); } static char * -- cgit v1.2.3 From 94a62b864b56d8bad1fb68925dcee7c71015bc54 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 18:19:53 +0300 Subject: worked around the buffer overrun in smprintf() --- slstatus.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 628c0f4..08866d2 100644 --- a/slstatus.c +++ b/slstatus.c @@ -78,14 +78,15 @@ setstatus(const char *str) static char * smprintf(const char *fmt, ...) { - /* FIXME: This code should have - bound checks, it is vulnerable to - buffer overflows */ va_list ap; + char tmp[120]; char *ret = NULL; va_start(ap, fmt); - if (vasprintf(&ret, fmt, ap) < 0) + vsnprintf(tmp, sizeof(tmp)-1, fmt, ap); + tmp[strlen(tmp)+1] = '\0'; + + if (asprintf(&ret, "%s", tmp) < 0) return NULL; va_end(ap); -- cgit v1.2.3 From 5829cee24ea0e40cbe402728d1ca2f0bfb630182 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 19:27:01 +0300 Subject: used a different implementation of smprintf() imported from dwmstatus --- slstatus.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 08866d2..bc48365 100644 --- a/slstatus.c +++ b/slstatus.c @@ -79,17 +79,23 @@ static char * smprintf(const char *fmt, ...) { va_list ap; - char tmp[120]; - char *ret = NULL; + char *ret; + int len; va_start(ap, fmt); - vsnprintf(tmp, sizeof(tmp)-1, fmt, ap); - tmp[strlen(tmp)+1] = '\0'; + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); - if (asprintf(&ret, "%s", tmp) < 0) - return NULL; + ret = malloc(++len); + if (ret == NULL) { + perror("malloc"); + exit(1); + } + va_start(ap, fmt); + vsnprintf(ret, len, fmt, ap); va_end(ap); + return ret; } -- cgit v1.2.3 From 0aefd57f9f469aba25ffbe9bdda04e894b9d739a Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 30 Aug 2016 21:50:40 +0300 Subject: 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 --- slstatus.c | 155 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 93 insertions(+), 62 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index bc48365..163b44e 100644 --- a/slstatus.c +++ b/slstatus.c @@ -105,7 +105,7 @@ battery_perc(const char *battery) int now, full, perc; char batterynowfile[64]; char batteryfullfile[64]; - FILE *fp; + FILE *fp = fopen(batterynowfile, "r"); strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); @@ -117,16 +117,20 @@ battery_perc(const char *battery) strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); - if (!(fp = fopen(batterynowfile, "r"))) { - fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); + if (fp == NULL ) { + fprintf(stderr, "Error opening battery file: %s: %s\n", + batterynowfile, + strerror(errno)); return smprintf(UNKNOWN_STR); } fscanf(fp, "%i", &now); fclose(fp); - if (!(fp = fopen(batteryfullfile, "r"))) { - fprintf(stderr, "Error opening battery file.\n"); + fp = fopen(batteryfullfile, "r"); + if (fp == NULL) { + fprintf(stderr, "Error opening battery file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -143,10 +147,11 @@ cpu_perc(void) { int perc; long double a[4], b[4]; - FILE *fp; + FILE *fp = fopen("/proc/stat","r"); - if (!(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); } @@ -156,8 +161,10 @@ cpu_perc(void) /* wait a second (for avg values) */ sleep(1); - if (!(fp = fopen("/proc/stat","r"))) { - fprintf(stderr, "Error opening stat file.\n"); + fp = fopen("/proc/stat","r"); + if (fp == NULL) { + fprintf(stderr, "Error opening stat file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -186,7 +193,8 @@ disk_free(const char *mountpoint) struct statvfs fs; 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("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); @@ -199,7 +207,8 @@ disk_perc(const char *mountpoint) struct statvfs fs; 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); } @@ -213,7 +222,8 @@ disk_total(const char *mountpoint) struct statvfs fs; 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); } @@ -226,7 +236,8 @@ disk_used(const char *mountpoint) struct statvfs fs; 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); } @@ -237,9 +248,9 @@ static char * entropy(void) { 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"); return smprintf(UNKNOWN_STR); } @@ -260,14 +271,18 @@ static char * hostname(void) { char hostname[HOST_NAME_MAX]; - FILE *fp; + FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); - if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { - fprintf(stderr, "Could not open hostname file.\n"); + if (fp == NULL) { + fprintf(stderr, "Could not open hostname file: %s\n", + strerror(errno)); 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); return smprintf("%s", hostname); } @@ -280,7 +295,8 @@ ip(const char *interface) char host[NI_MAXHOST]; 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); } @@ -289,7 +305,8 @@ ip(const char *interface) if (ifa->ifa_addr == NULL) 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 (s != 0) { @@ -323,10 +340,11 @@ static char * ram_free(void) { long free; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -340,10 +358,11 @@ ram_perc(void) { int perc; long total, free, buffers, cached; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -361,10 +380,11 @@ static char * ram_total(void) { long total; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -377,10 +397,11 @@ static char * ram_used(void) { long free, total, buffers, cached, used; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -398,15 +419,16 @@ static char * run_command(const char* command) { int good; - FILE *fp; + FILE *fp = popen(command, "r"); char buffer[64]; - if (!(fp = popen(command, "r"))) { - fprintf(stderr, "Could not get command output for: %s.\n", command); + if (fp == NULL) { + fprintf(stderr, "Could not get command output for: %s: %s\n", + command, strerror(errno)); return smprintf(UNKNOWN_STR); } - fgets(buffer, sizeof(buffer) - 1, fp); + fgets(buffer, sizeof(buffer)-1, fp); pclose(fp); for (int i = 0 ; i != sizeof(buffer); i++) { if (buffer[i] == '\0') { @@ -415,7 +437,7 @@ run_command(const char* command) } } if (good) - buffer[strlen(buffer) - 1] = '\0'; + buffer[strlen(buffer)-1] = '\0'; return smprintf("%s", buffer); } @@ -423,10 +445,11 @@ static char * temp(const char *file) { int temperature; - FILE *fp; + FILE *fp = fopen(file, "r"); - if (!(fp = fopen(file, "r"))) { - fprintf(stderr, "Could not open temperature file.\n"); + if (fp == NULL) { + fprintf(stderr, "Could not open temperature file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -461,7 +484,8 @@ username(void) if (pw) return smprintf("%s", pw->pw_name); else { - fprintf(stderr, "Could not get username.\n"); + fprintf(stderr, "Could not get username: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -471,6 +495,7 @@ username(void) static char * uid(void) { + /* FIXME: WHY USE register modifier? */ register uid_t uid; uid = geteuid(); @@ -531,22 +556,21 @@ vol_perc(const char *soundcard) static char * wifi_perc(const char *wificard) { - int bufsize = 255; int strength; - char buf[bufsize]; + char buf[255]; char *datastart; char path[64]; char status[5]; - char needle[sizeof wificard + 1]; - FILE *fp; + char needle[strlen(wificard)+2]; + FILE *fp = fopen(path, "r"); - memset(path, 0, sizeof path); - strlcat(path, "/sys/class/net/", sizeof(path)); + strlcpy(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); - if(!(fp = fopen(path, "r"))) { - fprintf(stderr, "Error opening wifi operstate file.\n"); + if(fp == NULL) { + fprintf(stderr, "Error opening wifi operstate file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -555,17 +579,21 @@ wifi_perc(const char *wificard) if(strcmp(status, "up\n") != 0) return smprintf(UNKNOWN_STR); - if (!(fp = fopen("/proc/net/wireless", "r"))) { - fprintf(stderr, "Error opening wireless file.\n"); + fp = fopen("/proc/net/wireless", "r"); + if (fp == NULL) { + fprintf(stderr, "Error opening wireless file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } strlcpy(needle, wificard, sizeof(needle)); strlcat(needle, ":", sizeof(needle)); - fgets(buf, bufsize, fp); - fgets(buf, bufsize, fp); - fgets(buf, bufsize, fp); - if ((datastart = strstr(buf, needle)) != NULL) { + fgets(buf, sizeof(buf), fp); + fgets(buf, sizeof(buf), fp); + fgets(buf, sizeof(buf), fp); + + datastart = strstr(buf, needle); + if (datastart != NULL) { datastart = strstr(buf, ":"); 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) { char id[IW_ESSID_MAX_SIZE+1]; - int sockfd; + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); struct iwreq wreq; memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); - if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); + if(sockfd == -1) { + fprintf(stderr, "Cannot open socket for interface: %s: %s\n", + wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; 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); } @@ -608,7 +638,8 @@ main(void) char *res, *element; struct arg argument; - if (!(dpy = XOpenDisplay(0x0))) { + dpy = XOpenDisplay(0x0); + if (!dpy) { fprintf(stderr, "Cannot open display!\n"); exit(1); } -- cgit v1.2.3 From cd3c084957483c4192edf2e8feb4c759668a1055 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 06:00:14 +0300 Subject: slow down boy! you opened the file too early! --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 163b44e..5246bc0 100644 --- a/slstatus.c +++ b/slstatus.c @@ -105,7 +105,7 @@ battery_perc(const char *battery) int now, full, perc; char batterynowfile[64]; char batteryfullfile[64]; - FILE *fp = fopen(batterynowfile, "r"); + FILE *fp; strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); @@ -117,6 +117,7 @@ battery_perc(const char *battery) strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); + fp = fopen(batterynowfile, "r"); if (fp == NULL ) { fprintf(stderr, "Error opening battery file: %s: %s\n", batterynowfile, -- cgit v1.2.3 From 5e4dc85bb9bf821d87bcf8a13e0f0843fd68bfe1 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 06:10:53 +0300 Subject: forgot to give entropy() some candy too --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 5246bc0..f070e1d 100644 --- a/slstatus.c +++ b/slstatus.c @@ -252,7 +252,8 @@ entropy(void) FILE *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: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } -- cgit v1.2.3 From aa70b6633016ed35f9fa3d91c26751e973ac5d63 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 14:29:27 +0300 Subject: opened the file before the path concatenates, what an idiot --- slstatus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index f070e1d..a9aeecb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -564,12 +564,14 @@ wifi_perc(const char *wificard) char path[64]; char status[5]; char needle[strlen(wificard)+2]; - FILE *fp = fopen(path, "r"); + FILE *fp; strlcpy(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); + fp = fopen(path, "r"); + if(fp == NULL) { fprintf(stderr, "Error opening wifi operstate file: %s\n", strerror(errno)); -- cgit v1.2.3 From 9e92d41db54b5c1128505ed778aa0feb9dd6457f Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:02:17 +0300 Subject: removed unneeded headers --- slstatus.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index a9aeecb..7c625d1 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,12 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include -#include #include #include #include #include -#include #include #include #include -- cgit v1.2.3 From 825141633bad2264c67c38ebcaab63f4c96631d3 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:15:40 +0300 Subject: removed unnecessary typecast and added more comments --- slstatus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 7c625d1..d360977 100644 --- a/slstatus.c +++ b/slstatus.c @@ -27,9 +27,8 @@ #include "strlcat.h" #include "strlcpy.h" -typedef char *(*op_fun)(); struct arg { - op_fun func; + char *(*func)(); const char *format; const char *args; }; @@ -475,6 +474,7 @@ uptime(void) static char * username(void) { + /* FIXME: WHY USE REGISTER MODIFIER? */ register struct passwd *pw; register uid_t uid; @@ -514,6 +514,10 @@ uid(void) static char * vol_perc(const char *soundcard) { + /* + * TODO: FIXME: + * https://github.com/drkh5h/slstatus/issues/12 + */ int mute = 0; long vol = 0, max = 0, min = 0; snd_mixer_t *handle; -- cgit v1.2.3 From 2f8335abf0d6347100cf63d82e8151ad0ff97ab0 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:35:32 +0300 Subject: simplified uid() and fixed username() --- slstatus.c | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index d360977..7337e4a 100644 --- a/slstatus.c +++ b/slstatus.c @@ -474,40 +474,21 @@ uptime(void) static char * username(void) { - /* FIXME: WHY USE REGISTER MODIFIER? */ - register struct passwd *pw; - register uid_t uid; + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); - uid = geteuid(); - pw = getpwuid(uid); - - if (pw) + if (pw == NULL) return smprintf("%s", pw->pw_name); - else { - fprintf(stderr, "Could not get username: %s\n", - strerror(errno)); - return smprintf(UNKNOWN_STR); - } + fprintf(stderr, "Could not get username: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } static char * uid(void) { - /* FIXME: WHY USE register modifier? */ - register uid_t uid; - - uid = geteuid(); - - if (uid) - return smprintf("%d", uid); - else { - fprintf(stderr, "Could not get uid.\n"); - return smprintf(UNKNOWN_STR); - } - - return smprintf(UNKNOWN_STR); + return smprintf("%d", geteuid()); } -- cgit v1.2.3 From d3a212da7e09f11753aafd76f16dccb1ade822e1 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:51:32 +0300 Subject: simplified gid() --- slstatus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 7337e4a..a3b6b6a 100644 --- a/slstatus.c +++ b/slstatus.c @@ -262,8 +262,7 @@ entropy(void) static char * gid(void) { - gid_t gid = getgid(); - return smprintf("%d", gid); + return smprintf("%d", getgid()); } static char * -- cgit v1.2.3 From d1ae2e785d26d944fb4b228c2e025528cd8b780b Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:54:00 +0300 Subject: NOTREACHED comment --- slstatus.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index a3b6b6a..368b51c 100644 --- a/slstatus.c +++ b/slstatus.c @@ -652,6 +652,13 @@ main(void) sleep(UPDATE_INTERVAL -1); } + /* NOT REACHED */ + /* + * TODO: find out a way to exit successfully + * to prevent memory leaks + */ +/* XCloseDisplay(dpy); return 0; +*/ } -- cgit v1.2.3 From b650c438f0dc049d98e339e66eeffd7650774a5d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Fri, 2 Sep 2016 22:13:58 +0300 Subject: removed setstatus() && simplified main() --- slstatus.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 368b51c..2e57fbb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -33,7 +33,6 @@ struct arg { const char *args; }; -static void setstatus(const char *); static char *smprintf(const char *, ...); static char *battery_perc(const char *); static char *cpu_perc(void); @@ -64,14 +63,6 @@ static Display *dpy; #include "config.h" -static void -setstatus(const char *str) -{ - /* set WM_NAME via X11 */ - XStoreName(dpy, DefaultRootWindow(dpy), str); - XSync(dpy, False); -} - static char * smprintf(const char *fmt, ...) { @@ -625,10 +616,6 @@ main(void) struct arg argument; dpy = XOpenDisplay(0x0); - if (!dpy) { - fprintf(stderr, "Cannot open display!\n"); - exit(1); - } for (;;) { memset(status_string, 0, sizeof(status_string)); @@ -648,7 +635,8 @@ main(void) free(element); } - setstatus(status_string); + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); sleep(UPDATE_INTERVAL -1); } @@ -657,8 +645,6 @@ main(void) * TODO: find out a way to exit successfully * to prevent memory leaks */ -/* XCloseDisplay(dpy); return 0; -*/ } -- cgit v1.2.3 From de4f20ace35037a510aafcf49a3d78637d7248b6 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sat, 3 Sep 2016 21:43:05 +0300 Subject: removed UPDATE_INTERVAL, it is neat to have it but removing it is a tradeoff worth making, because the clock would act weird if this used to work with cpu_perc(). --- slstatus.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 2e57fbb..ab461c8 100644 --- a/slstatus.c +++ b/slstatus.c @@ -147,7 +147,6 @@ cpu_perc(void) fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); fclose(fp); - /* wait a second (for avg values) */ sleep(1); fp = fopen("/proc/stat","r"); @@ -637,7 +636,6 @@ main(void) XStoreName(dpy, DefaultRootWindow(dpy), status_string); XSync(dpy, False); - sleep(UPDATE_INTERVAL -1); } /* NOT REACHED */ -- cgit v1.2.3 From d3d8b8ee03a773b7f2cd1aad16ee43d9784e5139 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 4 Sep 2016 00:10:49 +0300 Subject: added daemonization support --- slstatus.c | 102 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 27 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index ab461c8..5c83a84 100644 --- a/slstatus.c +++ b/slstatus.c @@ -24,8 +24,15 @@ #undef strlcat #undef strlcpy +#include "arg.h" #include "strlcat.h" #include "strlcpy.h" +#include "concat.h" + +char *argv0; +char concat[]; + +FILE *foutput; struct arg { char *(*func)(); @@ -107,7 +114,7 @@ battery_perc(const char *battery) fp = fopen(batterynowfile, "r"); if (fp == NULL ) { - fprintf(stderr, "Error opening battery file: %s: %s\n", + fprintf(foutput, "Error opening battery file: %s: %s\n", batterynowfile, strerror(errno)); return smprintf(UNKNOWN_STR); @@ -118,7 +125,7 @@ battery_perc(const char *battery) fp = fopen(batteryfullfile, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s\n", + fprintf(foutput, "Error opening battery file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -139,7 +146,7 @@ cpu_perc(void) FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -151,7 +158,7 @@ cpu_perc(void) fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -181,7 +188,7 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -195,7 +202,7 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -210,7 +217,7 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -224,7 +231,7 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -239,7 +246,7 @@ entropy(void) FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open entropy file: %s\n", + fprintf(foutput, "Could not open entropy file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -262,7 +269,7 @@ hostname(void) FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open hostname file: %s\n", + fprintf(foutput, "Could not open hostname file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -283,7 +290,7 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address: %s\n", + fprintf(foutput, "Error getting IP address: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -298,7 +305,7 @@ ip(const char *interface) if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - fprintf(stderr, "Error getting IP address.\n"); + fprintf(foutput, "Error getting IP address.\n"); return smprintf(UNKNOWN_STR); } return smprintf("%s", host); @@ -317,7 +324,7 @@ load_avg(void) double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(stderr, "Error getting load avg.\n"); + fprintf(foutput, "Error getting load avg.\n"); return smprintf(UNKNOWN_STR); } @@ -331,7 +338,7 @@ ram_free(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -349,7 +356,7 @@ ram_perc(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -371,7 +378,7 @@ ram_total(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -388,7 +395,7 @@ ram_used(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -411,7 +418,7 @@ run_command(const char* command) char buffer[64]; if (fp == NULL) { - fprintf(stderr, "Could not get command output for: %s: %s\n", + fprintf(foutput, "Could not get command output for: %s: %s\n", command, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -436,7 +443,7 @@ temp(const char *file) FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(stderr, "Could not open temperature file: %s\n", + fprintf(foutput, "Could not open temperature file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -469,7 +476,7 @@ username(void) if (pw == NULL) return smprintf("%s", pw->pw_name); - fprintf(stderr, "Could not get username: %s\n", + fprintf(foutput, "Could not get username: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -502,7 +509,7 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + fprintf(foutput, "Could not get alsa volume.\n"); return smprintf(UNKNOWN_STR); } snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); @@ -545,7 +552,7 @@ wifi_perc(const char *wificard) fp = fopen(path, "r"); if(fp == NULL) { - fprintf(stderr, "Error opening wifi operstate file: %s\n", + fprintf(foutput, "Error opening wifi operstate file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -557,7 +564,7 @@ wifi_perc(const char *wificard) fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening wireless file: %s\n", + fprintf(foutput, "Error opening wireless file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -589,13 +596,13 @@ wifi_essid(const char *wificard) wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(stderr, "Cannot open socket for interface: %s: %s\n", + fprintf(foutput, "Cannot open socket for interface: %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", + fprintf(foutput, "Get ESSID ioctl failed for interface %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -606,16 +613,57 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } +void +usage(void) +{ + fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); + exit(0); +} + int -main(void) +main(int argc, char *argv[]) { + unsigned short int dflag, lflag; size_t i; - char status_string[4096]; + char status_string[4096], logpath[4096]; char *res, *element; struct arg argument; + foutput = stderr; dpy = XOpenDisplay(0x0); + ARGBEGIN { + case 'd': + dflag = 1; + break; + case 'l': + strlcpy(logpath, EARGF(usage()), sizeof(logpath)-1); + logpath[strlen(logpath)+1] = '\0'; + foutput = fopen(logpath, "a"); + if (foutput == NULL) { + fprintf(stderr, "failed to open log file at %s: %s\n", + logpath, strerror(errno)); + return (1); + } + lflag = 1; + break; + default: + usage(); + } ARGEND + + if (dflag && !lflag) { + ccat(2, getenv("HOME"), "/.slstatus_log"); + foutput = fopen(concat, "a"); + if (foutput == NULL) { + fprintf(stderr, "failed to open log file at %s: %s\n", + logpath, strerror(errno)); + return (1); + } + } + + if (dflag) + daemon(0, 0); + for (;;) { memset(status_string, 0, sizeof(status_string)); for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { -- cgit v1.2.3 From 9fa858daeafb5398cdf19af9483b2854fe6e9772 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:13:48 +0300 Subject: added a tool for resetting the status bar && worked around some issues && removed the makefile (we need a better one) --- slstatus.c | 148 ++++++++++++++++++++----------------------------------------- 1 file changed, 47 insertions(+), 101 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 5c83a84..dae5e8a 100644 --- a/slstatus.c +++ b/slstatus.c @@ -24,15 +24,8 @@ #undef strlcat #undef strlcpy -#include "arg.h" #include "strlcat.h" #include "strlcpy.h" -#include "concat.h" - -char *argv0; -char concat[]; - -FILE *foutput; struct arg { char *(*func)(); @@ -114,7 +107,7 @@ battery_perc(const char *battery) fp = fopen(batterynowfile, "r"); if (fp == NULL ) { - fprintf(foutput, "Error opening battery file: %s: %s\n", + fprintf(stderr, "Error opening battery file: %s: %s\n", batterynowfile, strerror(errno)); return smprintf(UNKNOWN_STR); @@ -125,7 +118,7 @@ battery_perc(const char *battery) fp = fopen(batteryfullfile, "r"); if (fp == NULL) { - fprintf(foutput, "Error opening battery file: %s\n", + fprintf(stderr, "Error opening battery file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -146,7 +139,7 @@ cpu_perc(void) FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(foutput, "Error opening stat file: %s\n", + fprintf(stderr, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -158,7 +151,7 @@ cpu_perc(void) fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(foutput, "Error opening stat file: %s\n", + fprintf(stderr, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -188,7 +181,7 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -202,7 +195,7 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -217,7 +210,7 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -231,7 +224,7 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -246,7 +239,7 @@ entropy(void) FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(foutput, "Could not open entropy file: %s\n", + fprintf(stderr, "Could not open entropy file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -269,7 +262,7 @@ hostname(void) FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(foutput, "Could not open hostname file: %s\n", + fprintf(stderr, "Could not open hostname file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -290,7 +283,7 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(foutput, "Error getting IP address: %s\n", + fprintf(stderr, "Error getting IP address: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -305,7 +298,7 @@ ip(const char *interface) if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - fprintf(foutput, "Error getting IP address.\n"); + fprintf(stderr, "Error getting IP address.\n"); return smprintf(UNKNOWN_STR); } return smprintf("%s", host); @@ -324,7 +317,7 @@ load_avg(void) double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(foutput, "Error getting load avg.\n"); + fprintf(stderr, "Error getting load avg.\n"); return smprintf(UNKNOWN_STR); } @@ -338,7 +331,7 @@ ram_free(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -356,7 +349,7 @@ ram_perc(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -378,7 +371,7 @@ ram_total(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -395,7 +388,7 @@ ram_used(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -418,7 +411,7 @@ run_command(const char* command) char buffer[64]; if (fp == NULL) { - fprintf(foutput, "Could not get command output for: %s: %s\n", + fprintf(stderr, "Could not get command output for: %s: %s\n", command, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -443,7 +436,7 @@ temp(const char *file) FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(foutput, "Could not open temperature file: %s\n", + fprintf(stderr, "Could not open temperature file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -476,7 +469,7 @@ username(void) if (pw == NULL) return smprintf("%s", pw->pw_name); - fprintf(foutput, "Could not get username: %s\n", + fprintf(stderr, "Could not get username: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -509,7 +502,7 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { - fprintf(foutput, "Could not get alsa volume.\n"); + fprintf(stderr, "Could not get alsa volume.\n"); return smprintf(UNKNOWN_STR); } snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); @@ -552,7 +545,7 @@ wifi_perc(const char *wificard) fp = fopen(path, "r"); if(fp == NULL) { - fprintf(foutput, "Error opening wifi operstate file: %s\n", + fprintf(stderr, "Error opening wifi operstate file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -564,7 +557,7 @@ wifi_perc(const char *wificard) fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening wireless file: %s\n", + fprintf(stderr, "Error opening wireless file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -596,13 +589,13 @@ wifi_essid(const char *wificard) wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(foutput, "Cannot open socket for interface: %s: %s\n", + fprintf(stderr, "Cannot open socket for interface: %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(foutput, "Get ESSID ioctl failed for interface %s: %s\n", + fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -613,84 +606,37 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } -void -usage(void) -{ - fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); - exit(0); -} - int -main(int argc, char *argv[]) +main(void) { - unsigned short int dflag, lflag; size_t i; - char status_string[4096], logpath[4096]; + char status_string[4096]; char *res, *element; struct arg argument; - foutput = stderr; - dpy = XOpenDisplay(0x0); - - ARGBEGIN { - case 'd': - dflag = 1; - break; - case 'l': - strlcpy(logpath, EARGF(usage()), sizeof(logpath)-1); - logpath[strlen(logpath)+1] = '\0'; - foutput = fopen(logpath, "a"); - if (foutput == NULL) { - fprintf(stderr, "failed to open log file at %s: %s\n", - logpath, strerror(errno)); - return (1); - } - lflag = 1; - break; - default: - usage(); - } ARGEND - - if (dflag && !lflag) { - ccat(2, getenv("HOME"), "/.slstatus_log"); - foutput = fopen(concat, "a"); - if (foutput == NULL) { - fprintf(stderr, "failed to open log file at %s: %s\n", - logpath, strerror(errno)); - return (1); + stderr = stderr; + dpy = XOpenDisplay(NULL); + + memset(status_string, 0, sizeof(status_string)); + for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { + argument = args[i]; + if (argument.args == NULL) + res = argument.func(); + else + res = argument.func(argument.args); + element = smprintf(argument.format, res); + if (element == NULL) { + element = smprintf(UNKNOWN_STR); + fprintf(stderr, "Failed to format output.\n"); } + strlcat(status_string, element, sizeof(status_string)); + free(res); + free(element); } - if (dflag) - daemon(0, 0); - - for (;;) { - memset(status_string, 0, sizeof(status_string)); - for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { - argument = args[i]; - if (argument.args == NULL) - res = argument.func(); - else - res = argument.func(argument.args); - element = smprintf(argument.format, res); - if (element == NULL) { - element = smprintf(UNKNOWN_STR); - fprintf(stderr, "Failed to format output.\n"); - } - strlcat(status_string, element, sizeof(status_string)); - free(res); - free(element); - } - - XStoreName(dpy, DefaultRootWindow(dpy), status_string); - XSync(dpy, False); - } - - /* NOT REACHED */ - /* - * TODO: find out a way to exit successfully - * to prevent memory leaks - */ + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); XCloseDisplay(dpy); + return 0; } -- cgit v1.2.3 From 52d19f955e67712eab50f582046013246b6bc75d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:18:55 +0300 Subject: imported a new vol_perc() function, this should fix #12 (UNTESTED) --- slstatus.c | 57 ++++++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index dae5e8a..92cff2e 100644 --- a/slstatus.c +++ b/slstatus.c @@ -481,50 +481,37 @@ uid(void) } -static char * -vol_perc(const char *soundcard) -{ - /* - * TODO: FIXME: - * https://github.com/drkh5h/slstatus/issues/12 - */ - int mute = 0; - long vol = 0, max = 0, min = 0; +static char * +vol_perc(const char *snd_card) +{ /* thanks to botika for this function */ + long int vol, max, min; snd_mixer_t *handle; - snd_mixer_elem_t *pcm_mixer, *mas_mixer; - snd_mixer_selem_id_t *vol_info, *mute_info; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *s_elem; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, soundcard); + snd_mixer_attach(handle, snd_card); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); + snd_mixer_selem_id_malloc(&s_elem); + snd_mixer_selem_id_set_name(s_elem, "Master"); + elem = snd_mixer_find_selem(handle, s_elem); - snd_mixer_selem_id_malloc(&vol_info); - snd_mixer_selem_id_malloc(&mute_info); - if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + if (elem == NULL) { + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + perror("alsa error: "); return smprintf(UNKNOWN_STR); } - snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); - snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL); - pcm_mixer = snd_mixer_find_selem(handle, vol_info); - mas_mixer = snd_mixer_find_selem(handle, mute_info); - - snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); - snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); - snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); - - if (vol_info) - snd_mixer_selem_id_free(vol_info); - if (mute_info) - snd_mixer_selem_id_free(mute_info); - if (handle) - snd_mixer_close(handle); - if (!mute) - return smprintf("mute"); - else - return smprintf("%d%%", (vol * 100) / max); + snd_mixer_handle_events(handle); + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); + + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + + return smprintf("%d", (vol * 100) / max); } static char * -- cgit v1.2.3 From a4beda8eb9bdf34d055ba52c39b15b9580b27e6a Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:21:03 +0300 Subject: bringed back the loop --- slstatus.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 92cff2e..441c517 100644 --- a/slstatus.c +++ b/slstatus.c @@ -604,21 +604,23 @@ main(void) stderr = stderr; dpy = XOpenDisplay(NULL); - memset(status_string, 0, sizeof(status_string)); - for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { - argument = args[i]; - if (argument.args == NULL) - res = argument.func(); - else - res = argument.func(argument.args); - element = smprintf(argument.format, res); - if (element == NULL) { - element = smprintf(UNKNOWN_STR); - fprintf(stderr, "Failed to format output.\n"); + for (;;) { + memset(status_string, 0, sizeof(status_string)); + for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { + argument = args[i]; + if (argument.args == NULL) + res = argument.func(); + else + res = argument.func(argument.args); + element = smprintf(argument.format, res); + if (element == NULL) { + element = smprintf(UNKNOWN_STR); + fprintf(stderr, "Failed to format output.\n"); + } + strlcat(status_string, element, sizeof(status_string)); + free(res); + free(element); } - strlcat(status_string, element, sizeof(status_string)); - free(res); - free(element); } XStoreName(dpy, DefaultRootWindow(dpy), status_string); -- cgit v1.2.3 From 720328cef9a9d81d2bd876bdf89103782b9cab91 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 02:17:30 +0300 Subject: what kind of weed is that? --- slstatus.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 441c517..da4b237 100644 --- a/slstatus.c +++ b/slstatus.c @@ -601,7 +601,6 @@ main(void) char *res, *element; struct arg argument; - stderr = stderr; dpy = XOpenDisplay(NULL); for (;;) { @@ -621,10 +620,9 @@ main(void) free(res); free(element); } + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); } - - XStoreName(dpy, DefaultRootWindow(dpy), status_string); - XSync(dpy, False); XCloseDisplay(dpy); return 0; -- cgit v1.2.3 From 113979e5b8398a2c26736749480fe32b5c49a66b Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 02:28:18 +0300 Subject: fixed some mistakes --- slstatus.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index da4b237..c756de6 100644 --- a/slstatus.c +++ b/slstatus.c @@ -490,17 +490,17 @@ vol_perc(const char *snd_card) snd_mixer_selem_id_t *s_elem; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, snd_card); + snd_mixer_attach(handle, "default"); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_malloc(&s_elem); - snd_mixer_selem_id_set_name(s_elem, "Master"); + snd_mixer_selem_id_set_name(s_elem, snd_card); elem = snd_mixer_find_selem(handle, s_elem); if (elem == NULL) { snd_mixer_selem_id_free(s_elem); snd_mixer_close(handle); - perror("alsa error: "); + perror("alsa error"); return smprintf(UNKNOWN_STR); } -- cgit v1.2.3 From 2afea979877ae12226ab397355f3bf8c8e124e91 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 8 Sep 2016 04:31:49 +0300 Subject: used ccat() from concat.h for string concatenation --- slstatus.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index c756de6..53de9cb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -26,6 +26,9 @@ #include "strlcat.h" #include "strlcpy.h" +#include "concat.h" + +char concat[]; struct arg { char *(*func)(); @@ -91,24 +94,14 @@ static char * battery_perc(const char *battery) { int now, full, perc; - char batterynowfile[64]; - char batteryfullfile[64]; FILE *fp; - strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); - strlcat(batterynowfile, battery, sizeof(batterynowfile)); - strlcat(batterynowfile, "/", sizeof(batterynowfile)); - strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile)); - - strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile)); - strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); - strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); - strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); + ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW); - fp = fopen(batterynowfile, "r"); - if (fp == NULL ) { + fp = fopen(concat, "r"); + if (fp == NULL) { fprintf(stderr, "Error opening battery file: %s: %s\n", - batterynowfile, + concat, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -116,9 +109,12 @@ battery_perc(const char *battery) fscanf(fp, "%i", &now); fclose(fp); - fp = fopen(batteryfullfile, "r"); + ccat(4, BATTERY_PATH, battery, "/", BATTERY_FULL); + + fp = fopen(concat, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s\n", + fprintf(stderr, "Error opening battery file: %s: %s\n", + concat, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -520,16 +516,12 @@ wifi_perc(const char *wificard) int strength; char buf[255]; char *datastart; - char path[64]; char status[5]; - char needle[strlen(wificard)+2]; FILE *fp; - strlcpy(path, "/sys/class/net/", sizeof(path)); - strlcat(path, wificard, sizeof(path)); - strlcat(path, "/operstate", sizeof(path)); + ccat(3, "/sys/class/net", wificard, "/operstate"); - fp = fopen(path, "r"); + fp = fopen(concat, "r"); if(fp == NULL) { fprintf(stderr, "Error opening wifi operstate file: %s\n", @@ -549,13 +541,12 @@ wifi_perc(const char *wificard) return smprintf(UNKNOWN_STR); } - strlcpy(needle, wificard, sizeof(needle)); - strlcat(needle, ":", sizeof(needle)); + ccat(2, wificard, ":"); fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); - datastart = strstr(buf, needle); + datastart = strstr(buf, concat); if (datastart != NULL) { datastart = strstr(buf, ":"); sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength); -- cgit v1.2.3 From 9b2dc253485ef3a0ac34a5e781bc2c856bec463d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 8 Sep 2016 04:45:00 +0300 Subject: use warn[x]() instead of long fprintf()s --- slstatus.c | 75 ++++++++++++++++++++++---------------------------------------- 1 file changed, 26 insertions(+), 49 deletions(-) (limited to 'slstatus.c') diff --git a/slstatus.c b/slstatus.c index 53de9cb..55e638f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include +#include #include #include #include @@ -100,9 +101,7 @@ battery_perc(const char *battery) fp = fopen(concat, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s: %s\n", - concat, - strerror(errno)); + warn("Error opening battery file: %s", concat); return smprintf(UNKNOWN_STR); } @@ -113,9 +112,7 @@ battery_perc(const char *battery) fp = fopen(concat, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s: %s\n", - concat, - strerror(errno)); + warn("Error opening battery file: %s", concat); return smprintf(UNKNOWN_STR); } @@ -135,8 +132,7 @@ cpu_perc(void) FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", - strerror(errno)); + warn("Error opening stat file"); return smprintf(UNKNOWN_STR); } @@ -147,8 +143,7 @@ cpu_perc(void) fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", - strerror(errno)); + warn("Error opening stat file"); return smprintf(UNKNOWN_STR); } @@ -177,8 +172,7 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", - strerror(errno)); + warn("Could not get filesystem info"); return smprintf(UNKNOWN_STR); } 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; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", - strerror(errno)); + warn("Could not get filesystem info"); return smprintf(UNKNOWN_STR); } @@ -206,8 +199,7 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", - strerror(errno)); + warn("Could not get filesystem info"); return smprintf(UNKNOWN_STR); } @@ -220,8 +212,7 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", - strerror(errno)); + warn("Could not get filesystem info"); return smprintf(UNKNOWN_STR); } @@ -235,8 +226,7 @@ entropy(void) FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open entropy file: %s\n", - strerror(errno)); + warn("Could not open entropy file"); return smprintf(UNKNOWN_STR); } @@ -258,8 +248,7 @@ hostname(void) FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open hostname file: %s\n", - strerror(errno)); + warn("Could not open hostname file"); return smprintf(UNKNOWN_STR); } @@ -279,8 +268,7 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address: %s\n", - strerror(errno)); + warn("Error getting IP address"); 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 (s != 0) { - fprintf(stderr, "Error getting IP address.\n"); + warnx("Error getting IP address."); return smprintf(UNKNOWN_STR); } return smprintf("%s", host); @@ -313,7 +301,7 @@ load_avg(void) double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(stderr, "Error getting load avg.\n"); + warnx("Error getting load avg."); return smprintf(UNKNOWN_STR); } @@ -327,8 +315,7 @@ ram_free(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", - strerror(errno)); + warn("Error opening meminfo file"); return smprintf(UNKNOWN_STR); } @@ -345,8 +332,7 @@ ram_perc(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", - strerror(errno)); + warn("Error opening meminfo file"); return smprintf(UNKNOWN_STR); } @@ -367,8 +353,7 @@ ram_total(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", - strerror(errno)); + warn("Error opening meminfo file"); return smprintf(UNKNOWN_STR); } @@ -384,8 +369,7 @@ ram_used(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", - strerror(errno)); + warn("Error opening meminfo file"); return smprintf(UNKNOWN_STR); } @@ -407,8 +391,7 @@ run_command(const char* command) char buffer[64]; if (fp == NULL) { - fprintf(stderr, "Could not get command output for: %s: %s\n", - command, strerror(errno)); + warn("Could not get command output for: %s", command); return smprintf(UNKNOWN_STR); } @@ -432,8 +415,7 @@ temp(const char *file) FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(stderr, "Could not open temperature file: %s\n", - strerror(errno)); + warn("Could not open temperature file"); return smprintf(UNKNOWN_STR); } @@ -465,8 +447,7 @@ username(void) if (pw == NULL) return smprintf("%s", pw->pw_name); - fprintf(stderr, "Could not get username: %s\n", - strerror(errno)); + warn("Could not get username"); return smprintf(UNKNOWN_STR); } @@ -524,8 +505,7 @@ wifi_perc(const char *wificard) fp = fopen(concat, "r"); if(fp == NULL) { - fprintf(stderr, "Error opening wifi operstate file: %s\n", - strerror(errno)); + warn("Error opening wifi operstate file"); return smprintf(UNKNOWN_STR); } @@ -536,8 +516,7 @@ wifi_perc(const char *wificard) fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening wireless file: %s\n", - strerror(errno)); + warn("Error opening wireless file"); return smprintf(UNKNOWN_STR); } @@ -567,14 +546,12 @@ wifi_essid(const char *wificard) wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(stderr, "Cannot open socket for interface: %s: %s\n", - wificard, strerror(errno)); + warn("Cannot open socket for interface: %s", wificard); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", - wificard, strerror(errno)); + warn("Get ESSID ioctl failed for interface %s", wificard); return smprintf(UNKNOWN_STR); } @@ -605,7 +582,7 @@ main(void) element = smprintf(argument.format, res); if (element == NULL) { element = smprintf(UNKNOWN_STR); - fprintf(stderr, "Failed to format output.\n"); + warnx("Failed to format output."); } strlcat(status_string, element, sizeof(status_string)); free(res); -- cgit v1.2.3