X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/0aefd57f9f469aba25ffbe9bdda04e894b9d739a..1654efe1d67d0bec010792e8c6eb64a0f8e88a58:/slstatus.c?ds=sidebyside diff --git a/slstatus.c b/slstatus.c index 163b44e..5c83a84 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 @@ -26,17 +24,22 @@ #undef strlcat #undef strlcpy +#include "arg.h" #include "strlcat.h" #include "strlcpy.h" +#include "concat.h" + +char *argv0; +char concat[]; + +FILE *foutput; -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 +70,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, ...) { @@ -105,7 +100,7 @@ battery_perc(const char *battery) int now, full, perc; char batterynowfile[64]; char batteryfullfile[64]; - FILE *fp = fopen(batterynowfile, "r"); + FILE *fp; strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); @@ -117,8 +112,9 @@ battery_perc(const char *battery) strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); + fp = fopen(batterynowfile, "r"); if (fp == NULL ) { - fprintf(stderr, "Error opening battery file: %s: %s\n", + fprintf(foutput, "Error opening battery file: %s: %s\n", batterynowfile, strerror(errno)); return smprintf(UNKNOWN_STR); @@ -129,7 +125,7 @@ battery_perc(const char *battery) fp = fopen(batteryfullfile, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s\n", + fprintf(foutput, "Error opening battery file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -150,7 +146,7 @@ cpu_perc(void) FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -158,12 +154,11 @@ 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"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -193,7 +188,7 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -207,7 +202,7 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -222,7 +217,7 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -236,7 +231,7 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -251,7 +246,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(foutput, "Could not open entropy file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -263,8 +259,7 @@ entropy(void) static char * gid(void) { - gid_t gid = getgid(); - return smprintf("%d", gid); + return smprintf("%d", getgid()); } static char * @@ -274,7 +269,7 @@ hostname(void) FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open hostname file: %s\n", + fprintf(foutput, "Could not open hostname file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -295,7 +290,7 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address: %s\n", + fprintf(foutput, "Error getting IP address: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -310,7 +305,7 @@ ip(const char *interface) if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - fprintf(stderr, "Error getting IP address.\n"); + fprintf(foutput, "Error getting IP address.\n"); return smprintf(UNKNOWN_STR); } return smprintf("%s", host); @@ -329,7 +324,7 @@ load_avg(void) double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(stderr, "Error getting load avg.\n"); + fprintf(foutput, "Error getting load avg.\n"); return smprintf(UNKNOWN_STR); } @@ -343,7 +338,7 @@ ram_free(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -361,7 +356,7 @@ ram_perc(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -383,7 +378,7 @@ ram_total(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -400,7 +395,7 @@ ram_used(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -423,7 +418,7 @@ run_command(const char* command) char buffer[64]; if (fp == NULL) { - fprintf(stderr, "Could not get command output for: %s: %s\n", + fprintf(foutput, "Could not get command output for: %s: %s\n", command, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -448,7 +443,7 @@ temp(const char *file) FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(stderr, "Could not open temperature file: %s\n", + fprintf(foutput, "Could not open temperature file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -475,45 +470,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(foutput, "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; @@ -528,7 +509,7 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + fprintf(foutput, "Could not get alsa volume.\n"); return smprintf(UNKNOWN_STR); } snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); @@ -562,14 +543,16 @@ 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", + fprintf(foutput, "Error opening wifi operstate file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -581,7 +564,7 @@ wifi_perc(const char *wificard) fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening wireless file: %s\n", + fprintf(foutput, "Error opening wireless file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -613,13 +596,13 @@ wifi_essid(const char *wificard) wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(stderr, "Cannot open socket for interface: %s: %s\n", + fprintf(foutput, "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(stderr, "Get ESSID ioctl failed for interface %s: %s\n", + fprintf(foutput, "Get ESSID ioctl failed for interface %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -630,20 +613,57 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } +void +usage(void) +{ + fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); + exit(0); +} + int -main(void) +main(int argc, char *argv[]) { + unsigned short int dflag, lflag; size_t i; - char status_string[4096]; + char status_string[4096], logpath[4096]; char *res, *element; struct arg argument; + foutput = stderr; dpy = XOpenDisplay(0x0); - if (!dpy) { - fprintf(stderr, "Cannot open display!\n"); - exit(1); + + 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 + + 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); + } } + if (dflag) + daemon(0, 0); + for (;;) { memset(status_string, 0, sizeof(status_string)); for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { @@ -662,10 +682,15 @@ main(void) free(element); } - setstatus(status_string); - sleep(UPDATE_INTERVAL -1); + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); } + /* NOT REACHED */ + /* + * TODO: find out a way to exit successfully + * to prevent memory leaks + */ XCloseDisplay(dpy); return 0; }