Xinqi Bao's Git

added my fancy icon
[slstatus.git] / slstatus.c
index 55e638f..6c25f3f 100644 (file)
@@ -126,7 +126,7 @@ battery_perc(const char *battery)
 
 static char *
 cpu_perc(void)
 
 static char *
 cpu_perc(void)
-{
+{ /* FIXME: ugly function, would be better without sleep(), see below */
        int perc;
        long double a[4], b[4];
        FILE *fp = fopen("/proc/stat","r");
        int perc;
        long double a[4], b[4];
        FILE *fp = fopen("/proc/stat","r");
@@ -150,6 +150,7 @@ cpu_perc(void)
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
        fclose(fp);
        perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
        fclose(fp);
        perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+
        return smprintf("%d%%", perc);
 }
 
        return smprintf("%d%%", perc);
 }
 
@@ -175,6 +176,7 @@ disk_free(const char *mountpoint)
                warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
                warn("Could not get filesystem info");
                return smprintf(UNKNOWN_STR);
        }
+
        return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
 }
 
        return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
 }
 
@@ -190,6 +192,7 @@ disk_perc(const char *mountpoint)
        }
 
        perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
        }
 
        perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
+
        return smprintf("%d%%", perc);
 }
 
        return smprintf("%d%%", perc);
 }
 
@@ -232,6 +235,7 @@ entropy(void)
 
        fscanf(fp, "%d", &entropy);
        fclose(fp);
 
        fscanf(fp, "%d", &entropy);
        fclose(fp);
+
        return smprintf("%d", entropy);
 }
 
        return smprintf("%d", entropy);
 }
 
@@ -257,6 +261,7 @@ hostname(void)
        memset(&hostname[strlen(hostname)-1], '\0',
                sizeof(hostname) - strlen(hostname));
        fclose(fp);
        memset(&hostname[strlen(hostname)-1], '\0',
                sizeof(hostname) - strlen(hostname));
        fclose(fp);
+
        return smprintf("%s", hostname);
 }
 
        return smprintf("%s", hostname);
 }
 
@@ -272,7 +277,6 @@ ip(const char *interface)
                return smprintf(UNKNOWN_STR);
        }
 
                return smprintf(UNKNOWN_STR);
        }
 
-       /* get the ip address */
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
                if (ifa->ifa_addr == NULL)
                        continue;
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
                if (ifa->ifa_addr == NULL)
                        continue;
@@ -289,7 +293,6 @@ ip(const char *interface)
                }
        }
 
                }
        }
 
-       /* free the address */
        freeifaddrs(ifaddr);
 
        return smprintf(UNKNOWN_STR);
        freeifaddrs(ifaddr);
 
        return smprintf(UNKNOWN_STR);
@@ -321,6 +324,7 @@ ram_free(void)
 
        fscanf(fp, "MemFree: %ld kB\n", &free);
        fclose(fp);
 
        fscanf(fp, "MemFree: %ld kB\n", &free);
        fclose(fp);
+
        return smprintf("%f", (float)free / 1024 / 1024);
 }
 
        return smprintf("%f", (float)free / 1024 / 1024);
 }
 
@@ -343,6 +347,7 @@ ram_perc(void)
 
        fclose(fp);
        perc = 100 * ((total - free) - (buffers + cached)) / total;
 
        fclose(fp);
        perc = 100 * ((total - free) - (buffers + cached)) / total;
+
        return smprintf("%d%%", perc);
 }
 
        return smprintf("%d%%", perc);
 }
 
@@ -359,6 +364,7 @@ ram_total(void)
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
        fclose(fp);
 
        fscanf(fp, "MemTotal: %ld kB\n", &total);
        fclose(fp);
+
        return smprintf("%f", (float)total / 1024 / 1024);
 }
 
        return smprintf("%f", (float)total / 1024 / 1024);
 }
 
@@ -380,6 +386,7 @@ ram_used(void)
 
        fclose(fp);
        used = total - free - buffers - cached;
 
        fclose(fp);
        used = total - free - buffers - cached;
+
        return smprintf("%f", (float)used / 1024 / 1024);
 }
 
        return smprintf("%f", (float)used / 1024 / 1024);
 }
 
@@ -388,7 +395,7 @@ run_command(const char* command)
 {
        int good;
        FILE *fp = popen(command, "r");
 {
        int good;
        FILE *fp = popen(command, "r");
-       char buffer[64];
+       char buffer[64] = "";
 
        if (fp == NULL) {
                warn("Could not get command output for: %s", command);
 
        if (fp == NULL) {
                warn("Could not get command output for: %s", command);
@@ -405,6 +412,7 @@ run_command(const char* command)
        }
        if (good)
                buffer[strlen(buffer)-1] = '\0';
        }
        if (good)
                buffer[strlen(buffer)-1] = '\0';
+
        return smprintf("%s", buffer);
 }
 
        return smprintf("%s", buffer);
 }
 
@@ -421,6 +429,7 @@ temp(const char *file)
 
        fscanf(fp, "%d", &temperature);
        fclose(fp);
 
        fscanf(fp, "%d", &temperature);
        fclose(fp);
+
        return smprintf("%d°C", temperature / 1000);
 }
 
        return smprintf("%d°C", temperature / 1000);
 }
 
@@ -444,11 +453,12 @@ username(void)
        uid_t uid = geteuid();
        struct passwd *pw = getpwuid(uid);
 
        uid_t uid = geteuid();
        struct passwd *pw = getpwuid(uid);
 
-       if (pw == NULL)
-               return smprintf("%s", pw->pw_name);
+       if (pw == NULL) {
+               warn("Could not get username");
+               return smprintf(UNKNOWN_STR);
+       }
 
 
-       warn("Could not get username");
-       return smprintf(UNKNOWN_STR);
+       return smprintf("%s", pw->pw_name);
 }
 
 static char *
 }
 
 static char *
@@ -532,6 +542,7 @@ wifi_perc(const char *wificard)
        }
 
        fclose(fp);
        }
 
        fclose(fp);
+
        return smprintf("%d%%", strength);
 }
 
        return smprintf("%d%%", strength);
 }
 
@@ -590,7 +601,9 @@ main(void)
                }
                XStoreName(dpy, DefaultRootWindow(dpy), status_string);
                XSync(dpy, False);
                }
                XStoreName(dpy, DefaultRootWindow(dpy), status_string);
                XSync(dpy, False);
+               sleep(UPDATE_INTERVAL - 1); /* FIXME: ugly cpu function which uses 1 second */
        }
        }
+
        XCloseDisplay(dpy);
 
        return 0;
        XCloseDisplay(dpy);
 
        return 0;