Xinqi Bao's Git

added a tool for resetting the status bar && worked around some issues && removed...
[slstatus.git] / slstatus.c
index 7bd3b8c..dae5e8a 100644 (file)
@@ -1,12 +1,10 @@
 /* See LICENSE file for copyright and license details. */
 
 #include <alsa/asoundlib.h>
-#include <arpa/inet.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
 #include <limits.h>
 #include <linux/wireless.h>
-#include <locale.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <stdarg.h>
 #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,24 +63,26 @@ 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, ...)
 {
-       va_list fmtargs;
-       char *ret = NULL;
+       va_list ap;
+       char *ret;
+       int len;
 
-       va_start(fmtargs, fmt);
-       if (vasprintf(&ret, fmt, fmtargs) < 0)
-               return NULL;
-       va_end(fmtargs);
+       va_start(ap, fmt);
+       len = vsnprintf(NULL, 0, fmt, ap);
+       va_end(ap);
+
+       ret = malloc(++len);
+       if (ret == NULL) {
+               perror("malloc");
+               exit(1);
+       }
+
+       va_start(ap, fmt);
+       vsnprintf(ret, len, fmt, ap);
+       va_end(ap);
 
        return ret;
 }
@@ -93,31 +91,36 @@ static char *
 battery_perc(const char *battery)
 {
        int now, full, perc;
-       char batterynowfile[64] = "";
-       char batteryfullfile[64] = "";
+       char batterynowfile[64];
+       char batteryfullfile[64];
        FILE *fp;
 
-       strlcat(batterynowfile, batterypath, sizeof(batterynowfile));
+       strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile));
        strlcat(batterynowfile, battery, sizeof(batterynowfile));
        strlcat(batterynowfile, "/", sizeof(batterynowfile));
-       strlcat(batterynowfile, batterynow, sizeof(batterynowfile));
+       strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile));
 
-       strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile));
+       strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile));
        strlcat(batteryfullfile, battery, sizeof(batteryfullfile));
        strlcat(batteryfullfile, "/", sizeof(batteryfullfile));
-       strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile));
+       strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile));
 
