X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/cd3c084957483c4192edf2e8feb4c759668a1055..9fa858daeafb5398cdf19af9483b2854fe6e9772:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 5246bc0..dae5e8a 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, ...) { @@ -159,7 +147,6 @@ cpu_perc(void) fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); fclose(fp); - /* wait a second (for avg values) */ sleep(1); fp = fopen("/proc/stat","r"); @@ -252,7 +239,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 +252,7 @@ entropy(void) static char * gid(void) { - gid_t gid = getgid(); - return smprintf("%d", gid); + return smprintf("%d", getgid()); } static char * @@ -476,45 +463,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 +536,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)); @@ -639,34 +614,29 @@ main(void) char *res, *element; struct arg argument; - dpy = XOpenDisplay(0x0); - if (!dpy) { - fprintf(stderr, "Cannot open display!\n"); - exit(1); - } - - 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); + stderr = stderr; + dpy = XOpenDisplay(NULL); + + 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"); } - - setstatus(status_string); - sleep(UPDATE_INTERVAL -1); + strlcat(status_string, element, sizeof(status_string)); + free(res); + free(element); } + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); XCloseDisplay(dpy); + return 0; }