Merge pull request #6 from sahne/master
minor cleanups and sanity checks
This commit is contained in:
commit
e046ee18f9
4 changed files with 45 additions and 27 deletions
2
Makefile
2
Makefile
|
@ -19,7 +19,7 @@ options:
|
||||||
|
|
||||||
${OBJ}: config.h config.mk
|
${OBJ}: config.h config.mk
|
||||||
|
|
||||||
config.h:
|
config.h: config.def.h
|
||||||
@echo creating $@ from config.def.h
|
@echo creating $@ from config.def.h
|
||||||
@cp config.def.h $@
|
@cp config.def.h $@
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,9 @@ static const char batteryfullfile[] = "/sys/class/power_supply/BAT0/energy_full_
|
||||||
/* time */
|
/* time */
|
||||||
static const char timeformat[] = "%y-%m-%d %H:%M:%S";
|
static const char timeformat[] = "%y-%m-%d %H:%M:%S";
|
||||||
|
|
||||||
|
/* bar update interval in seconds */
|
||||||
|
static unsigned int update_interval = 10;
|
||||||
|
|
||||||
/* statusbar
|
/* statusbar
|
||||||
Possible arguments:
|
Possible arguments:
|
||||||
- battery (battery percentage)
|
- battery (battery percentage)
|
||||||
|
|
|
@ -15,7 +15,7 @@ INCS = -I. -I/usr/include -I${X11INC}
|
||||||
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound
|
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound
|
||||||
|
|
||||||
# flags
|
# flags
|
||||||
CPPFLAGS = -DVERSION=\"${VERSION}\"
|
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
|
||||||
CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
|
CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
|
||||||
#CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
#CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
||||||
LDFLAGS = -g ${LIBS}
|
LDFLAGS = -g ${LIBS}
|
||||||
|
|
63
slstatus.c
63
slstatus.c
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
/* global libraries */
|
/* global libraries */
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
|
@ -15,6 +18,7 @@
|
||||||
|
|
||||||
/* functions */
|
/* functions */
|
||||||
void setstatus(char *str);
|
void setstatus(char *str);
|
||||||
|
int config_check();
|
||||||
char *smprintf(char *fmt, ...);
|
char *smprintf(char *fmt, ...);
|
||||||
char *get_battery();
|
char *get_battery();
|
||||||
char *get_cpu_temperature();
|
char *get_cpu_temperature();
|
||||||
|
@ -40,26 +44,31 @@ char *
|
||||||
smprintf(char *fmt, ...)
|
smprintf(char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list fmtargs;
|
va_list fmtargs;
|
||||||
char *ret;
|
char *ret = NULL;
|
||||||
int len;
|
|
||||||
|
|
||||||
va_start(fmtargs, fmt);
|
va_start(fmtargs, fmt);
|
||||||
len = vsnprintf(NULL, 0, fmt, fmtargs);
|
if (vasprintf(&ret, fmt, fmtargs) < 0)
|
||||||
va_end(fmtargs);
|
return NULL;
|
||||||
|
|
||||||
ret = malloc(++len);
|
|
||||||
if (ret == NULL) {
|
|
||||||
fprintf(stderr, "Malloc error.");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
va_start(fmtargs, fmt);
|
|
||||||
vsnprintf(ret, len, fmt, fmtargs);
|
|
||||||
va_end(fmtargs);
|
va_end(fmtargs);
|
||||||
|
|
||||||
return ret;
|
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 */
|
/* battery percentage */
|
||||||
char *
|
char *
|
||||||
get_battery()
|
get_battery()
|
||||||
|
@ -70,7 +79,7 @@ get_battery()
|
||||||
/* open battery now file */
|
/* open battery now file */
|
||||||
if (!(fp = fopen(batterynowfile, "r"))) {
|
if (!(fp = fopen(batterynowfile, "r"))) {
|
||||||
fprintf(stderr, "Error opening battery file.");
|
fprintf(stderr, "Error opening battery file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read value */
|
/* read value */
|
||||||
|
@ -82,7 +91,7 @@ get_battery()
|
||||||
/* open battery full file */
|
/* open battery full file */
|
||||||
if (!(fp = fopen(batteryfullfile, "r"))) {
|
if (!(fp = fopen(batteryfullfile, "r"))) {
|
||||||
fprintf(stderr, "Error opening battery file.");
|
fprintf(stderr, "Error opening battery file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read value */
|
/* read value */
|
||||||
|
@ -108,7 +117,7 @@ get_cpu_temperature()
|
||||||
/* open temperature file */
|
/* open temperature file */
|
||||||
if (!(fp = fopen(tempfile, "r"))) {
|
if (!(fp = fopen(tempfile, "r"))) {
|
||||||
fprintf(stderr, "Could not open temperature file.\n");
|
fprintf(stderr, "Could not open temperature file.\n");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* extract temperature */
|
/* extract temperature */
|
||||||
|
@ -132,7 +141,7 @@ get_cpu_usage()
|
||||||
/* open stat file */
|
/* open stat file */
|
||||||
if (!(fp = fopen("/proc/stat","r"))) {
|
if (!(fp = fopen("/proc/stat","r"))) {
|
||||||
fprintf(stderr, "Error opening stat file.");
|
fprintf(stderr, "Error opening stat file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read values */
|
/* read values */
|
||||||
|
@ -147,7 +156,7 @@ get_cpu_usage()
|
||||||
/* open stat file */
|
/* open stat file */
|
||||||
if (!(fp = fopen("/proc/stat","r"))) {
|
if (!(fp = fopen("/proc/stat","r"))) {
|
||||||
fprintf(stderr, "Error opening stat file.");
|
fprintf(stderr, "Error opening stat file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read values */
|
/* read values */
|
||||||
|
@ -175,7 +184,7 @@ get_datetime()
|
||||||
time(&tm);
|
time(&tm);
|
||||||
if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
|
if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
|
||||||
fprintf(stderr, "Strftime failed.\n");
|
fprintf(stderr, "Strftime failed.\n");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return time */
|
/* return time */
|
||||||
|
@ -193,7 +202,7 @@ get_ram_usage()
|
||||||
/* open meminfo file */
|
/* open meminfo file */
|
||||||
if (!(fp = fopen("/proc/meminfo", "r"))) {
|
if (!(fp = fopen("/proc/meminfo", "r"))) {
|
||||||
fprintf(stderr, "Error opening meminfo file.");
|
fprintf(stderr, "Error opening meminfo file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read the values */
|
/* read the values */
|
||||||
|
@ -274,7 +283,7 @@ get_wifi_signal()
|
||||||
/* open wifi file */
|
/* open wifi file */
|
||||||
if(!(fp = fopen(path, "r"))) {
|
if(!(fp = fopen(path, "r"))) {
|
||||||
fprintf(stderr, "Error opening wifi operstate file.");
|
fprintf(stderr, "Error opening wifi operstate file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read the status */
|
/* read the status */
|
||||||
|
@ -285,13 +294,13 @@ get_wifi_signal()
|
||||||
|
|
||||||
/* check if interface down */
|
/* check if interface down */
|
||||||
if(strcmp(status, "up\n") != 0){
|
if(strcmp(status, "up\n") != 0){
|
||||||
return "n/a";
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* open wifi file */
|
/* open wifi file */
|
||||||
if (!(fp = fopen("/proc/net/wireless", "r"))) {
|
if (!(fp = fopen("/proc/net/wireless", "r"))) {
|
||||||
fprintf(stderr, "Error opening wireless file.");
|
fprintf(stderr, "Error opening wireless file.");
|
||||||
exit(1);
|
return smprintf("n/a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* extract the signal strength */
|
/* extract the signal strength */
|
||||||
|
@ -325,6 +334,11 @@ main()
|
||||||
char *volume = NULL;
|
char *volume = NULL;
|
||||||
char *wifi_signal = 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 */
|
/* open display */
|
||||||
if (!(dpy = XOpenDisplay(0x0))) {
|
if (!(dpy = XOpenDisplay(0x0))) {
|
||||||
fprintf(stderr, "Cannot open display!\n");
|
fprintf(stderr, "Cannot open display!\n");
|
||||||
|
@ -354,6 +368,7 @@ main()
|
||||||
free(ram_usage);
|
free(ram_usage);
|
||||||
free(volume);
|
free(volume);
|
||||||
free(wifi_signal);
|
free(wifi_signal);
|
||||||
|
sleep(update_interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* close display */
|
/* close display */
|
||||||
|
|
Loading…
Reference in a new issue