From e1ae7d3be6faa710b3a711ed317cfe45d90f28bc Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:00:23 +0300 Subject: [PATCH 01/47] added bounds checking via secure strl*() routines --- slstatus.c | 34 +++++++++++++++++++-------------- strlcat.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ strlcpy.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 strlcat.h create mode 100644 strlcpy.h 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); } diff --git a/strlcat.h b/strlcat.h new file mode 100644 index 0000000..2596420 --- /dev/null +++ b/strlcat.h @@ -0,0 +1,55 @@ +/* $OpenBSD: strlcat.c,v 1.16 2015/08/31 02:53:57 guenther Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} diff --git a/strlcpy.h b/strlcpy.h new file mode 100644 index 0000000..6301674 --- /dev/null +++ b/strlcpy.h @@ -0,0 +1,50 @@ +/* $OpenBSD: strlcpy.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} From 1853884520ddca4237dd0534256da0c8bf628251 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:28:36 +0300 Subject: [PATCH 02/47] braces are unneeded for one-liner if()/while() --- slstatus.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) 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 */ From 2e1eb518afea7c93e3ba750804f1f4c4d0a51af4 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:10:14 +0300 Subject: [PATCH 03/47] the code describes itself, there is no need to write stories in /* */ --- slstatus.c | 221 +++++++---------------------------------------------- 1 file changed, 27 insertions(+), 194 deletions(-) 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); +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 *); -/* global variables */ 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; } From 73ec65a84d422fe4bd15a8d3572875f2413926e6 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:10:37 +0300 Subject: [PATCH 04/47] -Wextra --- config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mk b/config.mk index cdb9656..f01bcdb 100644 --- a/config.mk +++ b/config.mk @@ -16,7 +16,7 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound # flags CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE -CFLAGS = -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} +CFLAGS = -std=c99 -pedantic -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} #CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} LDFLAGS = ${LIBS} #LDFLAGS = -s ${LIBS} From 46b6876d7ec6e17bb51ee647f77bd238c864ca25 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:28:42 +0300 Subject: [PATCH 05/47] set local function as static --- slstatus.c | 104 ++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) 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]; From b4f55e7170576a2dd800018c8690832bbc7f9bf0 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 12:00:51 +0300 Subject: [PATCH 06/47] (void)ed the prototypes --- slstatus.c | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) 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); From 0da2af8c621aff973cd711f33a5348ea82b6b09d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 12:04:01 +0300 Subject: [PATCH 07/47] Added myself to CONTRIBUTORS.md by drkh5h's request --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4e01b97..63a71c9 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,3 +6,4 @@ Thanks you very much for your great help! - [Vlaix](https://github.com/Vlaix) - [pfannkuckengesicht](https://github.com/pfannkuchengesicht) - [sahne](https://github.com/sahne) +- [Ali H. Fardan](http://raiz.duckdns.org) From f65fb9bca18bb445de2c337ed9e4d84de5b631f7 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:19:45 +0300 Subject: [PATCH 08/47] fixed the code, works now --- .config.h.swp | Bin 0 -> 12288 bytes slstatus.c | 32 +++++++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 .config.h.swp diff --git a/.config.h.swp b/.config.h.swp new file mode 100644 index 0000000000000000000000000000000000000000..dfc71bbf434b571d5f007bb60cb40d435163615d GIT binary patch literal 12288 zcmeHNJ!~9B6rLc0lRzLMee^hV(k9CJVlGl7TNVY1L?qjd6%bhFG~S)PTV?+=GkfL~ z0tJzPf|{C&ijD?K5J(`XprWOtMo=O6-t4Y@mxSb;R6(=Sx4Sd@-kbNndHbC#@7|@? zu5Hql<;w!c86h@4{_^#$6Mu@Obs;LFG*3V1SQ!kiJjjNhzsmvpT9rlLq{d2HnSNO0 zaWYySFXHcQJOiGAM`7T!7`(J{i6gw&d4bM8fBjK{d0)?fXTUSy8So5v20R0v0ndPE z;4x#smdC_<$oYvT@0I3y?2c1IK|Ufxk`* z@fGj^@CI-W`0W`XJ_Ax<9XJF0@w5;>0-pjoum&svznl`{6W~pt2b=;OIBIPeo{;d;L3n*8Az@Cl@6VG#~7Fl4823nzteB+CPjK^MjZ^P zd->2VQn9R3+gfXx&01qP8+GgY#zwPzSXQ%!7`m`c@&|_f3lvv*Xpqx~dKhRT>EqJZzo-JhqOHqxpy zp-}3GlH4jC%TQ4+GquZ$G!dA%CV)vsxlCb8Jc(Qu3x-H7wG_zINFJgVl6gc~Wfe-` z4<`RBuqmoM;w*O&iNCYt6e&2kD`E;Ot(hC1n@fu6b#5hh4xHTe-X=^`h0`MOtjbCX z_^dQ?*NT&m)JQ93=AzG<#7U|+8C~VWu*fp5KfnO#8(U;##wty*tM18MlxEI3qwi2A zd!pqkBS&uSd+kVDJh!SXEVk}wKK0fgX_*)V9ATBTkkL?1MilTJ$qCpTskzeIA``8p$JtvjnSZb>B8+ox0y?fg_t?cA|*;!*Z zo+4Y|JuLPqi778=BGXDiB6HePq?Of)n&91oJhqXmoMjPn*i5QQ=40p@60T6<6&D$) zG|$*fX?_O5FbWbL8464$(^Nss>-FGves+yo=w0?rt#Os6$exdN>Zn}l(R9d@!$@&H zDCq9W8(&o4xX$=u@_s(0wBy6|`eusH+f!*wzby8Z9-68w(`mn^vE7atl4c4(6?v}O z_3o=`mk% Date: Sun, 21 Aug 2016 15:20:07 +0300 Subject: [PATCH 09/47] rm .config.h.swp --- .config.h.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .config.h.swp diff --git a/.config.h.swp b/.config.h.swp deleted file mode 100644 index dfc71bbf434b571d5f007bb60cb40d435163615d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeHNJ!~9B6rLc0lRzLMee^hV(k9CJVlGl7TNVY1L?qjd6%bhFG~S)PTV?+=GkfL~ z0tJzPf|{C&ijD?K5J(`XprWOtMo=O6-t4Y@mxSb;R6(=Sx4Sd@-kbNndHbC#@7|@? zu5Hql<;w!c86h@4{_^#$6Mu@Obs;LFG*3V1SQ!kiJjjNhzsmvpT9rlLq{d2HnSNO0 zaWYySFXHcQJOiGAM`7T!7`(J{i6gw&d4bM8fBjK{d0)?fXTUSy8So5v20R0v0ndPE z;4x#smdC_<$oYvT@0I3y?2c1IK|Ufxk`* z@fGj^@CI-W`0W`XJ_Ax<9XJF0@w5;>0-pjoum&svznl`{6W~pt2b=;OIBIPeo{;d;L3n*8Az@Cl@6VG#~7Fl4823nzteB+CPjK^MjZ^P zd->2VQn9R3+gfXx&01qP8+GgY#zwPzSXQ%!7`m`c@&|_f3lvv*Xpqx~dKhRT>EqJZzo-JhqOHqxpy zp-}3GlH4jC%TQ4+GquZ$G!dA%CV)vsxlCb8Jc(Qu3x-H7wG_zINFJgVl6gc~Wfe-` z4<`RBuqmoM;w*O&iNCYt6e&2kD`E;Ot(hC1n@fu6b#5hh4xHTe-X=^`h0`MOtjbCX z_^dQ?*NT&m)JQ93=AzG<#7U|+8C~VWu*fp5KfnO#8(U;##wty*tM18MlxEI3qwi2A zd!pqkBS&uSd+kVDJh!SXEVk}wKK0fgX_*)V9ATBTkkL?1MilTJ$qCpTskzeIA``8p$JtvjnSZb>B8+ox0y?fg_t?cA|*;!*Z zo+4Y|JuLPqi778=BGXDiB6HePq?Of)n&91oJhqXmoMjPn*i5QQ=40p@60T6<6&D$) zG|$*fX?_O5FbWbL8464$(^Nss>-FGves+yo=w0?rt#Os6$exdN>Zn}l(R9d@!$@&H zDCq9W8(&o4xX$=u@_s(0wBy6|`eusH+f!*wzby8Z9-68w(`mn^vE7atl4c4(6?v}O z_3o=`mk% Date: Sun, 21 Aug 2016 15:21:20 +0300 Subject: [PATCH 10/47] corrected the time format --- config.def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index 5fb52b3..75858dd 100644 --- a/config.def.h +++ b/config.def.h @@ -48,5 +48,5 @@ static const struct arg args[] = { { ram_perc, "ram %3s | ", NULL }, { vol_perc, "vol %4s | ", "default" }, { disk_perc, "ssd %3s | ", "/" }, - { datetime, "%s", "%y-%m-%d %H:%M:%S" }, + { datetime, "%s", "%F %T" }, }; From 6f31ca6ce3ea722c7d1ecfe2099108133ce4215c Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:43:57 +0300 Subject: [PATCH 11/47] fixed compiler warnings in a better way --- config.mk | 2 +- slstatus.c | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/config.mk b/config.mk index f01bcdb..8e404d6 100644 --- a/config.mk +++ b/config.mk @@ -16,7 +16,7 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound # flags CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE -CFLAGS = -std=c99 -pedantic -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} +CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} #CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} LDFLAGS = ${LIBS} #LDFLAGS = -s ${LIBS} 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); From ff013b5fd4e059ae7c9e07ff2ac0e669b7dac136 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 16:00:34 +0300 Subject: [PATCH 12/47] status_string can hold 4096 bytes now --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From f9f72a06f11de9ecd1b14811eba63c6e8c7a1165 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 13:27:42 +0300 Subject: [PATCH 13/47] 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(-) 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) From b79837b075305f8430859d014f892333a1b8c782 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 14:11:55 +0300 Subject: [PATCH 14/47] README update --- README.md | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e19e16d..744cea5 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,29 @@ slstatus ======== -**slstatus** is a suckless and lightweight status monitor for window managers which use WM_NAME as statusbar (e.g. DWM). It is written in pure C without any system() calls and only reads from files most of the time. It is meant as a better alternative to Bash scripts (inefficient) and Conky (bloated for this use). +**slstatus** is a suckless and lightweight status monitor for window managers which use WM_NAME as statusbar (e.g. DWM). It is written in pure C without any system calls and only reads from files most of the time. It is meant as a better alternative to Bash scripts (inefficient) and Conky (bloated for this use). -If you write a bash script that shows system information in WM_NAME, it executes a huge amount of external command (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs / procfs. +If you write a bash script that shows system information in WM_NAME, it executes a huge amount of external command (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs/procfs. -Looking at the LOC (lines of code) in the [Conky project](https://github.com/brndnmtthws/conky) is very interesting: *28.346 lines C++, 219 lines Python and 110 lines Lua*. slstatus currently has about **600 lines of clean, well commented C code** and even includes additional possibilities as it can be customized and extended very easily. Configuring it by editing config.h (a C header file) is very secure and fast as no config files are parsed at runtime. +Looking at the LOC (lines of code) of the [Conky project](https://github.com/brndnmtthws/conky), very interesting: *28.346 lines C++, 219 lines Python and 110 lines Lua*. slstatus currently has about **800 lines of clean documented C code** and even includes additional possibilities as it can be customized and extended very easily. Configure it by customizing the config.h (C header file) which is secure and fast as no config files are parsed at runtime. The following information is included: -- battery percentage -- cpu usage (in percent) -- custom shell commands -- date and time -- disk numbers (free storage, percentage, total storage and used storage) -- available entropy -- username/gid/uid of current user -- hostname -- ip addresses -- load average -- ram numbers (free ram, percentage, total ram and used ram) -- temperature -- uptime -- volume percentage + mute status (alsa) -- wifi signal percentage and essid +- Battery percentage +- CPU usage (in percent) +- Custom shell commands +- Date and time +- Disk[s] status (free storage, percentage, total storage and used storage) +- Available entropy +- username/gid/uid +- Hostname +- IP addresses +- Load average +- Memory status (free memory, percentage, total memory and used memory) +- Temperature +- Uptime +- Volume percentage + mute status (alsa) +- WiFi signal percentage and essid Multiple entries per function are supported and everything can be reordered and customized via the C header file config.h (similar to DWM). @@ -31,11 +31,14 @@ Multiple entries per function are supported and everything can be reordered and ### Installation -Before you continue, please be sure that a C compiler, GNU make and `alsa-lib` (for volume percentage) are installed. Then copy config.def.h to config.h and edit it to your needs. Recompile and install it after every change via `sudo make install`! +Before you continue, please be sure that a C compiler (preferrably gcc), GNU make and `alsa-lib` (for volume percentage) are installed. Then copy config.def.h to config.h and customize it to fit your needs. Recompile and install it after modifications: + + $ make clean all + # make install ### Starting -Put the following code in your ~/.xinitrc (or similar): +Write the following code in your ~/.xinitrc (or any other initialization script): ``` while true; do From 5b6097471ff8024029268834e482f7102d26c049 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 14:13:23 +0300 Subject: [PATCH 15/47] another update to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 744cea5..bf435fa 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ slstatus ======== -**slstatus** is a suckless and lightweight status monitor for window managers which use WM_NAME as statusbar (e.g. DWM). It is written in pure C without any system calls and only reads from files most of the time. It is meant as a better alternative to Bash scripts (inefficient) and Conky (bloated for this use). +**slstatus** is a suckless and lightweight status monitor for window managers that use WM_NAME as statusbar (e.g. DWM). It is written in pure C without any system calls and only reads from files most of the time. It is meant to be a better alternative to Bash scripts (inefficient) and Conky (bloated for this use). If you write a bash script that shows system information in WM_NAME, it executes a huge amount of external command (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs/procfs. From ee5337babe5fb756d5877fc820a73f5a34a0d799 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 14:16:45 +0300 Subject: [PATCH 16/47] typofix && another update --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bf435fa..c8f0896 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ slstatus **slstatus** is a suckless and lightweight status monitor for window managers that use WM_NAME as statusbar (e.g. DWM). It is written in pure C without any system calls and only reads from files most of the time. It is meant to be a better alternative to Bash scripts (inefficient) and Conky (bloated for this use). -If you write a bash script that shows system information in WM_NAME, it executes a huge amount of external command (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs/procfs. +If you write a bash script that shows system information in WM_NAME, it executes a huge amount of external commands (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs/procfs. Looking at the LOC (lines of code) of the [Conky project](https://github.com/brndnmtthws/conky), very interesting: *28.346 lines C++, 219 lines Python and 110 lines Lua*. slstatus currently has about **800 lines of clean documented C code** and even includes additional possibilities as it can be customized and extended very easily. Configure it by customizing the config.h (C header file) which is secure and fast as no config files are parsed at runtime. @@ -40,11 +40,9 @@ Before you continue, please be sure that a C compiler (preferrably gcc), GNU mak Write the following code in your ~/.xinitrc (or any other initialization script): -``` -while true; do - slstatus -done & -``` + while true; do + slstatus + done & The loop is needed that the program runs after suspend to ram. From 447283fda1505124f68f0cf74833f307cb630fa3 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 25 Aug 2016 23:26:17 +0300 Subject: [PATCH 17/47] removing typecasts for pcm_mixer cause more issues than before --- slstatus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) From 674602c6af0357ed963caabbd4ae102d1ac1e77c Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 15:33:31 +0300 Subject: [PATCH 18/47] grammar correction --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c8f0896..9d6f813 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Before you continue, please be sure that a C compiler (preferrably gcc), GNU mak ### Starting -Write the following code in your ~/.xinitrc (or any other initialization script): +Write the following code to your ~/.xinitrc (or any other initialization script): while true; do slstatus From 47ddf9382e6a02bdfce9c78306b694b4a44203cb Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:20:50 +0300 Subject: [PATCH 19/47] used constant string literals && remote initialization to in battery_perc() && trying to fix possible buffer overflow --- config.def.h | 27 +++++++-------- slstatus.c | 93 +++++++++++++++++++++++++++------------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/config.def.h b/config.def.h index 75858dd..7a1ee3c 100644 --- a/config.def.h +++ b/config.def.h @@ -1,18 +1,18 @@ /* See LICENSE file for copyright and license details. */ /* alsa sound */ -static const char channel[] = "Master"; +#define ALSA_CHANNEL "Master" /* battery */ -static const char batterypath[] = "/sys/class/power_supply/"; -static const char batterynow[] = "energy_now"; -static const char batteryfull[] = "energy_full_design"; +#define BATTERY_PATH "/sys/class/power_supply/" +#define BATTERY_NOW "energy_now" +#define BATTERY_FULL "energy_full_design" /* bar update interval in seconds (smallest value = 1) */ -static unsigned int update_interval = 1; +#define UPDATE_INTERVAL 1; /* text to show if no value can be retrieved */ -static const char unknowntext[] = "n/a"; +#define UNKNOWN_STR "n/a" /* statusbar - battery_perc (battery percentage) [argument: battery name] @@ -40,13 +40,10 @@ static const char unknowntext[] = "n/a"; - wifi_perc (wifi signal in percent) [argument: wifi card interface name] - wifi_essid (wifi essid) [argument: wifi card interface name] */ static const struct arg args[] = { - /* function format argument */ - { wifi_perc, "wifi %4s | ", "wlp3s0" }, - { battery_perc, "bat %4s | ", "BAT0" }, - { cpu_perc, "cpu %4s ", NULL }, - { temp, "%3s | ", "/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input" }, - { ram_perc, "ram %3s | ", NULL }, - { vol_perc, "vol %4s | ", "default" }, - { disk_perc, "ssd %3s | ", "/" }, - { datetime, "%s", "%F %T" }, + /* function format argument */ + { cpu_perc, "[ CPU %4s ]", NULL }, + { ram_perc, "[ Mem %3s ]", NULL }, + { vol_perc, "[ Volume %4s ]", "default" }, + { disk_perc, "[ HDD %3s ]", "/" }, + { datetime, "[ %s ]", "%F %T" }, }; 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); From 24c4134df6e0f7dc86e5f3c57342d2b60b1e5dab Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:24:19 +0300 Subject: [PATCH 20/47] removed a misplaced ';' --- config.def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index 7a1ee3c..6c7d2bc 100644 --- a/config.def.h +++ b/config.def.h @@ -9,7 +9,7 @@ #define BATTERY_FULL "energy_full_design" /* bar update interval in seconds (smallest value = 1) */ -#define UPDATE_INTERVAL 1; +#define UPDATE_INTERVAL 1 /* text to show if no value can be retrieved */ #define UNKNOWN_STR "n/a" From 25eb9ff35e76312b09ff5613c9a3cc1275938680 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:30:12 +0300 Subject: [PATCH 21/47] FIXME: buffer overflow warning --- slstatus.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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; } From 1d257999ed6049dce4d1305c4dc3304ea9910ca7 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 16:39:04 +0300 Subject: [PATCH 22/47] removed heap dependency in datetime() and simplified the function --- slstatus.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) 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 * From 94a62b864b56d8bad1fb68925dcee7c71015bc54 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 18:19:53 +0300 Subject: [PATCH 23/47] worked around the buffer overrun in smprintf() --- slstatus.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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); From 5829cee24ea0e40cbe402728d1ca2f0bfb630182 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 28 Aug 2016 19:27:01 +0300 Subject: [PATCH 24/47] used a different implementation of smprintf() imported from dwmstatus --- slstatus.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) 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'; - - if (asprintf(&ret, "%s", tmp) < 0) - return NULL; - + len = vsnprintf(NULL, 0, fmt, ap); va_end(ap); + + ret = malloc(++len); + if (ret == NULL) { + perror("malloc"); + exit(1); + } + + va_start(ap, fmt); + vsnprintf(ret, len, fmt, ap); + va_end(ap); + return ret; } From 0aefd57f9f469aba25ffbe9bdda04e894b9d739a Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 30 Aug 2016 21:50:40 +0300 Subject: [PATCH 25/47] 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(-) 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); } From b800eb21a31921066bb0e574227b6b42d9223162 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 02:17:42 +0300 Subject: [PATCH 26/47] removed vol function from the default config file --- config.def.h | 1 - 1 file changed, 1 deletion(-) diff --git a/config.def.h b/config.def.h index 6c7d2bc..011acc7 100644 --- a/config.def.h +++ b/config.def.h @@ -43,7 +43,6 @@ static const struct arg args[] = { /* function format argument */ { cpu_perc, "[ CPU %4s ]", NULL }, { ram_perc, "[ Mem %3s ]", NULL }, - { vol_perc, "[ Volume %4s ]", "default" }, { disk_perc, "[ HDD %3s ]", "/" }, { datetime, "[ %s ]", "%F %T" }, }; From cd3c084957483c4192edf2e8feb4c759668a1055 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 06:00:14 +0300 Subject: [PATCH 27/47] slow down boy! you opened the file too early! --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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, From 5e4dc85bb9bf821d87bcf8a13e0f0843fd68bfe1 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 06:10:53 +0300 Subject: [PATCH 28/47] forgot to give entropy() some candy too --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From aa70b6633016ed35f9fa3d91c26751e973ac5d63 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Wed, 31 Aug 2016 14:29:27 +0300 Subject: [PATCH 29/47] opened the file before the path concatenates, what an idiot --- slstatus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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)); From 9e92d41db54b5c1128505ed778aa0feb9dd6457f Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:02:17 +0300 Subject: [PATCH 30/47] removed unneeded headers --- slstatus.c | 2 -- 1 file changed, 2 deletions(-) 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 From 825141633bad2264c67c38ebcaab63f4c96631d3 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:15:40 +0300 Subject: [PATCH 31/47] removed unnecessary typecast and added more comments --- slstatus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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; From 2f8335abf0d6347100cf63d82e8151ad0ff97ab0 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:35:32 +0300 Subject: [PATCH 32/47] simplified uid() and fixed username() --- slstatus.c | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) 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()); } From d3a212da7e09f11753aafd76f16dccb1ade822e1 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:51:32 +0300 Subject: [PATCH 33/47] simplified gid() --- slstatus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 * From d1ae2e785d26d944fb4b228c2e025528cd8b780b Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 1 Sep 2016 21:54:00 +0300 Subject: [PATCH 34/47] NOTREACHED comment --- slstatus.c | 7 +++++++ 1 file changed, 7 insertions(+) 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; +*/ } From b650c438f0dc049d98e339e66eeffd7650774a5d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Fri, 2 Sep 2016 22:13:58 +0300 Subject: [PATCH 35/47] removed setstatus() && simplified main() --- slstatus.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) 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; -*/ } From de4f20ace35037a510aafcf49a3d78637d7248b6 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sat, 3 Sep 2016 21:43:05 +0300 Subject: [PATCH 36/47] 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(). --- config.def.h | 3 --- slstatus.c | 2 -- 2 files changed, 5 deletions(-) diff --git a/config.def.h b/config.def.h index 011acc7..ded771a 100644 --- a/config.def.h +++ b/config.def.h @@ -8,9 +8,6 @@ #define BATTERY_NOW "energy_now" #define BATTERY_FULL "energy_full_design" -/* bar update interval in seconds (smallest value = 1) */ -#define UPDATE_INTERVAL 1 - /* text to show if no value can be retrieved */ #define UNKNOWN_STR "n/a" 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 */ From d3d8b8ee03a773b7f2cd1aad16ee43d9784e5139 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 4 Sep 2016 00:10:49 +0300 Subject: [PATCH 37/47] added daemonization support --- arg.h | 51 ++++++++++++++++++++++++++ concat.h | 19 ++++++++++ slstatus.c | 104 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 146 insertions(+), 28 deletions(-) create mode 100644 arg.h create mode 100644 concat.h diff --git a/arg.h b/arg.h new file mode 100644 index 0000000..6c4a528 --- /dev/null +++ b/arg.h @@ -0,0 +1,51 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/concat.h b/concat.h new file mode 100644 index 0000000..9f138d7 --- /dev/null +++ b/concat.h @@ -0,0 +1,19 @@ +/* + * Thanks to lloyd for contribution + */ + +extern char concat[4096]; + +extern void +ccat(const unsigned short int count, ...) +{ + va_list ap; + unsigned short int i; + concat[0] = '\0'; + + va_start(ap, count); + for(i = 0; i < count; i++) + strlcat(concat, va_arg(ap, char *), sizeof(concat)); + va_end(ap); + return; +} 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); } -int -main(void) +void +usage(void) { + fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); + exit(0); +} + +int +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) { From 1654efe1d67d0bec010792e8c6eb64a0f8e88a58 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 00:56:22 +0300 Subject: [PATCH 38/47] config.mk cleanup --- arg.h | 51 --------------------------------------------------- concat.h | 19 ------------------- config.mk | 8 -------- 3 files changed, 78 deletions(-) delete mode 100644 arg.h delete mode 100644 concat.h diff --git a/arg.h b/arg.h deleted file mode 100644 index 6c4a528..0000000 --- a/arg.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copy me if you can. - * by 20h - */ - -#ifndef ARG_H__ -#define ARG_H__ - -extern char *argv0; - -/* use main(int argc, char *argv[]) */ -#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ - argv[0] && argv[0][1]\ - && argv[0][0] == '-';\ - argc--, argv++) {\ - char argc_;\ - char **argv_;\ - int brk_;\ - if (argv[0][1] == '-' && argv[0][2] == '\0') {\ - argv++;\ - argc--;\ - break;\ - }\ - for (brk_ = 0, argv[0]++, argv_ = argv;\ - argv[0][0] && !brk_;\ - argv[0]++) {\ - if (argv_ != argv)\ - break;\ - argc_ = argv[0][0];\ - switch (argc_) - -#define ARGEND }\ - } - -#define ARGC() argc_ - -#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) - -#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ - ((x), abort(), (char *)0) :\ - (brk_ = 1, (argv[0][1] != '\0')?\ - (&argv[0][1]) :\ - (argc--, argv++, argv[0]))) - -#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ - (char *)0 :\ - (brk_ = 1, (argv[0][1] != '\0')?\ - (&argv[0][1]) :\ - (argc--, argv++, argv[0]))) - -#endif diff --git a/concat.h b/concat.h deleted file mode 100644 index 9f138d7..0000000 --- a/concat.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Thanks to lloyd for contribution - */ - -extern char concat[4096]; - -extern void -ccat(const unsigned short int count, ...) -{ - va_list ap; - unsigned short int i; - concat[0] = '\0'; - - va_start(ap, count); - for(i = 0; i < count; i++) - strlcat(concat, va_arg(ap, char *), sizeof(concat)); - va_end(ap); - return; -} diff --git a/config.mk b/config.mk index 8e404d6..704558a 100644 --- a/config.mk +++ b/config.mk @@ -3,9 +3,7 @@ VERSION = 1.0 # Customize below to fit your system -# paths PREFIX = /usr/local -MANPREFIX = ${PREFIX}/share/man X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib @@ -17,13 +15,7 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound # flags CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} -#CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} LDFLAGS = ${LIBS} -#LDFLAGS = -s ${LIBS} - -# Solaris -#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" -#LDFLAGS = ${LIBS} # compiler and linker CC = cc From 9fa858daeafb5398cdf19af9483b2854fe6e9772 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:13:48 +0300 Subject: [PATCH 39/47] added a tool for resetting the status bar && worked around some issues && removed the makefile (we need a better one) --- Makefile | 53 ------------------ config.mk | 22 -------- config.sh | 17 ++++++ loop.sh | 7 +++ slstatus.c | 146 ++++++++++++++++--------------------------------- status_reset.c | 10 ++++ 6 files changed, 80 insertions(+), 175 deletions(-) delete mode 100644 Makefile delete mode 100644 config.mk create mode 100644 config.sh create mode 100755 loop.sh create mode 100644 status_reset.c diff --git a/Makefile b/Makefile deleted file mode 100644 index d20ad8f..0000000 --- a/Makefile +++ /dev/null @@ -1,53 +0,0 @@ -# See LICENSE file for copyright and license details. - -include config.mk - -SRC = ${NAME}.c -OBJ = ${SRC:.c=.o} - -all: options ${NAME} - -options: - @echo ${NAME} build options: - @echo "CFLAGS = ${CFLAGS}" - @echo "LDFLAGS = ${LDFLAGS}" - @echo "CC = ${CC}" - -.c.o: - @echo CC $< - @${CC} -c ${CFLAGS} $< - -${OBJ}: config.h config.mk - -config.h: config.def.h - @echo creating $@ from config.def.h - @cp config.def.h $@ - -${NAME}: ${OBJ} - @echo CC -o $@ - @${CC} -o $@ ${OBJ} ${LDFLAGS} - -clean: - @echo cleaning - @rm -f ${NAME} ${OBJ} ${NAME}-${VERSION}.tar.gz - -dist: clean - @echo creating dist tarball - @mkdir -p ${NAME}-${VERSION} - @cp -R Makefile config.mk LICENSE \ - ${SRC} ${NAME}-${VERSION} - @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} - @gzip ${NAME}-${VERSION}.tar - @rm -rf ${NAME}-${VERSION} - -install: all - @echo installing executable file to ${DESTDIR}${PREFIX}/bin - @mkdir -p ${DESTDIR}${PREFIX}/bin - @cp -f ${NAME} ${DESTDIR}${PREFIX}/bin - @chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} - -uninstall: - @echo removing executable file from ${DESTDIR}${PREFIX}/bin - @rm -f ${DESTDIR}${PREFIX}/bin/${NAME} - -.PHONY: all options clean dist install uninstall diff --git a/config.mk b/config.mk deleted file mode 100644 index 704558a..0000000 --- a/config.mk +++ /dev/null @@ -1,22 +0,0 @@ -NAME = slstatus -VERSION = 1.0 - -# Customize below to fit your system - -PREFIX = /usr/local - -X11INC = /usr/X11R6/include -X11LIB = /usr/X11R6/lib - -# includes and libs -INCS = -I. -I/usr/include -I${X11INC} -LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound - -# flags -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE -CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} -LDFLAGS = ${LIBS} - -# compiler and linker -CC = cc - diff --git a/config.sh b/config.sh new file mode 100644 index 0000000..c460bd9 --- /dev/null +++ b/config.sh @@ -0,0 +1,17 @@ +NAME="slstatus" +VERSION="1.0" + +PREFIX="/usr/local" + +X11INC="/usr/X11R6/include" +X11LIB="/usr/X11R6/lib" + +INCS="-I. -I/usr/include -I${X11INC}" +LIBS="-L/usr/lib -lc -L${X11LIB} -lX11 -lasound" + +CPPFLAGS="-DVERSION=\"${VERSION}\" -D_GNU_SOURCE" +CFLAGS="-std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS}" +LDFLAGS="${LIBS}" + +CC = cc + diff --git a/loop.sh b/loop.sh new file mode 100755 index 0000000..be231cf --- /dev/null +++ b/loop.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +while true +do + slstatus + status_reset +done 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); + stderr = stderr; + dpy = XOpenDisplay(NULL); - 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); + 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; } diff --git a/status_reset.c b/status_reset.c new file mode 100644 index 0000000..eebaac6 --- /dev/null +++ b/status_reset.c @@ -0,0 +1,10 @@ +#include + +int +main(void) +{ + Display *dpy = XOpenDisplay(NULL); + XStoreName(dpy, DefaultRootWindow(dpy), NULL); + XCloseDisplay(dpy); + return (0); +} From 52d19f955e67712eab50f582046013246b6bc75d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:18:55 +0300 Subject: [PATCH 40/47] imported a new vol_perc() function, this should fix #12 (UNTESTED) --- slstatus.c | 53 ++++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) 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); + snd_mixer_handle_events(handle); + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); - 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); + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); - if (!mute) - return smprintf("mute"); - else - return smprintf("%d%%", (vol * 100) / max); + return smprintf("%d", (vol * 100) / max); } static char * From a4beda8eb9bdf34d055ba52c39b15b9580b27e6a Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:21:03 +0300 Subject: [PATCH 41/47] bringed back the loop --- slstatus.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) 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); From a66b73f5f253f7f5388ae909e6901bf115d79c2f Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:40:10 +0300 Subject: [PATCH 42/47] bringed back the old config.mk --- config.sh | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 config.sh diff --git a/config.sh b/config.sh deleted file mode 100644 index c460bd9..0000000 --- a/config.sh +++ /dev/null @@ -1,17 +0,0 @@ -NAME="slstatus" -VERSION="1.0" - -PREFIX="/usr/local" - -X11INC="/usr/X11R6/include" -X11LIB="/usr/X11R6/lib" - -INCS="-I. -I/usr/include -I${X11INC}" -LIBS="-L/usr/lib -lc -L${X11LIB} -lX11 -lasound" - -CPPFLAGS="-DVERSION=\"${VERSION}\" -D_GNU_SOURCE" -CFLAGS="-std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS}" -LDFLAGS="${LIBS}" - -CC = cc - From b9b6486cda91051553c9f9d7be75468c64206eec Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 01:40:47 +0300 Subject: [PATCH 43/47] bringed back the old config.mk --- config.mk | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 config.mk diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..4745073 --- /dev/null +++ b/config.mk @@ -0,0 +1,19 @@ +VERSION = 1.0 + +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +X11INC = /usr/X11R6/include +X11LIB = /usr/X11R6/lib + +INCS = -I. -I/usr/include -I${X11INC} +LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound + +# flags +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE +CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} +LDFLAGS = ${LIBS} + +CC = cc +LD = ld + From 720328cef9a9d81d2bd876bdf89103782b9cab91 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 02:17:30 +0300 Subject: [PATCH 44/47] what kind of weed is that? --- Makefile | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ TODO.md | 1 + slstatus.c | 6 ++---- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4848eef --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +# See LICENSE file for copyright and license details. + +include config.mk + +NAME=slstatus + +SRC = ${NAME}.c +OBJ = ${SRC:.c=.o} + +all: options ${NAME} + +options: + @echo ${NAME} build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.h config.mk + +config.h: config.def.h + @echo creating $@ from config.def.h + @cp config.def.h $@ + +${NAME}: ${OBJ} + @echo CC -o $@ + @${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + @echo cleaning + @rm -f ${NAME} ${OBJ} ${NAME}-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p ${NAME}-${VERSION} + @cp -R Makefile config.mk LICENSE \ + ${SRC} ${NAME}-${VERSION} + @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} + @gzip ${NAME}-${VERSION}.tar + @rm -rf ${NAME}-${VERSION} + +install: all + @echo installing executable file to ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/bin + @cp -f ${NAME} ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} + +uninstall: + @echo removing executable file from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/bin/${NAME} + +.PHONY: all options clean dist install uninstall diff --git a/TODO.md b/TODO.md index ea038b2..f4b54f7 100644 --- a/TODO.md +++ b/TODO.md @@ -2,3 +2,4 @@ Todo ==== - slstatus icon (in that cool dwm icon style) +- include status_reset in the makefile 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; From 113979e5b8398a2c26736749480fe32b5c49a66b Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Mon, 5 Sep 2016 02:28:18 +0300 Subject: [PATCH 45/47] fixed some mistakes --- slstatus.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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); } From 2afea979877ae12226ab397355f3bf8c8e124e91 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 8 Sep 2016 04:31:49 +0300 Subject: [PATCH 46/47] used ccat() from concat.h for string concatenation --- concat.h | 19 +++++++++++++++++++ slstatus.c | 41 ++++++++++++++++------------------------- 2 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 concat.h diff --git a/concat.h b/concat.h new file mode 100644 index 0000000..7f2ea46 --- /dev/null +++ b/concat.h @@ -0,0 +1,19 @@ +/* + * Thanks to lloyd for contribution + */ + +extern char concat[8192]; + +extern void +ccat(const unsigned short int count, ...) +{ + va_list ap; + unsigned short int i; + concat[0] = '\0'; + + va_start(ap, count); + for(i = 0; i < count; i++) + strlcat(concat, va_arg(ap, char *), sizeof(concat)); + va_end(ap); + return; +} 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)); + ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW); - strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile)); - strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); - strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); - strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); - - 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); From 9b2dc253485ef3a0ac34a5e781bc2c856bec463d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Thu, 8 Sep 2016 04:45:00 +0300 Subject: [PATCH 47/47] use warn[x]() instead of long fprintf()s --- slstatus.c | 75 +++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 49 deletions(-) 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);