From 7d8f5f4882abfbcc3b7ed2dce7ac584cdc2e88f3 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 18 Aug 2016 15:02:51 +0200 Subject: [PATCH 01/16] update sloc count in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43aaf38..4b517de 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ slstatus 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 **500 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) 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. The following information is included: -- 2.20.1 From 5c86bbd67f25815aa9c6c309697df7aea2d80330 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 18 Aug 2016 15:03:43 +0200 Subject: [PATCH 02/16] remove screenshot --- README.md | 2 -- screenshot.png | Bin 939 -> 0 bytes 2 files changed, 2 deletions(-) delete mode 100644 screenshot.png diff --git a/README.md b/README.md index 4b517de..e19e16d 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@ The following information is included: Multiple entries per function are supported and everything can be reordered and customized via the C header file config.h (similar to DWM). -![screenshot](screenshot.png) - ## Usage ### Installation diff --git a/screenshot.png b/screenshot.png deleted file mode 100644 index 9b04911be5968e3f0701fce4fe9f9b0bf1a62446..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 939 zcmV;c162HpP)@ z`9K%|006*JT#_I*3t)eN?kU(&#zzHX4?d)Ht@!bjZ>!gyZhQCdo1fKk+Ia)%Dc_N- zDf%D1e>VQ@`agaBWH0A*E}@PvpGXzm4ucJ-1p;yIoQ8ZRt?;OR)#a_&4kSE$ip>_h+(C4lfe_ zK6-AIect_+6>UxY%RrwZAIWpk^d<4>o4b3~p0ZBP;sSl0*INJO4*BPnuXOyK4*=^2 zfPH3&>sT}AW^iY?MuU3k;NG*d=RC_*PvsQrazTUys0oviN_AkMgp=OlvudKUpb1 ziLNa+priZV^ffV;mEOkvQDs57DN@!NIarqDs3pp> zK|7A5JT__<@YcVFepjv^E1$~I1?T;%+i5)Fs;1ohJJ3`jU=+)g~8M?h)Co+ z>$CT8vN-g_=@+o`Uy(_K`$pGm{aDjF(LTKsIGikT#M~@5y;}O3@!#boKtNq9&-*WL zsqGJ$PnMbF?JZxdpR@H#4K(^){ms(D1pNq5s3V#;zk9GhfJ$Ur(O0x^u%dOCF-1_qH|62dJ+CH^kR?yn7 zRlCGW`yB0IwEB%`CmHG3n!bmQR(|1WB%iN>f9neX$W8$O006*Y_ys&nsY1n6ILrV5 N002ovPDHLkV1l>Z&zk@M -- 2.20.1 From e1ae7d3be6faa710b3a711ed317cfe45d90f28bc Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:00:23 +0300 Subject: [PATCH 03/16] 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 */ +} -- 2.20.1 From 1853884520ddca4237dd0534256da0c8bf628251 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 00:28:36 +0300 Subject: [PATCH 04/16] 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 */ -- 2.20.1 From 2e1eb518afea7c93e3ba750804f1f4c4d0a51af4 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:10:14 +0300 Subject: [PATCH 05/16] the code describes itself, there is no need to write stories in /* */ --- slstatus.c | 223 +++++++---------------------------------------------- 1 file changed, 28 insertions(+), 195 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); - -/* global variables */ +void setstatus(const char *); +char *smprintf(const char *, ...); +char *battery_perc(const char *); +char *cpu_perc(const char *); +char *datetime(const char *); +char *disk_free(const char *); +char *disk_perc(const char *); +char *disk_total(const char *); +char *disk_used(const char *); +char *entropy(const char *); +char *gid(const char *); +char *hostname(const char *); +char *ip(const char *); +char *load_avg(const char *); +char *ram_free(const char *); +char *ram_perc(const char *); +char *ram_used(const char *); +char *ram_total(const char *); +char *run_command(const char *); +char *temp(const char *); +char *uid(const char *); +char *uptime(const char *); +char *username(const char *); +char *vol_perc(const char *); +char *wifi_perc(const char *); +char *wifi_essid(const char *); + static Display *dpy; -/* configuration header */ #include "config.h" -/* set statusbar */ void setstatus(const char *str) { @@ -81,7 +75,6 @@ setstatus(const char *str) XSync(dpy, False); } -/* smprintf function */ char * smprintf(const char *fmt, ...) { @@ -96,7 +89,6 @@ smprintf(const char *fmt, ...) return ret; } -/* battery percentage */ char * battery_perc(const char *battery) { @@ -105,50 +97,37 @@ battery_perc(const char *battery) char batteryfullfile[64] = ""; FILE *fp; - /* generate battery nowfile path */ strlcat(batterynowfile, batterypath, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); strlcat(batterynowfile, "/", sizeof(batterynowfile)); strlcat(batterynowfile, batterynow, sizeof(batterynowfile)); - /* generate battery fullfile path */ strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile)); strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile)); - /* open battery now file */ if (!(fp = fopen(batterynowfile, "r"))) { fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); return smprintf(unknowntext); } - /* read value */ fscanf(fp, "%i", &now); - - /* close battery now file */ fclose(fp); - /* open battery full file */ if (!(fp = fopen(batteryfullfile, "r"))) { fprintf(stderr, "Error opening battery file.\n"); return smprintf(unknowntext); } - /* read value */ fscanf(fp, "%i", &full); - - /* close battery full file */ fclose(fp); - /* calculate percent */ perc = now / (full / 100); - /* return perc as string */ return smprintf("%d%%", perc); } -/* cpu percentage */ char * cpu_perc(const char *null) { @@ -156,41 +135,28 @@ cpu_perc(const char *null) long double a[4], b[4]; FILE *fp; - /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); return smprintf(unknowntext); } - /* read values */ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); - - /* close stat file */ fclose(fp); /* wait a second (for avg values) */ sleep(1); - /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file.\n"); return smprintf(unknowntext); } - /* read values */ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]); - - /* close stat file */ fclose(fp); - - /* calculate avg in this second */ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); - - /* return perc as string */ return smprintf("%d%%", perc); } -/* date and time */ char * datetime(const char *timeformat) { @@ -202,7 +168,6 @@ datetime(const char *timeformat) return smprintf(unknowntext); } - /* get time in format */ time(&tm); setlocale(LC_TIME, ""); if (!strftime(buf, bufsize, timeformat, localtime(&tm))) { @@ -213,104 +178,80 @@ datetime(const char *timeformat) } setlocale(LC_TIME, "C"); - /* return time */ char *ret = smprintf("%s", buf); free(buf); return ret; } -/* disk free */ char * disk_free(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - - /* return free */ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); } -/* disk usage percentage */ char * disk_perc(const char *mountpoint) { int perc = 0; struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* calculate percent */ perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); - - /* return perc */ return smprintf("%d%%", perc); } -/* disk total */ char * disk_total(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* return total */ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); } -/* disk used */ char * disk_used(const char *mountpoint) { struct statvfs fs; - /* try to open mountpoint */ if (statvfs(mountpoint, &fs) < 0) { fprintf(stderr, "Could not get filesystem info.\n"); return smprintf(unknowntext); } - /* return used */ return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); } -/* entropy available */ char * entropy(const char *null) { int entropy = 0; FILE *fp; - /* open entropy file */ if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { fprintf(stderr, "Could not open entropy file.\n"); return smprintf(unknowntext); } - /* extract entropy */ fscanf(fp, "%d", &entropy); - - /* close entropy file */ fclose(fp); - - /* return entropy */ return smprintf("%d", entropy); } -/* gid */ char * gid(const char *null) { @@ -319,37 +260,27 @@ gid(const char *null) if ((gid = getgid()) < 0) { fprintf(stderr, "Could no get gid.\n"); return smprintf(unknowntext); - } else { + } else return smprintf("%d", gid); - } - return smprintf(unknowntext); } -/* hostname */ char * hostname(const char *null) { char hostname[HOST_NAME_MAX]; FILE *fp; - /* open hostname file */ if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { fprintf(stderr, "Could not open hostname file.\n"); return smprintf(unknowntext); } - /* extract hostname */ fscanf(fp, "%s\n", hostname); - - /* close hostname file */ fclose(fp); - - /* return entropy */ return smprintf("%s", hostname); } -/* ip address */ char * ip(const char *interface) { @@ -357,7 +288,6 @@ ip(const char *interface) int s; char host[NI_MAXHOST]; - /* check if getting ip address works */ if (getifaddrs(&ifaddr) == -1) { fprintf(stderr, "Error getting IP address.\n"); return smprintf(unknowntext); @@ -385,46 +315,35 @@ ip(const char *interface) return smprintf(unknowntext); } -/* load avg */ char * load_avg(const char *null) { double avgs[3]; - /* try to get load avg */ if (getloadavg(avgs, 3) < 0) { fprintf(stderr, "Error getting load avg.\n"); return smprintf(unknowntext); } - /* return it */ return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); } -/* ram free */ char * ram_free(const char *null) { long free; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemFree: %ld kB\n", &free); - - /* close meminfo file */ fclose(fp); - - /* return free ram as string */ return smprintf("%f", (float)free / 1024 / 1024); } -/* ram percentage */ char * ram_perc(const char *null) { @@ -432,81 +351,58 @@ ram_perc(const char *null) long total, free, buffers, cached; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers); fscanf(fp, "Cached: %ld kB\n", &cached); - /* close meminfo file */ fclose(fp); - - /* calculate percentage */ perc = 100 * ((total - free) - (buffers + cached)) / total; - - /* return perc as string */ return smprintf("%d%%", perc); } -/* ram total */ char * ram_total(const char *null) { long total; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); - - /* close meminfo file */ fclose(fp); - - /* return total ram as string */ return smprintf("%f", (float)total / 1024 / 1024); } -/* ram used */ char * ram_used(const char *null) { long free, total, buffers, cached, used; FILE *fp; - /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file.\n"); return smprintf(unknowntext); } - /* read the values */ fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers); fscanf(fp, "Cached: %ld kB\n", &cached); - /* close meminfo file */ fclose(fp); - - /* calculate used */ used = total - free - buffers - cached; - - /* return used ram as string */ return smprintf("%f", (float)used / 1024 / 1024); } -/* custom shell command */ char * run_command(const char* command) { @@ -514,19 +410,13 @@ run_command(const char* command) FILE *fp; char buffer[64]; - /* try to open the command output */ if (!(fp = popen(command, "r"))) { fprintf(stderr, "Could not get command output for: %s.\n", command); return smprintf(unknowntext); } - /* get command output text, save it to buffer */ fgets(buffer, sizeof(buffer) - 1, fp); - - /* close it again */ pclose(fp); - - /* add nullchar at the end */ for (int i = 0 ; i != sizeof(buffer); i++) { if (buffer[i] == '\0') { good = 1; @@ -535,35 +425,25 @@ run_command(const char* command) } if (good) buffer[strlen(buffer) - 1] = '\0'; - - /* return the output */ return smprintf("%s", buffer); } -/* temperature */ char * temp(const char *file) { int temperature; FILE *fp; - /* open temperature file */ if (!(fp = fopen(file, "r"))) { fprintf(stderr, "Could not open temperature file.\n"); return smprintf(unknowntext); } - /* extract temperature */ fscanf(fp, "%d", &temperature); - - /* close temperature file */ fclose(fp); - - /* return temperature in degrees */ return smprintf("%d°C", temperature / 1000); } -/* uptime */ char * uptime(const char *null) { @@ -571,27 +451,22 @@ uptime(const char *null) int hours = 0; int minutes = 0; - /* get info */ sysinfo(&info); hours = info.uptime / 3600; minutes = (info.uptime - hours * 3600 ) / 60; - /* return it */ return smprintf("%dh %dm", hours, minutes); } -/* username */ char * username(const char *null) { register struct passwd *pw; register uid_t uid; - /* get the values */ uid = geteuid(); pw = getpwuid(uid); - /* if it worked, return */ if (pw) return smprintf("%s", pw->pw_name); else { @@ -602,16 +477,13 @@ username(const char *null) return smprintf(unknowntext); } -/* uid */ char * uid(const char *null) { register uid_t uid; - /* get the values */ uid = geteuid(); - /* if it worked, return */ if (uid) return smprintf("%d", uid); else { @@ -623,7 +495,6 @@ uid(const char *null) } -/* alsa volume percentage */ char * vol_perc(const char *soundcard) { @@ -633,16 +504,13 @@ vol_perc(const char *soundcard) snd_mixer_elem_t *pcm_mixer, *mas_mixer; snd_mixer_selem_id_t *vol_info, *mute_info; - /* open everything */ snd_mixer_open(&handle, 0); snd_mixer_attach(handle, soundcard); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); - /* prepare everything */ snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); - /* check */ if (vol_info == NULL || mute_info == NULL) { fprintf(stderr, "Could not get alsa volume.\n"); return smprintf(unknowntext); @@ -652,12 +520,10 @@ vol_perc(const char *soundcard) pcm_mixer = snd_mixer_find_selem(handle, vol_info); mas_mixer = snd_mixer_find_selem(handle, mute_info); - /* get the info */ snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); - /* clean up */ if (vol_info) snd_mixer_selem_id_free(vol_info); if (mute_info) @@ -665,14 +531,12 @@ vol_perc(const char *soundcard) if (handle) snd_mixer_close(handle); - /* return the string (mute) */ if (!mute) return smprintf("mute"); else return smprintf("%d%%", (vol * 100) / max); } -/* wifi percentage */ char * wifi_perc(const char *wificard) { @@ -685,35 +549,26 @@ wifi_perc(const char *wificard) char needle[sizeof wificard + 1]; FILE *fp; - /* generate the path name */ memset(path, 0, sizeof path); strlcat(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); - /* open wifi file */ if(!(fp = fopen(path, "r"))) { fprintf(stderr, "Error opening wifi operstate file.\n"); return smprintf(unknowntext); } - /* read the status */ fgets(status, 5, fp); - - /* close wifi file */ fclose(fp); - - /* check if interface down */ if(strcmp(status, "up\n") != 0) return smprintf(unknowntext); - /* open wifi file */ if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "Error opening wireless file.\n"); return smprintf(unknowntext); } - /* extract the signal strength */ strlcpy(needle, wificard, sizeof(needle)); strlcat(needle, ":", sizeof(needle)); fgets(buf, bufsize, fp); @@ -724,14 +579,10 @@ wifi_perc(const char *wificard) sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength); } - /* close wifi file */ fclose(fp); - - /* return strength in percent */ return smprintf("%d%%", strength); } -/* wifi essid */ char * wifi_essid(const char *wificard) { @@ -739,14 +590,9 @@ wifi_essid(const char *wificard) int sockfd; struct iwreq wreq; - /* prepare */ memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; - - /* set the interface */ sprintf(wreq.ifr_name, wificard); - - /* check */ if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); return smprintf(unknowntext); @@ -757,32 +603,25 @@ wifi_essid(const char *wificard) return smprintf(unknowntext); } - /* return the essid */ if (strcmp((char *)wreq.u.essid.pointer, "") == 0) return smprintf(unknowntext); else return smprintf("%s", (char *)wreq.u.essid.pointer); } -/* main function */ int main(void) { char status_string[1024]; struct arg argument; - /* try to open display */ if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); exit(1); } - /* return status every interval */ for (;;) { - /* clear the string */ memset(status_string, 0, sizeof(status_string)); - - /* generate status_string */ for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; char *res = argument.func(argument.args); @@ -796,16 +635,10 @@ main(void) free(element); } - /* return the statusbar */ setstatus(status_string); - - /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */ sleep(update_interval -1); } - /* close display */ XCloseDisplay(dpy); - - /* exit successfully */ return 0; } -- 2.20.1 From 73ec65a84d422fe4bd15a8d3572875f2413926e6 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:10:37 +0300 Subject: [PATCH 06/16] -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} -- 2.20.1 From 46b6876d7ec6e17bb51ee647f77bd238c864ca25 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 11:28:42 +0300 Subject: [PATCH 07/16] 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]; -- 2.20.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 08/16] (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); -- 2.20.1 From 0da2af8c621aff973cd711f33a5348ea82b6b09d Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 12:04:01 +0300 Subject: [PATCH 09/16] 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) -- 2.20.1 From f65fb9bca18bb445de2c337ed9e4d84de5b631f7 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:19:45 +0300 Subject: [PATCH 10/16] 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 11/16] 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 12/16] 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" }, }; -- 2.20.1 From 6f31ca6ce3ea722c7d1ecfe2099108133ce4215c Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 21 Aug 2016 15:43:57 +0300 Subject: [PATCH 13/16] 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); -- 2.20.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 14/16] 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; -- 2.20.1 From f9f72a06f11de9ecd1b14811eba63c6e8c7a1165 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 13:27:42 +0300 Subject: [PATCH 15/16] 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) -- 2.20.1 From b79837b075305f8430859d014f892333a1b8c782 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Tue, 23 Aug 2016 14:11:55 +0300 Subject: [PATCH 16/16] 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 -- 2.20.1