-       if (!(fp = fopen(batterynowfile, "r"))) {
-               fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile);
-               return smprintf(unknowntext);
+       fp = fopen(batterynowfile, "r");
+       if (fp == NULL ) {
+               fprintf(stderr, "Error opening battery file: %s: %s\n",
+                                               batterynowfile,
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%i", &now);
        fclose(fp);
 
-       if (!(fp = fopen(batteryfullfile, "r"))) {
-               fprintf(stderr, "Error opening battery file.\n");
-               return smprintf(unknowntext);
+       fp = fopen(batteryfullfile, "r");
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening battery file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%i", &full);
@@ -133,22 +136,24 @@ cpu_perc(void)
 {
        int perc;
        long double a[4], b[4];
-       FILE *fp;
+       FILE *fp = fopen("/proc/stat","r");
 
-       if (!(fp = fopen("/proc/stat","r"))) {
-               fprintf(stderr, "Error opening stat file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening stat file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        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);
 
-       if (!(fp = fopen("/proc/stat","r"))) {
-               fprintf(stderr, "Error opening stat file.\n");
-               return smprintf(unknowntext);
+       fp = fopen("/proc/stat","r");
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening stat file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
@@ -160,27 +165,14 @@ cpu_perc(void)
 static char *
 datetime(const char *timeformat)
 {
-       time_t tm;
-       size_t bufsize = 64;
-       char *buf = malloc(bufsize);
-       if (buf == NULL) {
-               fprintf(stderr, "Failed to get date/time.\n");
-               return smprintf(unknowntext);
-       }
+       time_t t;
+       char timestr[80];
 
-       time(&tm);
-       setlocale(LC_TIME, "");
-       if (!strftime(buf, bufsize, timeformat, localtime(&tm))) {
-               setlocale(LC_TIME, "C");
-               free(buf);
-               fprintf(stderr, "Strftime failed.\n");
-               return smprintf(unknowntext);
-       }
+       t = time(NULL);
+       if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
+               return smprintf(UNKNOWN_STR);
 
-       setlocale(LC_TIME, "C");
-       char *ret = smprintf("%s", buf);
-       free(buf);
-       return ret;
+       return smprintf("%s", timestr);
 }
 
 static char *
@@ -189,8 +181,9 @@ disk_free(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info.\n");
-               return smprintf(unknowntext);
+               fprintf(stderr, "Could not get filesystem info: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
        return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
 }
@@ -202,8 +195,9 @@ disk_perc(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info.\n");
-               return smprintf(unknowntext);
+               fprintf(stderr, "Could not get filesystem info: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
@@ -216,8 +210,9 @@ disk_total(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info.\n");
-               return smprintf(unknowntext);
+               fprintf(stderr, "Could not get filesystem info: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
@@ -229,8 +224,9 @@ disk_used(const char *mountpoint)
        struct statvfs fs;
 
        if (statvfs(mountpoint, &fs) < 0) {
-               fprintf(stderr, "Could not get filesystem info.\n");
-               return smprintf(unknowntext);
+               fprintf(stderr, "Could not get filesystem info: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
@@ -240,11 +236,12 @@ static char *
 entropy(void)
 {
        int entropy = 0;
-       FILE *fp;
+       FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
 
-       if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) {
-               fprintf(stderr, "Could not open entropy file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Could not open entropy file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%d", &entropy);
@@ -255,22 +252,25 @@ entropy(void)
 static char *
 gid(void)
 {
-       gid_t gid = getgid();
-       return smprintf("%d", gid);
+       return smprintf("%d", getgid());
 }
 
 static char *
 hostname(void)
 {
        char hostname[HOST_NAME_MAX];
-       FILE *fp;
+       FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
 
-       if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) {
-               fprintf(stderr, "Could not open hostname file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Could not open hostname file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
-       fscanf(fp, "%s\n", hostname);
+       fgets(hostname, sizeof(hostname), fp);
+       /* FIXME: needs improvement */
+       memset(&hostname[strlen(hostname)-1], '\0',
+               sizeof(hostname) - strlen(hostname));
        fclose(fp);
        return smprintf("%s", hostname);
 }
@@ -283,8 +283,9 @@ ip(const char *interface)
        char host[NI_MAXHOST];
 
        if (getifaddrs(&ifaddr) == -1) {
-               fprintf(stderr, "Error getting IP address.\n");
-               return smprintf(unknowntext);
+               fprintf(stderr, "Error getting IP address: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        /* get the ip address */
@@ -292,12 +293,13 @@ ip(const char *interface)
                if (ifa->ifa_addr == NULL)
                        continue;
 
-               s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
+               s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
+                                                               NULL, 0, NI_NUMERICHOST);
 
                if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
                        if (s != 0) {
                                fprintf(stderr, "Error getting IP address.\n");
-                               return smprintf(unknowntext);
+                               return smprintf(UNKNOWN_STR);
                        }
                        return smprintf("%s", host);
                }
@@ -306,7 +308,7 @@ ip(const char *interface)
        /* free the address */
        freeifaddrs(ifaddr);
 
-       return smprintf(unknowntext);
+       return smprintf(UNKNOWN_STR);
 }
 
 static char *
@@ -316,7 +318,7 @@ load_avg(void)
 
        if (getloadavg(avgs, 3) < 0) {
                fprintf(stderr, "Error getting load avg.\n");
-               return smprintf(unknowntext);
+               return smprintf(UNKNOWN_STR);
        }
 
        return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
@@ -326,11 +328,12 @@ static char *
 ram_free(void)
 {
        long free;
-       FILE *fp;
+       FILE *fp = fopen("/proc/meminfo", "r");
 
-       if (!(fp = fopen("/proc/meminfo", "r"))) {
-               fprintf(stderr, "Error opening meminfo file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening meminfo file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemFree: %ld kB\n", &free);
@@ -343,11 +346,12 @@ ram_perc(void)
 {
        int perc;
        long total, free, buffers, cached;
-       FILE *fp;
+       FILE *fp = fopen("/proc/meminfo", "r");
 
-       if (!(fp = fopen("/proc/meminfo", "r"))) {
-               fprintf(stderr, "Error opening meminfo file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening meminfo file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
@@ -364,11 +368,12 @@ static char *
 ram_total(void)
 {
        long total;
-       FILE *fp;
+       FILE *fp = fopen("/proc/meminfo", "r");
 
-       if (!(fp = fopen("/proc/meminfo", "r"))) {
-               fprintf(stderr, "Error opening meminfo file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening meminfo file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
@@ -380,11 +385,12 @@ static char *
 ram_used(void)
 {
        long free, total, buffers, cached, used;
-       FILE *fp;
+       FILE *fp = fopen("/proc/meminfo", "r");
 
-       if (!(fp = fopen("/proc/meminfo", "r"))) {
-               fprintf(stderr, "Error opening meminfo file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening meminfo file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
@@ -401,15 +407,16 @@ static char *
 run_command(const char* command)
 {
        int good;
-       FILE *fp;
+       FILE *fp = popen(command, "r");
        char buffer[64];
 
-       if (!(fp = popen(command, "r"))) {
-               fprintf(stderr, "Could not get command output for: %s.\n", command);
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Could not get command output for: %s: %s\n",
+                                               command, strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
-       fgets(buffer, sizeof(buffer) - 1, fp);
+       fgets(buffer, sizeof(buffer)-1, fp);
        pclose(fp);
        for (int i = 0 ; i != sizeof(buffer); i++) {
                if (buffer[i] == '\0') {
@@ -418,7 +425,7 @@ run_command(const char* command)
                }
        }
        if (good)
-               buffer[strlen(buffer) - 1] = '\0';
+               buffer[strlen(buffer)-1] = '\0';
        return smprintf("%s", buffer);
 }
 
@@ -426,11 +433,12 @@ static char *
 temp(const char *file)
 {
        int temperature;
-       FILE *fp;
+       FILE *fp = fopen(file, "r");
 
-       if (!(fp = fopen(file, "r"))) {
-               fprintf(stderr, "Could not open temperature file.\n");
-               return smprintf(unknowntext);
+       if (fp == NULL) {
+               fprintf(stderr, "Could not open temperature file: %s\n",
+                                                       strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fscanf(fp, "%d", &temperature);
@@ -455,43 +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.\n");
-               return smprintf(unknowntext);
-       }
 
-       return smprintf(unknowntext);
+       fprintf(stderr, "Could not get username: %s\n",
+                                       strerror(errno));
+       return smprintf(UNKNOWN_STR);
 }
 
 static char *
 uid(void)
 {
-       register uid_t uid;
-
-       uid = geteuid();
-
-       if (uid)
-               return smprintf("%d", uid);
-       else {
-               fprintf(stderr, "Could not get uid.\n");
-               return smprintf(unknowntext);
-       }
-
-       return smprintf(unknowntext);
+       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;
@@ -507,10 +503,10 @@ vol_perc(const char *soundcard)
        snd_mixer_selem_id_malloc(&mute_info);
        if (vol_info == NULL || mute_info == NULL) {
                fprintf(stderr, "Could not get alsa volume.\n");
-               return smprintf(unknowntext);
+               return smprintf(UNKNOWN_STR);
        }
-       snd_mixer_selem_id_set_name(vol_info, channel);
-       snd_mixer_selem_id_set_name(mute_info, channel);
+       snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL);
+       snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL);
        pcm_mixer = snd_mixer_find_selem(handle, vol_info);
        mas_mixer = snd_mixer_find_selem(handle, mute_info);
 
@@ -534,41 +530,46 @@ vol_perc(const char *soundcard)
 static char *
 wifi_perc(const char *wificard)
 {
-       int bufsize = 255;
        int strength;
-       char buf[bufsize];
+       char buf[255];
        char *datastart;
        char path[64];
        char status[5];
-       char needle[sizeof wificard + 1];
+       char needle[strlen(wificard)+2];
        FILE *fp;
 
-       memset(path, 0, sizeof path);
-       strlcat(path, "/sys/class/net/", sizeof(path));
+       strlcpy(path, "/sys/class/net/", sizeof(path));
        strlcat(path, wificard, sizeof(path));
        strlcat(path, "/operstate", sizeof(path));
 
-       if(!(fp = fopen(path, "r"))) {
-               fprintf(stderr, "Error opening wifi operstate file.\n");
-               return smprintf(unknowntext);
+       fp = fopen(path, "r");
+
+       if(fp == NULL) {
+               fprintf(stderr, "Error opening wifi operstate file: %s\n",
+                                                       strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        fgets(status, 5, fp);
        fclose(fp);
        if(strcmp(status, "up\n") != 0)
-               return smprintf(unknowntext);
+               return smprintf(UNKNOWN_STR);
 
-       if (!(fp = fopen("/proc/net/wireless", "r"))) {
-               fprintf(stderr, "Error opening wireless file.\n");
-               return smprintf(unknowntext);
+       fp = fopen("/proc/net/wireless", "r");
+       if (fp == NULL) {
+               fprintf(stderr, "Error opening wireless file: %s\n",
+                                               strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        strlcpy(needle, wificard, sizeof(needle));
        strlcat(needle, ":", sizeof(needle));
-       fgets(buf, bufsize, fp);
-       fgets(buf, bufsize, fp);
-       fgets(buf, bufsize, fp);
-       if ((datastart = strstr(buf, needle)) != NULL) {
+       fgets(buf, sizeof(buf), fp);
+       fgets(buf, sizeof(buf), fp);
+       fgets(buf, sizeof(buf), fp);
+
+       datastart = strstr(buf, needle);
+       if (datastart != NULL) {
                datastart = strstr(buf, ":");
                sscanf(datastart + 1, " %*d   %d  %*d  %*d                %*d      %*d          %*d              %*d      %*d            %*d", &strength);
        }
@@ -581,24 +582,26 @@ static char *
 wifi_essid(const char *wificard)
 {
        char id[IW_ESSID_MAX_SIZE+1];
-       int sockfd;
+       int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        struct iwreq wreq;
 
        memset(&wreq, 0, sizeof(struct iwreq));
        wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
        sprintf(wreq.ifr_name, wificard);
-       if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
-               fprintf(stderr, "Cannot open socket for interface: %s\n", wificard);
-               return smprintf(unknowntext);
+       if(sockfd == -1) {
+               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(stderr, "Get ESSID ioctl failed for interface %s\n", wificard);
-               return smprintf(unknowntext);
+               fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n",
+                                               wificard, strerror(errno));
+               return smprintf(UNKNOWN_STR);
        }
 
        if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
-               return smprintf(unknowntext);
+               return smprintf(UNKNOWN_STR);
        else
                return smprintf("%s", (char *)wreq.u.essid.pointer);
 }
@@ -607,37 +610,33 @@ int
 main(void)
 {
        size_t i;
-       char status_string[1024];
+       char status_string[4096];
        char *res, *element;
        struct arg argument;
 
-       if (!(dpy = XOpenDisplay(0x0))) {
-               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(unknowntext);
-                               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;
 }