Xinqi Bao's Git

enable stack protector and compile to position independent executable
[slstatus.git] / slstatus.c
index 6d2e89e..9e6aca0 100644 (file)
@@ -47,6 +47,7 @@ static char *gid(void);
 static char *hostname(void);
 static char *ip(const char *iface);
 static char *kernel_release(void);
 static char *hostname(void);
 static char *ip(const char *iface);
 static char *kernel_release(void);
+static char *keyboard_indicators(void);
 static char *load_avg(void);
 static char *ram_free(void);
 static char *ram_perc(void);
 static char *load_avg(void);
 static char *ram_free(void);
 static char *ram_perc(void);
@@ -139,6 +140,8 @@ battery_state(const char *bat)
                return smprintf("-");
        } else if (strcmp(state, "Full") == 0) {
                return smprintf("=");
                return smprintf("-");
        } else if (strcmp(state, "Full") == 0) {
                return smprintf("=");
+       } else if (strcmp(state, "Unknown") == 0) {
+               return smprintf("/");
        } else {
                return smprintf("?");
        }
        } else {
                return smprintf("?");
        }
@@ -322,6 +325,29 @@ kernel_release(void)
        return smprintf("%s", udata.release);
 }
 
        return smprintf("%s", udata.release);
 }
 
+static char *
+keyboard_indicators(void)
+{
+       Display *dpy = XOpenDisplay(NULL);
+       XKeyboardState state;
+       XGetKeyboardControl(dpy, &state);
+       XCloseDisplay(dpy);
+
+       switch (state.led_mask) {
+               case 1:
+                       return smprintf("c");
+                       break;
+               case 2:
+                       return smprintf("n");
+                       break;
+               case 3:
+                       return smprintf("cn");
+                       break;
+               default:
+                       return smprintf("");
+       }
+}
+
 static char *
 load_avg(void)
 {
 static char *
 load_avg(void)
 {
@@ -423,9 +449,9 @@ run_command(const char *cmd)
        }
        fgets(buf, sizeof(buf), fp);
        pclose(fp);
        }
        fgets(buf, sizeof(buf), fp);
        pclose(fp);
-       buf[sizeof(buf)] = '\0';
+       buf[sizeof(buf) - 1] = '\0';
 
 
-       if ((nlptr = strstr(buf, "\n")) != NULL) {
+       if ((nlptr = strrchr(buf, '\n')) != NULL) {
                nlptr[0] = '\0';
        }
 
                nlptr[0] = '\0';
        }
 
@@ -447,7 +473,7 @@ swap_free(void)
                return smprintf("%s", UNKNOWN_STR);
        }
 
                return smprintf("%s", UNKNOWN_STR);
        }
 
-       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                warn("swap_free: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
                warn("swap_free: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
@@ -484,7 +510,7 @@ swap_perc(void)
                return smprintf("%s", UNKNOWN_STR);
        }
 
                return smprintf("%s", UNKNOWN_STR);
        }
 
-       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                warn("swap_perc: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
                warn("swap_perc: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
@@ -525,7 +551,7 @@ swap_total(void)
                warn("Failed to open file /proc/meminfo");
                return smprintf("%s", UNKNOWN_STR);
        }
                warn("Failed to open file /proc/meminfo");
                return smprintf("%s", UNKNOWN_STR);
        }
-       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                warn("swap_total: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
                warn("swap_total: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
@@ -556,7 +582,7 @@ swap_used(void)
                warn("Failed to open file /proc/meminfo");
                return smprintf("%s", UNKNOWN_STR);
        }
                warn("Failed to open file /proc/meminfo");
                return smprintf("%s", UNKNOWN_STR);
        }
-       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+       if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
                warn("swap_used: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
                warn("swap_used: read error");
                fclose(fp);
                return smprintf("%s", UNKNOWN_STR);
@@ -617,8 +643,7 @@ uptime(void)
 static char *
 username(void)
 {
 static char *
 username(void)
 {
-       uid_t uid = geteuid();
-       struct passwd *pw = getpwuid(uid);
+       struct passwd *pw = getpwuid(geteuid());
 
        if (pw == NULL) {
                warn("Failed to get username");
 
        if (pw == NULL) {
                warn("Failed to get username");
@@ -642,25 +667,29 @@ vol_perc(const char *card)
        int v, afd, devmask;
        char *vnames[] = SOUND_DEVICE_NAMES;
 
        int v, afd, devmask;
        char *vnames[] = SOUND_DEVICE_NAMES;
 
-       afd = open(card, O_RDONLY);
-       if (afd < 0) {
+       afd = open(card, O_RDONLY | O_NONBLOCK);
+       if (afd == -1) {
                warn("Cannot open %s", card);
                return smprintf(UNKNOWN_STR);
        }
 
                warn("Cannot open %s", card);
                return smprintf(UNKNOWN_STR);
        }
 
-       ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
+       if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
+               warn("Cannot get volume for %s", card);
+               close(afd);
+               return smprintf("%s", UNKNOWN_STR);
+       }
        for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
        for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
-               if (devmask & (1 << i)) {
-                       if (!strcmp("vol", vnames[i])) {
-                               ioctl(afd, MIXER_READ(i), &v);
+               if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
+                       if (ioctl(afd, MIXER_READ(i), &v) == -1) {
+                               warn("vol_perc: ioctl");
+                               close(afd);
+                               return smprintf("%s", UNKNOWN_STR);
                        }
                }
        }
 
        close(afd);
                        }
                }
        }
 
        close(afd);
-       if (v == 0) {
-               return smprintf("mute");
-       }
+
        return smprintf("%d%%", v & 0xff);
 }
 
        return smprintf("%d%%", v & 0xff);
 }