From 5a218e990ca05e6bb087f4fecaaa4807fbe69528 Mon Sep 17 00:00:00 2001 From: raiz Date: Sun, 18 Dec 2016 10:35:36 +0300 Subject: [PATCH 01/26] remove unused strlcpy() and reduce the size of output buffer --- extern/strlcpy.h | 50 ------------------------------------------------ slstatus.c | 5 +++-- 2 files changed, 3 insertions(+), 52 deletions(-) delete mode 100644 extern/strlcpy.h diff --git a/extern/strlcpy.h b/extern/strlcpy.h deleted file mode 100644 index 6301674..0000000 --- a/extern/strlcpy.h +++ /dev/null @@ -1,50 +0,0 @@ -/* $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 */ -} diff --git a/slstatus.c b/slstatus.c index 2b63be4..2d5d173 100644 --- a/slstatus.c +++ b/slstatus.c @@ -759,7 +759,7 @@ int main(int argc, char *argv[]) { unsigned short int i; - char status_string[4096]; + char status_string[2048]; char *res, *element; struct arg argument; struct sigaction act; @@ -811,7 +811,8 @@ main(int argc, char *argv[]) element = smprintf(UNKNOWN_STR); warnx("Failed to format output"); } - strlcat(status_string, element, sizeof(status_string)); + if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) + warnx("Output too long"); free(res); free(element); } From f62d626d9b7958362fcf3605bcd2280295c0a30c Mon Sep 17 00:00:00 2001 From: raiz Date: Sun, 18 Dec 2016 10:36:01 +0300 Subject: [PATCH 02/26] whhops --- slstatus.c | 1 - 1 file changed, 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 2d5d173..21cda51 100644 --- a/slstatus.c +++ b/slstatus.c @@ -30,7 +30,6 @@ #include "extern/arg.h" #include "extern/strlcat.h" -#include "extern/strlcpy.h" #include "extern/concat.h" struct arg { From 3bfc3bbcae41c58cc527ee074468afc83ef1a9ab Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:54:16 +0300 Subject: [PATCH 03/26] strtok() has no effect on buf && fgets() should have the full buffer length --- slstatus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/slstatus.c b/slstatus.c index 21cda51..73e0f2c 100644 --- a/slstatus.c +++ b/slstatus.c @@ -413,11 +413,10 @@ run_command(const char *cmd) warn("Failed to get command output for %s", cmd); return smprintf(UNKNOWN_STR); } - fgets(buf, sizeof(buf)-1, fp); + fgets(buf, sizeof(buf), fp); pclose(fp); buf[strlen(buf)] = '\0'; - strtok(buf, "\n"); return smprintf("%s", buf); } From b7f4541506aac11a92d947939f6e9da8af49dd26 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:57:27 +0300 Subject: [PATCH 04/26] remove newline character from run_command() output accurately --- slstatus.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 73e0f2c..329093f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -405,6 +405,7 @@ ram_used(void) static char * run_command(const char *cmd) { + char *nlptr; FILE *fp; char buf[1024] = "n/a"; @@ -415,9 +416,12 @@ run_command(const char *cmd) } fgets(buf, sizeof(buf), fp); pclose(fp); - buf[strlen(buf)] = '\0'; + if ((nlptr = strstr(buf, "\n")) != NULL) { + nlptr[0] = '\0'; + } + return smprintf("%s", buf); } From e1c5476291cf35693c072327cc5a0261983e1573 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:58:31 +0300 Subject: [PATCH 05/26] inititalize run_command()'s buf to UNKNOWN_STR by default --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 329093f..b593765 100644 --- a/slstatus.c +++ b/slstatus.c @@ -407,7 +407,7 @@ run_command(const char *cmd) { char *nlptr; FILE *fp; - char buf[1024] = "n/a"; + char buf[1024] = UNKNOWN_STR; fp = popen(cmd, "r"); if (fp == NULL) { From 6cab2d57199edd8cc9e292ded04558fe9279962b Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:07:19 +0300 Subject: [PATCH 06/26] remove newlines from warn() in swap_*(), those functions already print a newline after printing string stderr --- slstatus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index b593765..e6e726e 100644 --- a/slstatus.c +++ b/slstatus.c @@ -443,7 +443,7 @@ swap_free(void) buf[bytes_read] = '\0'; fclose(fp); if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read /proc/meminfo\n"); + warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); } @@ -477,7 +477,7 @@ swap_perc(void) buf[bytes_read] = '\0'; fclose(fp); if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read /proc/meminfo\n"); + warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); } @@ -514,7 +514,7 @@ swap_total(void) buf[bytes_read] = '\0'; fclose(fp); if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read /proc/meminfo\n"); + warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); } @@ -545,7 +545,7 @@ swap_used(void) buf[bytes_read] = '\0'; fclose(fp); if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read /proc/meminfo\n"); + warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); } From 862d158b7c3f1bd138c00e1bd29de87d3fc89f15 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:12:39 +0300 Subject: [PATCH 07/26] fix delay in a cleaner way --- slstatus.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index e6e726e..c92da0d 100644 --- a/slstatus.c +++ b/slstatus.c @@ -75,7 +75,7 @@ static void usage(void); char *argv0; char concat[]; -static unsigned short int delay; +static unsigned short int delay = 0; static unsigned short int done; static unsigned short int dflag, oflag; static Display *dpy; @@ -164,7 +164,7 @@ cpu_perc(void) fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); fclose(fp); - delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1)); + delay++; sleep(delay); fp = fopen("/proc/stat", "r"); @@ -829,8 +829,13 @@ main(int argc, char *argv[]) * subtract delay time spend in function * calls from the actual global delay time */ - sleep(UPDATE_INTERVAL - delay); - delay = 0; + if ((UPDATE_INTERVAL - delay) <= 0) { + delay = 0; + continue; + } else { + sleep(UPDATE_INTERVAL - delay); + delay = 0; + } } if (!oflag) { From a36cb96f274c6dd829b140ffa604074477c7557a Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:26:04 +0300 Subject: [PATCH 08/26] swap_*(): add more error tests --- slstatus.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/slstatus.c b/slstatus.c index c92da0d..ccb7cbe 100644 --- a/slstatus.c +++ b/slstatus.c @@ -439,9 +439,15 @@ swap_free(void) warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } - bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_free: read error"); + fclose(fp); + } + buf[bytes_read] = '\0'; fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); @@ -473,9 +479,11 @@ swap_perc(void) warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); buf[bytes_read] = '\0'; fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR); @@ -487,12 +495,17 @@ swap_perc(void) return smprintf(UNKNOWN_STR); } - match = strstr(buf, "SwapCached"); + if ((match = strstr(buf, "SwapCached")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapFree"); + if ((match = strstr(buf, "SwapFree")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapFree: %ld kB\n", &free); + return smprintf("%d%%", 100 * (total - free - cached) / total); } From 7db4f5cf67df1f2cab3a69a6efe8ae8b37bc70f6 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:49:31 +0300 Subject: [PATCH 09/26] add extra error tests to swap_*() && fix bytes_read bug --- slstatus.c | 79 +++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/slstatus.c b/slstatus.c index ccb7cbe..ac09c9f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -443,23 +443,20 @@ swap_free(void) if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { warn("swap_free: read error"); fclose(fp); + return smprintf("%s", UNKNOWN_STR); } buf[bytes_read] = '\0'; fclose(fp); - if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read from /proc/meminfo"); - return smprintf(UNKNOWN_STR); + if ((match = strstr(buf, "SwapTotal")) == NULL) { + return smprintf("%s", UNKNOWN_STR); } - - match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); - } - match = strstr(buf, "SwapFree"); + if ((match = strstr(buf, "SwapFree")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapFree: %ld kB\n", &free); return smprintf("%f", (float)free / 1024 / 1024); @@ -480,20 +477,19 @@ swap_perc(void) return smprintf(UNKNOWN_STR); } - bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_perc: read error"); + fclose(fp); + return smprintf("%s", UNKNOWN_STR); + } + buf[bytes_read] = '\0'; fclose(fp); - if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read from /proc/meminfo"); - return smprintf(UNKNOWN_STR); + if ((match = strstr(buf, "SwapTotal")) == NULL) { + return smprintf("%s", UNKNOWN_STR); } - - match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); - } if ((match = strstr(buf, "SwapCached")) == NULL) { return smprintf("%s", UNKNOWN_STR); @@ -505,7 +501,6 @@ swap_perc(void) } sscanf(match, "SwapFree: %ld kB\n", &free); - return smprintf("%d%%", 100 * (total - free - cached) / total); } @@ -523,19 +518,19 @@ swap_total(void) warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } - bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); - buf[bytes_read] = '\0'; - fclose(fp); - if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read from /proc/meminfo"); - return smprintf(UNKNOWN_STR); + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_total: read error"); + fclose(fp); + return smprintf("%s", UNKNOWN_STR); } - match = strstr(buf, "SwapTotal"); - sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); + buf[bytes_read] = '\0'; + fclose(fp); + + if ((match = strstr(buf, "SwapTotal")) == NULL) { + return smprintf("%s", UNKNOWN_STR); } + sscanf(match, "SwapTotal: %ld kB\n", &total); return smprintf("%f", (float)total / 1024 / 1024); } @@ -554,24 +549,28 @@ swap_used(void) warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } - bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_used: read error"); + fclose(fp); + return smprintf("%s", UNKNOWN_STR); + } + buf[bytes_read] = '\0'; fclose(fp); - if (bytes_read == 0 || bytes_read == sizeof(buf)) { - warn("Failed to read from /proc/meminfo"); - return smprintf(UNKNOWN_STR); - } - match = strstr(buf, "SwapTotal"); + if ((match = strstr(buf, "SwapTotal")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); - } - match = strstr(buf, "SwapCached"); + if ((match = strstr(buf, "SwapCached")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapFree"); + if ((match = strstr(buf, "SwapFree")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapFree: %ld kB\n", &free); return smprintf("%f", (float)(total - free - cached) / 1024 / 1024); From 3c8717f4e01ec477d873ee7c3f3aa54eb60a7c87 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:53:46 +0300 Subject: [PATCH 10/26] allow the use of %* symbols in UNKNOWN_STR --- slstatus.c | 70 +++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/slstatus.c b/slstatus.c index ac09c9f..56f2649 100644 --- a/slstatus.c +++ b/slstatus.c @@ -115,7 +115,7 @@ battery_perc(const char *bat) fp = fopen(concat, "r"); if (fp == NULL) { warn("Failed to open file %s", concat); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%i", &perc); fclose(fp); @@ -133,7 +133,7 @@ battery_state(const char *bat) fp = fopen(concat, "r"); if (fp == NULL) { warn("Failed to open file %s", concat); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%12s", state); fclose(fp); @@ -159,7 +159,7 @@ cpu_perc(void) fp = fopen("/proc/stat", "r"); if (fp == NULL) { warn("Failed to open file /proc/stat"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); fclose(fp); @@ -170,7 +170,7 @@ cpu_perc(void) fp = fopen("/proc/stat", "r"); if (fp == NULL) { warn("Failed to open file /proc/stat"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]); fclose(fp); @@ -187,7 +187,7 @@ datetime(const char *fmt) t = time(NULL); if (strftime(str, sizeof(str), fmt, localtime(&t)) == 0) { - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%s", str); @@ -200,7 +200,7 @@ disk_free(const char *mnt) if (statvfs(mnt, &fs) < 0) { warn("Failed to get filesystem info"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); @@ -214,7 +214,7 @@ disk_perc(const char *mnt) if (statvfs(mnt, &fs) < 0) { warn("Failed to get filesystem info"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); @@ -229,7 +229,7 @@ disk_total(const char *mnt) if (statvfs(mnt, &fs) < 0) { warn("Failed to get filesystem info"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024); @@ -242,7 +242,7 @@ disk_used(const char *mnt) if (statvfs(mnt, &fs) < 0) { warn("Failed to get filesystem info"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); @@ -257,7 +257,7 @@ entropy(void) fp= fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { warn("Failed to open file /proc/sys/kernel/random/entropy_avail"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%d", &num); fclose(fp); @@ -278,7 +278,7 @@ hostname(void) if (gethostname(buf, sizeof(buf)) == -1) { warn("hostname"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%s", buf); @@ -293,7 +293,7 @@ ip(const char *iface) if (getifaddrs(&ifaddr) == -1) { warn("Failed to get IP address for interface %s", iface); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { @@ -304,7 +304,7 @@ ip(const char *iface) if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { warnx("Failed to get IP address for interface %s", iface); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%s", host); } @@ -312,7 +312,7 @@ ip(const char *iface) freeifaddrs(ifaddr); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } static char * @@ -322,7 +322,7 @@ load_avg(void) if (getloadavg(avgs, 3) < 0) { warnx("Failed to get the load avg"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); @@ -337,7 +337,7 @@ ram_free(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "MemFree: %ld kB\n", &free); fclose(fp); @@ -354,7 +354,7 @@ ram_perc(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); @@ -374,7 +374,7 @@ ram_total(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); fclose(fp); @@ -391,7 +391,7 @@ ram_used(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemFree: %ld kB\n", &free); @@ -412,7 +412,7 @@ run_command(const char *cmd) fp = popen(cmd, "r"); if (fp == NULL) { warn("Failed to get command output for %s", cmd); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fgets(buf, sizeof(buf), fp); pclose(fp); @@ -437,7 +437,7 @@ swap_free(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { @@ -474,7 +474,7 @@ swap_perc(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { @@ -516,7 +516,7 @@ swap_total(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { warn("swap_total: read error"); @@ -547,7 +547,7 @@ swap_used(void) fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { warn("Failed to open file /proc/meminfo"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { warn("swap_used: read error"); @@ -585,7 +585,7 @@ temp(const char *file) fp = fopen(file, "r"); if (fp == NULL) { warn("Failed to open file %s", file); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fscanf(fp, "%d", &temp); fclose(fp); @@ -615,7 +615,7 @@ username(void) if (pw == NULL) { warn("Failed to get username"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } return smprintf("%s", pw->pw_name); @@ -649,7 +649,7 @@ vol_perc(const char *card) snd_mixer_selem_id_free(s_elem); snd_mixer_close(handle); warn("Failed to get volume percentage for %s", card); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } snd_mixer_handle_events(handle); @@ -681,18 +681,18 @@ wifi_perc(const char *iface) fp = fopen(concat, "r"); if (fp == NULL) { warn("Failed to open file %s", concat); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fgets(status, 5, fp); fclose(fp); if(strcmp(status, "up\n") != 0) { - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { warn("Failed to open file /proc/net/wireless"); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } ccat(2, iface, ":"); fgets(buf, sizeof(buf), fp); @@ -721,18 +721,18 @@ wifi_essid(const char *iface) sprintf(wreq.ifr_name, iface); if (sockfd == -1) { warn("Failed to get ESSID for interface %s", iface); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { warn("Failed to get ESSID for interface %s", iface); - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); } close(sockfd); if (strcmp((char *)wreq.u.essid.pointer, "") == 0) - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); else return smprintf("%s", (char *)wreq.u.essid.pointer); } @@ -742,7 +742,7 @@ kernel_release(void) { struct utsname udata; if (uname(&udata) < 0) - return smprintf(UNKNOWN_STR); + return smprintf("%s", UNKNOWN_STR); return smprintf("%s", udata.release); } @@ -822,7 +822,7 @@ main(int argc, char *argv[]) } element = smprintf(argument.fmt, res); if (element == NULL) { - element = smprintf(UNKNOWN_STR); + element = smprintf("%s", UNKNOWN_STR); warnx("Failed to format output"); } if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) From 13772c3979cbed6883fddb5811da9b56fb16e179 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:56:11 +0300 Subject: [PATCH 11/26] get rid of set_status() since it's only used once --- slstatus.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/slstatus.c b/slstatus.c index 56f2649..8c65435 100644 --- a/slstatus.c +++ b/slstatus.c @@ -69,7 +69,6 @@ static char *vol_perc(const char *card); static char *wifi_perc(const char *iface); static char *wifi_essid(const char *iface); static char *kernel_release(void); -static void set_status(const char *str); static void sighandler(const int signo); static void usage(void); @@ -747,13 +746,6 @@ kernel_release(void) return smprintf("%s", udata.release); } -static void -set_status(const char *str) -{ - XStoreName(dpy, DefaultRootWindow(dpy), str); - XSync(dpy, False); -} - static void sighandler(const int signo) { @@ -832,7 +824,8 @@ main(int argc, char *argv[]) } if (!oflag) { - set_status(status_string); + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); } else { printf("%s\n", status_string); } @@ -851,7 +844,7 @@ main(int argc, char *argv[]) } if (!oflag) { - set_status(NULL); + XStoreName(dpy, DefaultRootWindow(dpy), NULL); XCloseDisplay(dpy); } From 8b5b72b65efeaab5985e704f00e5865e3cfe4528 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:57:16 +0300 Subject: [PATCH 12/26] remove misleading comment --- slstatus.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index 8c65435..ad0b960 100644 --- a/slstatus.c +++ b/slstatus.c @@ -830,10 +830,6 @@ main(int argc, char *argv[]) printf("%s\n", status_string); } - /* - * subtract delay time spend in function - * calls from the actual global delay time - */ if ((UPDATE_INTERVAL - delay) <= 0) { delay = 0; continue; From 11fec4fa9501bd20aace72235c4525dc87d5d281 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:09:07 +0300 Subject: [PATCH 13/26] don't allow the use of perc if uninitialized --- slstatus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index ad0b960..7b02205 100644 --- a/slstatus.c +++ b/slstatus.c @@ -699,11 +699,11 @@ wifi_perc(const char *iface) fgets(buf, sizeof(buf), fp); fclose(fp); - datastart = strstr(buf, concat); - if (datastart != NULL) { - datastart = strstr(buf, ":"); - sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc); + if ((datastart = strstr(buf, concat)) == NULL) { + return smprintf("%s", UNKNOWN_STR); } + datastart = (datastart+(strlen(iface)+1)); + sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc); return smprintf("%d%%", perc); } From 94e87eb8baaf1c20d02602ba3d01bc1babdb2e35 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:14:45 +0300 Subject: [PATCH 14/26] wifi_essid: add boundary check && prevent the use of direct string literal as a format string in snprintf() --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 7b02205..c852b2f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -717,7 +717,8 @@ wifi_essid(const char *iface) memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; - sprintf(wreq.ifr_name, iface); + snprintf(wreq.ifr_name, ifrn_name, "%s", iface); + if (sockfd == -1) { warn("Failed to get ESSID for interface %s", iface); return smprintf("%s", UNKNOWN_STR); From e9f738b8b6afa1d94db666d657319e0b5647cb1f Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:18:09 +0300 Subject: [PATCH 15/26] whhops, my bad --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index c852b2f..6a338f9 100644 --- a/slstatus.c +++ b/slstatus.c @@ -717,7 +717,7 @@ wifi_essid(const char *iface) memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; - snprintf(wreq.ifr_name, ifrn_name, "%s", iface); + snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); if (sockfd == -1) { warn("Failed to get ESSID for interface %s", iface); From ba0f0998d9e782714f1007ca3e812403e5a4d0f7 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:18:33 +0300 Subject: [PATCH 16/26] extern/concat.h: don't segfault if count == 0 --- extern/concat.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extern/concat.h b/extern/concat.h index 7f2ea46..0f3be99 100644 --- a/extern/concat.h +++ b/extern/concat.h @@ -11,6 +11,9 @@ ccat(const unsigned short int count, ...) unsigned short int i; concat[0] = '\0'; + if (count == 0) + return; + va_start(ap, count); for(i = 0; i < count; i++) strlcat(concat, va_arg(ap, char *), sizeof(concat)); From cf67aebec579e85b5012e7efef4af220bee8981e Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:20:45 +0300 Subject: [PATCH 17/26] homepage update --- CONTRIBUTORS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 3ece0d9..610d4bf 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,6 +6,6 @@ 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) +- [Ali H. Fardan](http://hexeract.info) - [Quentin Rameau](https://fifth.space) - [Mike Coddington](https://coddington.us) From 2994a8d0ba51c8afe889728617b30db351bfd82a Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:32:22 +0300 Subject: [PATCH 18/26] TODO update --- TODO.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..3ad8835 --- /dev/null +++ b/TODO.md @@ -0,0 +1,4 @@ +[ ] fix build on OpenBSD +[ ] fix build on NetBSD +[ ] fix build on FreeBSD +[ ] test wifi functions after the last few commits From d1dd72bae955d8f2c0861db5a8c449e423ee3495 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:33:28 +0300 Subject: [PATCH 19/26] another TODO update --- TODO.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TODO.md b/TODO.md index 3ad8835..314cd91 100644 --- a/TODO.md +++ b/TODO.md @@ -2,3 +2,5 @@ [ ] fix build on NetBSD [ ] fix build on FreeBSD [ ] test wifi functions after the last few commits +[ ] allow calling routines from command line arguments like: slstatus --datetime "%F %T" + [ ] see if drkhsh approves it From cfb4a5d3ffeec74722101f394a1a3f0856217210 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:41:40 +0300 Subject: [PATCH 20/26] usage(): return success if -h is called && print more appropriate usage, opts can't be all called at the same time, that's why a separator is required --- slstatus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/slstatus.c b/slstatus.c index 6a338f9..1f08514 100644 --- a/slstatus.c +++ b/slstatus.c @@ -70,7 +70,7 @@ static char *wifi_perc(const char *iface); static char *wifi_essid(const char *iface); static char *kernel_release(void); static void sighandler(const int signo); -static void usage(void); +static void usage(int); char *argv0; char concat[]; @@ -756,10 +756,10 @@ sighandler(const int signo) } static void -usage(void) +usage(int eval) { - fprintf(stderr, "usage: %s [-dhov]\n", argv0); - exit(1); + fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0); + exit(eval); } int @@ -781,12 +781,14 @@ main(int argc, char *argv[]) case 'v': printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION); return 0; + case 'h': + usage(0); default: - usage(); + usage(1); } ARGEND if (dflag && oflag) { - usage(); + usage(1); } if (dflag && daemon(1, 1) < 0) { err(1, "daemon"); From 7392dbabebe69187618f31301a7842d4d2993966 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 28 Dec 2016 23:59:19 +0100 Subject: [PATCH 21/26] kernel_release() coding style and position in code fix --- slstatus.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/slstatus.c b/slstatus.c index 2b63be4..7ef9e23 100644 --- a/slstatus.c +++ b/slstatus.c @@ -52,6 +52,7 @@ static char *entropy(void); static char *gid(void); static char *hostname(void); static char *ip(const char *iface); +static char *kernel_release(void); static char *load_avg(void); static char *ram_free(void); static char *ram_perc(void); @@ -69,7 +70,6 @@ static char *username(void); static char *vol_perc(const char *card); static char *wifi_perc(const char *iface); static char *wifi_essid(const char *iface); -static char *kernel_release(void); static void set_status(const char *str); static void sighandler(const int signo); static void usage(void); @@ -316,6 +316,18 @@ ip(const char *iface) return smprintf(UNKNOWN_STR); } +static char * +kernel_release(void) +{ + struct utsname udata; + + if (uname(&udata) < 0) { + return smprintf(UNKNOWN_STR); + } + + return smprintf("%s", udata.release); +} + static char * load_avg(void) { @@ -723,16 +735,6 @@ wifi_essid(const char *iface) return smprintf("%s", (char *)wreq.u.essid.pointer); } -static char * -kernel_release(void) -{ - struct utsname udata; - if (uname(&udata) < 0) - return smprintf(UNKNOWN_STR); - - return smprintf("%s", udata.release); -} - static void set_status(const char *str) { From b231cd90ebe54eef8649996fc80cabc3a420c9e5 Mon Sep 17 00:00:00 2001 From: raiz Date: Thu, 29 Dec 2016 12:40:43 +0300 Subject: [PATCH 22/26] add braces to single statements so drkhsh doesn't be upset --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 1f08514..ac4c962 100644 --- a/slstatus.c +++ b/slstatus.c @@ -820,8 +820,9 @@ main(int argc, char *argv[]) element = smprintf("%s", UNKNOWN_STR); warnx("Failed to format output"); } - if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) + if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) { warnx("Output too long"); + } free(res); free(element); } From 0a3aa0b4e6dac661f3c318ac3f61e01668442903 Mon Sep 17 00:00:00 2001 From: raiz Date: Thu, 29 Dec 2016 15:37:17 +0300 Subject: [PATCH 23/26] fix TODO markdown syntax && update --- TODO.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index 314cd91..c95b57d 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,6 @@ -[ ] fix build on OpenBSD -[ ] fix build on NetBSD -[ ] fix build on FreeBSD -[ ] test wifi functions after the last few commits -[ ] allow calling routines from command line arguments like: slstatus --datetime "%F %T" - [ ] see if drkhsh approves it +* [ ] fix build on OpenBSD +* [ ] fix build on NetBSD +* [ ] fix build on FreeBSD +* [ ] test wifi functions after the last few commits +* [ ] allow calling routines from command line arguments like: slstatus --datetime "%F %T" +* [X] see if drkhsh approves it From d7fbcb3586de88bdd7a99ff25221646adf19a38e Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 29 Dec 2016 23:30:17 +0100 Subject: [PATCH 24/26] tested wifi --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index c95b57d..0c8b552 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,6 @@ * [ ] fix build on OpenBSD * [ ] fix build on NetBSD * [ ] fix build on FreeBSD -* [ ] test wifi functions after the last few commits +* [X] test wifi functions after the last few commits * [ ] allow calling routines from command line arguments like: slstatus --datetime "%F %T" * [X] see if drkhsh approves it From b68925dbef2bcac96333e1f7cd3da7d2e4f67591 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 29 Dec 2016 23:30:53 +0100 Subject: [PATCH 25/26] #undef strlcpy not needed anymore --- slstatus.c | 1 - 1 file changed, 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 46e13c9..1f5a3b2 100644 --- a/slstatus.c +++ b/slstatus.c @@ -26,7 +26,6 @@ #include #undef strlcat -#undef strlcpy #include "extern/arg.h" #include "extern/strlcat.h" From 8fa622975991d1fb539ffab50c98f0d2d7d6da1c Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 29 Dec 2016 23:36:05 +0100 Subject: [PATCH 26/26] why releases for such a small project? useless and overhead! --- Makefile | 14 +++++++------- config.mk | 2 -- slstatus.1 | 2 +- slstatus.c | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 85c001f..6cba860 100644 --- a/Makefile +++ b/Makefile @@ -31,16 +31,16 @@ ${NAME}: ${OBJ} clean: @echo cleaning - @rm -f ${NAME} ${OBJ} ${NAME}-${VERSION}.tar.gz + @rm -f ${NAME} ${OBJ} ${NAME}.tar.gz dist: clean @echo creating dist tarball - @mkdir -p ${NAME}-${VERSION} + @mkdir -p ${NAME} @cp -R Makefile config.mk LICENSE \ - ${SRC} ${NAME}-${VERSION} - @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} - @gzip ${NAME}-${VERSION}.tar - @rm -rf ${NAME}-${VERSION} + ${SRC} ${NAME} + @tar -cf ${NAME}.tar ${NAME} + @gzip ${NAME}.tar + @rm -rf ${NAME} install: all @echo installing executable file to ${DESTDIR}${PREFIX}/bin @@ -49,7 +49,7 @@ install: all @chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1 @mkdir -p ${DESTDIR}${MANPREFIX}/man1 - @sed "s/VERSION/${VERSION}/g" < ${NAME}.1 > ${DESTDIR}${MANPREFIX}/man1/${NAME}.1 + @cp -f ${NAME}.1 ${DESTDIR}${MANPREFIX}/man1 @chmod 644 ${DESTDIR}${MANPREFIX}/man1/${NAME}.1 uninstall: diff --git a/config.mk b/config.mk index c120e76..b5053fe 100644 --- a/config.mk +++ b/config.mk @@ -1,7 +1,5 @@ # See LICENSE file for copyright and license details. -VERSION = 3.0 - PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man diff --git a/slstatus.1 b/slstatus.1 index aaeb42d..afc3505 100644 --- a/slstatus.1 +++ b/slstatus.1 @@ -1,4 +1,4 @@ -.TH SLSTATUS 1 slstatus\-VERSION +.TH SLSTATUS 1 slstatus .SH NAME slstatus \- suckless status .SH SYNOPSIS diff --git a/slstatus.c b/slstatus.c index 1f5a3b2..b80bb7f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -780,7 +780,7 @@ main(int argc, char *argv[]) oflag = 1; break; case 'v': - printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION); + printf("slstatus (C) 2016 slstatus engineers\n"); return 0; case 'h': usage(0);