From 5a218e990ca05e6bb087f4fecaaa4807fbe69528 Mon Sep 17 00:00:00 2001 From: raiz Date: Sun, 18 Dec 2016 10:35:36 +0300 Subject: remove unused strlcpy() and reduce the size of output buffer --- slstatus.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From f62d626d9b7958362fcf3605bcd2280295c0a30c Mon Sep 17 00:00:00 2001 From: raiz Date: Sun, 18 Dec 2016 10:36:01 +0300 Subject: whhops --- slstatus.c | 1 - 1 file changed, 1 deletion(-) (limited to 'slstatus.c') 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 { -- cgit v1.2.3 From 3bfc3bbcae41c58cc527ee074468afc83ef1a9ab Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:54:16 +0300 Subject: strtok() has no effect on buf && fgets() should have the full buffer length --- slstatus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From b7f4541506aac11a92d947939f6e9da8af49dd26 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:57:27 +0300 Subject: remove newline character from run_command() output accurately --- slstatus.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From e1c5476291cf35693c072327cc5a0261983e1573 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 18:58:31 +0300 Subject: inititalize run_command()'s buf to UNKNOWN_STR by default --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'slstatus.c') 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) { -- cgit v1.2.3 From 6cab2d57199edd8cc9e292ded04558fe9279962b Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:07:19 +0300 Subject: 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(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From 862d158b7c3f1bd138c00e1bd29de87d3fc89f15 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:12:39 +0300 Subject: fix delay in a cleaner way --- slstatus.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'slstatus.c') 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) { -- cgit v1.2.3 From a36cb96f274c6dd829b140ffa604074477c7557a Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:26:04 +0300 Subject: swap_*(): add more error tests --- slstatus.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From 7db4f5cf67df1f2cab3a69a6efe8ae8b37bc70f6 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:49:31 +0300 Subject: add extra error tests to swap_*() && fix bytes_read bug --- slstatus.c | 77 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'slstatus.c') 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); + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_total: 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"); - sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); + 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"); - sscanf(match, "SwapTotal: %ld kB\n", &total); - if (total == 0) { - return smprintf(UNKNOWN_STR); + if ((match = strstr(buf, "SwapTotal")) == NULL) { + return smprintf("%s", UNKNOWN_STR); } + sscanf(match, "SwapTotal: %ld kB\n", &total); - 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); -- cgit v1.2.3 From 3c8717f4e01ec477d873ee7c3f3aa54eb60a7c87 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:53:46 +0300 Subject: allow the use of %* symbols in UNKNOWN_STR --- slstatus.c | 70 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'slstatus.c') 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)) -- cgit v1.2.3 From 13772c3979cbed6883fddb5811da9b56fb16e179 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:56:11 +0300 Subject: get rid of set_status() since it's only used once --- slstatus.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From 8b5b72b65efeaab5985e704f00e5865e3cfe4528 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 19:57:16 +0300 Subject: remove misleading comment --- slstatus.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'slstatus.c') 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; -- cgit v1.2.3 From 11fec4fa9501bd20aace72235c4525dc87d5d281 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:09:07 +0300 Subject: don't allow the use of perc if uninitialized --- slstatus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From 94e87eb8baaf1c20d02602ba3d01bc1babdb2e35 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:14:45 +0300 Subject: 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(-) (limited to 'slstatus.c') 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); -- cgit v1.2.3 From e9f738b8b6afa1d94db666d657319e0b5647cb1f Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:18:09 +0300 Subject: whhops, my bad --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'slstatus.c') 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); -- cgit v1.2.3 From cfb4a5d3ffeec74722101f394a1a3f0856217210 Mon Sep 17 00:00:00 2001 From: raiz Date: Tue, 27 Dec 2016 20:41:40 +0300 Subject: 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(-) (limited to 'slstatus.c') 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"); -- cgit v1.2.3 From 7392dbabebe69187618f31301a7842d4d2993966 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 28 Dec 2016 23:59:19 +0100 Subject: kernel_release() coding style and position in code fix --- slstatus.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'slstatus.c') 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) { -- cgit v1.2.3 From b231cd90ebe54eef8649996fc80cabc3a420c9e5 Mon Sep 17 00:00:00 2001 From: raiz Date: Thu, 29 Dec 2016 12:40:43 +0300 Subject: add braces to single statements so drkhsh doesn't be upset --- slstatus.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'slstatus.c') 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); } -- cgit v1.2.3 From b68925dbef2bcac96333e1f7cd3da7d2e4f67591 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 29 Dec 2016 23:30:53 +0100 Subject: #undef strlcpy not needed anymore --- slstatus.c | 1 - 1 file changed, 1 deletion(-) (limited to 'slstatus.c') 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" -- cgit v1.2.3 From 8fa622975991d1fb539ffab50c98f0d2d7d6da1c Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 29 Dec 2016 23:36:05 +0100 Subject: why releases for such a small project? useless and overhead! --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'slstatus.c') 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); -- cgit v1.2.3