X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/cd3c084957483c4192edf2e8feb4c759668a1055..b650c438f0dc049d98e339e66eeffd7650774a5d:/slstatus.c?ds=inline diff --git a/slstatus.c b/slstatus.c index 5246bc0..2e57fbb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,12 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include -#include #include #include #include #include -#include #include #include #include @@ -29,14 +27,12 @@ #include "strlcat.h" #include "strlcpy.h" -typedef char *(*op_fun)(); struct arg { - op_fun func; + char *(*func)(); const char *format; const char *args; }; -static void setstatus(const char *); static char *smprintf(const char *, ...); static char *battery_perc(const char *); static char *cpu_perc(void); @@ -67,14 +63,6 @@ static Display *dpy; #include "config.h" -static void -setstatus(const char *str) -{ - /* set WM_NAME via X11 */ - XStoreName(dpy, DefaultRootWindow(dpy), str); - XSync(dpy, False); -} - static char * smprintf(const char *fmt, ...) { @@ -252,7 +240,8 @@ entropy(void) FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open entropy file.\n"); + fprintf(stderr, "Could not open entropy file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -264,8 +253,7 @@ entropy(void) static char * gid(void) { - gid_t gid = getgid(); - return smprintf("%d", gid); + return smprintf("%d", getgid()); } static char * @@ -476,45 +464,31 @@ uptime(void) static char * username(void) { - register struct passwd *pw; - register uid_t uid; - - uid = geteuid(); - pw = getpwuid(uid); + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); - if (pw) + if (pw == NULL) return smprintf("%s", pw->pw_name); - else { - fprintf(stderr, "Could not get username: %s\n", - strerror(errno)); - return smprintf(UNKNOWN_STR); - } + fprintf(stderr, "Could not get username: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } static char * uid(void) { - /* FIXME: WHY USE register modifier? */ - register uid_t uid; - - uid = geteuid(); - - if (uid) - return smprintf("%d", uid); - else { - fprintf(stderr, "Could not get uid.\n"); - return smprintf(UNKNOWN_STR); - } - - return smprintf(UNKNOWN_STR); + return smprintf("%d", geteuid()); } static char * vol_perc(const char *soundcard) { + /* + * TODO: FIXME: + * https://github.com/drkh5h/slstatus/issues/12 + */ int mute = 0; long vol = 0, max = 0, min = 0; snd_mixer_t *handle; @@ -563,12 +537,14 @@ wifi_perc(const char *wificard) char path[64]; char status[5]; char needle[strlen(wificard)+2]; - FILE *fp = fopen(path, "r"); + FILE *fp; strlcpy(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); + fp = fopen(path, "r"); + if(fp == NULL) { fprintf(stderr, "Error opening wifi operstate file: %s\n", strerror(errno)); @@ -640,10 +616,6 @@ main(void) struct arg argument; dpy = XOpenDisplay(0x0); - if (!dpy) { - fprintf(stderr, "Cannot open display!\n"); - exit(1); - } for (;;) { memset(status_string, 0, sizeof(status_string)); @@ -663,10 +635,16 @@ main(void) free(element); } - setstatus(status_string); + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); sleep(UPDATE_INTERVAL -1); } + /* NOT REACHED */ + /* + * TODO: find out a way to exit successfully + * to prevent memory leaks + */ XCloseDisplay(dpy); return 0; }