added a tool for resetting the status bar && worked around some issues && removed the makefile (we need a better one)
@@ -1,53 +0,0 @@
-# See LICENSE file for copyright and license details. - -include config.mk - -SRC = ${NAME}.c -OBJ = ${SRC:.c=.o} - -all: options ${NAME} - -options: - @echo ${NAME} build options: - @echo "CFLAGS = ${CFLAGS}" - @echo "LDFLAGS = ${LDFLAGS}" - @echo "CC = ${CC}" - -.c.o: - @echo CC $< - @${CC} -c ${CFLAGS} $< - -${OBJ}: config.h config.mk - -config.h: config.def.h - @echo creating $@ from config.def.h - @cp config.def.h $@ - -${NAME}: ${OBJ} - @echo CC -o $@ - @${CC} -o $@ ${OBJ} ${LDFLAGS} - -clean: - @echo cleaning - @rm -f ${NAME} ${OBJ} ${NAME}-${VERSION}.tar.gz - -dist: clean - @echo creating dist tarball - @mkdir -p ${NAME}-${VERSION} - @cp -R Makefile config.mk LICENSE \ - ${SRC} ${NAME}-${VERSION} - @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} - @gzip ${NAME}-${VERSION}.tar - @rm -rf ${NAME}-${VERSION} - -install: all - @echo installing executable file to ${DESTDIR}${PREFIX}/bin - @mkdir -p ${DESTDIR}${PREFIX}/bin - @cp -f ${NAME} ${DESTDIR}${PREFIX}/bin - @chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} - -uninstall: - @echo removing executable file from ${DESTDIR}${PREFIX}/bin - @rm -f ${DESTDIR}${PREFIX}/bin/${NAME} - -.PHONY: all options clean dist install uninstall
@@ -1,22 +0,0 @@
-NAME = slstatus -VERSION = 1.0 - -# Customize below to fit your system - -PREFIX = /usr/local - -X11INC = /usr/X11R6/include -X11LIB = /usr/X11R6/lib - -# includes and libs -INCS = -I. -I/usr/include -I${X11INC} -LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound - -# flags -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE -CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS} -LDFLAGS = ${LIBS} - -# compiler and linker -CC = cc -
@@ -0,0 +1,17 @@
+NAME="slstatus" +VERSION="1.0" + +PREFIX="/usr/local" + +X11INC="/usr/X11R6/include" +X11LIB="/usr/X11R6/lib" + +INCS="-I. -I/usr/include -I${X11INC}" +LIBS="-L/usr/lib -lc -L${X11LIB} -lX11 -lasound" + +CPPFLAGS="-DVERSION=\"${VERSION}\" -D_GNU_SOURCE" +CFLAGS="-std=c99 -pedantic -Wno-unused-function -Wall -Wextra -O0 ${INCS} ${CPPFLAGS}" +LDFLAGS="${LIBS}" + +CC = cc +
@@ -24,15 +24,8 @@
#undef strlcat #undef strlcpy -#include "arg.h" #include "strlcat.h" #include "strlcpy.h" -#include "concat.h" - -char *argv0; -char concat[]; - -FILE *foutput; struct arg { char *(*func)();@@ -114,7 +107,7 @@ strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile));
fp = fopen(batterynowfile, "r"); if (fp == NULL ) { - fprintf(foutput, "Error opening battery file: %s: %s\n", + fprintf(stderr, "Error opening battery file: %s: %s\n", batterynowfile, strerror(errno)); return smprintf(UNKNOWN_STR);@@ -125,7 +118,7 @@ fclose(fp);
fp = fopen(batteryfullfile, "r"); if (fp == NULL) { - fprintf(foutput, "Error opening battery file: %s\n", + fprintf(stderr, "Error opening battery file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -146,7 +139,7 @@ long double a[4], b[4];
FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(foutput, "Error opening stat file: %s\n", + fprintf(stderr, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -158,7 +151,7 @@ sleep(1);
fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(foutput, "Error opening stat file: %s\n", + fprintf(stderr, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -188,7 +181,7 @@ {
struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -202,7 +195,7 @@ int perc = 0;
struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -217,7 +210,7 @@ {
struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -231,7 +224,7 @@ {
struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(foutput, "Could not get filesystem info: %s\n", + fprintf(stderr, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -246,7 +239,7 @@ int entropy = 0;
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(foutput, "Could not open entropy file: %s\n", + fprintf(stderr, "Could not open entropy file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -269,7 +262,7 @@ char hostname[HOST_NAME_MAX];
FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(foutput, "Could not open hostname file: %s\n", + fprintf(stderr, "Could not open hostname file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -290,7 +283,7 @@ int s;
char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(foutput, "Error getting IP address: %s\n", + fprintf(stderr, "Error getting IP address: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -305,7 +298,7 @@ NULL, 0, NI_NUMERICHOST);
if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - fprintf(foutput, "Error getting IP address.\n"); + fprintf(stderr, "Error getting IP address.\n"); return smprintf(UNKNOWN_STR); } return smprintf("%s", host);@@ -324,7 +317,7 @@ {
double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(foutput, "Error getting load avg.\n"); + fprintf(stderr, "Error getting load avg.\n"); return smprintf(UNKNOWN_STR); }@@ -338,7 +331,7 @@ long free;
FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -356,7 +349,7 @@ long total, free, buffers, cached;
FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -378,7 +371,7 @@ long total;
FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -395,7 +388,7 @@ long free, total, buffers, cached, used;
FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening meminfo file: %s\n", + fprintf(stderr, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -418,7 +411,7 @@ FILE *fp = popen(command, "r");
char buffer[64]; if (fp == NULL) { - fprintf(foutput, "Could not get command output for: %s: %s\n", + fprintf(stderr, "Could not get command output for: %s: %s\n", command, strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -443,7 +436,7 @@ int temperature;
FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(foutput, "Could not open temperature file: %s\n", + fprintf(stderr, "Could not open temperature file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -476,7 +469,7 @@
if (pw == NULL) return smprintf("%s", pw->pw_name); - fprintf(foutput, "Could not get username: %s\n", + fprintf(stderr, "Could not get username: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -509,7 +502,7 @@
snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { - fprintf(foutput, "Could not get alsa volume.\n"); + fprintf(stderr, "Could not get alsa volume.\n"); return smprintf(UNKNOWN_STR); } snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL);@@ -552,7 +545,7 @@
fp = fopen(path, "r"); if(fp == NULL) { - fprintf(foutput, "Error opening wifi operstate file: %s\n", + fprintf(stderr, "Error opening wifi operstate file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -564,7 +557,7 @@ return smprintf(UNKNOWN_STR);
fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(foutput, "Error opening wireless file: %s\n", + fprintf(stderr, "Error opening wireless file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -596,13 +589,13 @@ memset(&wreq, 0, sizeof(struct iwreq));
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(foutput, "Cannot open socket for interface: %s: %s\n", + fprintf(stderr, "Cannot open socket for interface: %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(foutput, "Get ESSID ioctl failed for interface %s: %s\n", + fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); }@@ -611,86 +604,39 @@ if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
return smprintf(UNKNOWN_STR); else return smprintf("%s", (char *)wreq.u.essid.pointer); -} - -void -usage(void) -{ - fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); - exit(0); } int -main(int argc, char *argv[]) +main(void) { - unsigned short int dflag, lflag; size_t i; - char status_string[4096], logpath[4096]; + char status_string[4096]; char *res, *element; struct arg argument; - foutput = stderr; - dpy = XOpenDisplay(0x0); - - ARGBEGIN { - case 'd': - dflag = 1; - break; - case 'l': - strlcpy(logpath, EARGF(usage()), sizeof(logpath)-1); - logpath[strlen(logpath)+1] = '\0'; - foutput = fopen(logpath, "a"); - if (foutput == NULL) { - fprintf(stderr, "failed to open log file at %s: %s\n", - logpath, strerror(errno)); - return (1); - } - lflag = 1; - break; - default: - usage(); - } ARGEND + stderr = stderr; + dpy = XOpenDisplay(NULL); - if (dflag && !lflag) { - ccat(2, getenv("HOME"), "/.slstatus_log"); - foutput = fopen(concat, "a"); - if (foutput == NULL) { - fprintf(stderr, "failed to open log file at %s: %s\n", - logpath, strerror(errno)); - return (1); + memset(status_string, 0, sizeof(status_string)); + for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { + argument = args[i]; + if (argument.args == NULL) + res = argument.func(); + else + res = argument.func(argument.args); + element = smprintf(argument.format, res); + if (element == NULL) { + element = smprintf(UNKNOWN_STR); + fprintf(stderr, "Failed to format output.\n"); } - } - - if (dflag) - daemon(0, 0); - - for (;;) { - memset(status_string, 0, sizeof(status_string)); - for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { - argument = args[i]; - if (argument.args == NULL) - res = argument.func(); - else - res = argument.func(argument.args); - element = smprintf(argument.format, res); - if (element == NULL) { - element = smprintf(UNKNOWN_STR); - fprintf(stderr, "Failed to format output.\n"); - } - strlcat(status_string, element, sizeof(status_string)); - free(res); - free(element); - } - - XStoreName(dpy, DefaultRootWindow(dpy), status_string); - XSync(dpy, False); + strlcat(status_string, element, sizeof(status_string)); + free(res); + free(element); } - /* NOT REACHED */ - /* - * TODO: find out a way to exit successfully - * to prevent memory leaks - */ + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); XCloseDisplay(dpy); + return 0; }
@@ -0,0 +1,10 @@
+#include <X11/Xlib.h> + +int +main(void) +{ + Display *dpy = XOpenDisplay(NULL); + XStoreName(dpy, DefaultRootWindow(dpy), NULL); + XCloseDisplay(dpy); + return (0); +}