X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/bcd5732b04d5b8b6572b2d1f122a2762316ea476..e1c5476291cf35693c072327cc5a0261983e1573:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 9559226..b593765 100644 --- a/slstatus.c +++ b/slstatus.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -29,7 +30,6 @@ #include "extern/arg.h" #include "extern/strlcat.h" -#include "extern/strlcpy.h" #include "extern/concat.h" struct arg { @@ -68,6 +68,7 @@ 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); @@ -404,19 +405,22 @@ ram_used(void) static char * 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) { 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"); + + if ((nlptr = strstr(buf, "\n")) != NULL) { + nlptr[0] = '\0'; + } return smprintf("%s", buf); } @@ -721,6 +725,16 @@ 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) { @@ -747,7 +761,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; @@ -799,7 +813,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); }