From 046e273a7eb270f4b275c02546e4547d8cfc2a82 Mon Sep 17 00:00:00 2001 From: Daniel Walter Date: Thu, 10 Mar 2016 11:49:48 +0100 Subject: [PATCH 1/5] simplify smprintf by using vasprintf --- config.mk | 2 +- slstatus.c | 17 +++-------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/config.mk b/config.mk index 75ba482..4888003 100644 --- a/config.mk +++ b/config.mk @@ -15,7 +15,7 @@ INCS = -I. -I/usr/include -I${X11INC} LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound # flags -CPPFLAGS = -DVERSION=\"${VERSION}\" +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} #CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} LDFLAGS = -g ${LIBS} diff --git a/slstatus.c b/slstatus.c index 42e5751..214f667 100644 --- a/slstatus.c +++ b/slstatus.c @@ -40,21 +40,10 @@ char * smprintf(char *fmt, ...) { va_list fmtargs; - char *ret; - int len; - + char *ret = NULL; va_start(fmtargs, fmt); - len = vsnprintf(NULL, 0, fmt, fmtargs); - va_end(fmtargs); - - ret = malloc(++len); - if (ret == NULL) { - fprintf(stderr, "Malloc error."); - exit(1); - } - - va_start(fmtargs, fmt); - vsnprintf(ret, len, fmt, fmtargs); + if (vasprintf(&ret, fmt, fmtargs) < 0) + return NULL; va_end(fmtargs); return ret; From 410ba38a0160da8cbcc81dddd704ae9c7f6d15e2 Mon Sep 17 00:00:00 2001 From: Daniel Walter Date: Thu, 10 Mar 2016 11:55:12 +0100 Subject: [PATCH 2/5] make config.h target depending on config.def.h --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e1bb370..d20ad8f 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ options: ${OBJ}: config.h config.mk -config.h: +config.h: config.def.h @echo creating $@ from config.def.h @cp config.def.h $@ From 5295629c4fe987ee51fcfdd380c86e3e0d5de5b5 Mon Sep 17 00:00:00 2001 From: Daniel Walter Date: Thu, 10 Mar 2016 11:55:42 +0100 Subject: [PATCH 3/5] add update_interval add a short sleep to reduce load --- config.def.h | 3 +++ slstatus.c | 1 + 2 files changed, 4 insertions(+) diff --git a/config.def.h b/config.def.h index a6bba6f..afd0320 100644 --- a/config.def.h +++ b/config.def.h @@ -17,6 +17,9 @@ static const char batteryfullfile[] = "/sys/class/power_supply/BAT0/energy_full_ /* time */ static const char timeformat[] = "%y-%m-%d %H:%M:%S"; +/* bar update interval in seconds */ +static unsigned int update_interval = 10; + /* statusbar Possible arguments: - battery (battery percentage) diff --git a/slstatus.c b/slstatus.c index 214f667..658bc79 100644 --- a/slstatus.c +++ b/slstatus.c @@ -343,6 +343,7 @@ main() free(ram_usage); free(volume); free(wifi_signal); + sleep(update_interval); } /* close display */ From b51721c65a8ec2067c599c076d2db6b4a29f567a Mon Sep 17 00:00:00 2001 From: Daniel Walter Date: Thu, 10 Mar 2016 12:11:46 +0100 Subject: [PATCH 4/5] add config checks on startup for better error handling --- slstatus.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/slstatus.c b/slstatus.c index 658bc79..369c6ff 100644 --- a/slstatus.c +++ b/slstatus.c @@ -2,10 +2,13 @@ /* global libraries */ #include +#include #include #include #include #include +#include +#include #include #include #include @@ -15,6 +18,7 @@ /* functions */ void setstatus(char *str); +int config_check(); char *smprintf(char *fmt, ...); char *get_battery(); char *get_cpu_temperature(); @@ -49,6 +53,22 @@ smprintf(char *fmt, ...) return ret; } +#define CHECK_FILE(X,Y) do { \ + if (stat(X,&Y) < 0) return -1; \ + if (!S_ISREG(Y.st_mode)) return -1; \ +} while (0); + +/* check configured paths */ +int +config_check() +{ + struct stat fs; + CHECK_FILE(batterynowfile, fs); + CHECK_FILE(batteryfullfile, fs); + CHECK_FILE(tempfile, fs); + return 0; +} + /* battery percentage */ char * get_battery() @@ -314,6 +334,11 @@ main() char *volume = NULL; char *wifi_signal = NULL; + /* check config for sanity */ + if (config_check() < 0) { + fprintf(stderr, "Config error, please check paths and recompile\n"); + exit(1); + } /* open display */ if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); From 50219004d6a631b156be04e4ce4b235622a6db93 Mon Sep 17 00:00:00 2001 From: Daniel Walter Date: Thu, 10 Mar 2016 13:09:24 +0100 Subject: [PATCH 5/5] return n/a instead of exiting if a specific value cannot be read --- slstatus.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/slstatus.c b/slstatus.c index 369c6ff..3297e1d 100644 --- a/slstatus.c +++ b/slstatus.c @@ -79,7 +79,7 @@ get_battery() /* open battery now file */ if (!(fp = fopen(batterynowfile, "r"))) { fprintf(stderr, "Error opening battery file."); - exit(1); + return smprintf("n/a"); } /* read value */ @@ -91,7 +91,7 @@ get_battery() /* open battery full file */ if (!(fp = fopen(batteryfullfile, "r"))) { fprintf(stderr, "Error opening battery file."); - exit(1); + return smprintf("n/a"); } /* read value */ @@ -117,7 +117,7 @@ get_cpu_temperature() /* open temperature file */ if (!(fp = fopen(tempfile, "r"))) { fprintf(stderr, "Could not open temperature file.\n"); - exit(1); + return smprintf("n/a"); } /* extract temperature */ @@ -141,7 +141,7 @@ get_cpu_usage() /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file."); - exit(1); + return smprintf("n/a"); } /* read values */ @@ -156,7 +156,7 @@ get_cpu_usage() /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file."); - exit(1); + return smprintf("n/a"); } /* read values */ @@ -183,8 +183,8 @@ get_datetime() /* get time in format */ time(&tm); if(!strftime(buf, bufsize, timeformat, localtime(&tm))) { - fprintf(stderr, "Strftime failed.\n"); - exit(1); + fprintf(stderr, "Strftime failed.\n"); + return smprintf("n/a"); } /* return time */ @@ -202,7 +202,7 @@ get_ram_usage() /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file."); - exit(1); + return smprintf("n/a"); } /* read the values */ @@ -283,7 +283,7 @@ get_wifi_signal() /* open wifi file */ if(!(fp = fopen(path, "r"))) { fprintf(stderr, "Error opening wifi operstate file."); - exit(1); + return smprintf("n/a"); } /* read the status */ @@ -294,13 +294,13 @@ get_wifi_signal() /* check if interface down */ if(strcmp(status, "up\n") != 0){ - return "n/a"; + return smprintf("n/a"); } /* open wifi file */ if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "Error opening wireless file."); - exit(1); + return smprintf("n/a"); } /* extract the signal strength */