Xinqi Bao's Git

fix delay in a cleaner way
[slstatus.git] / slstatus.c
index 21cda51..c92da0d 100644 (file)
@@ -75,7 +75,7 @@ static void usage(void);
 
 char *argv0;
 char concat[];
-static unsigned short int delay;
+static unsigned short int delay = 0;
 static unsigned short int done;
 static unsigned short int dflag, oflag;
 static Display *dpy;
@@ -164,7 +164,7 @@ cpu_perc(void)
        fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
        fclose(fp);
 
-       delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
+       delay++;
        sleep(delay);
 
        fp = fopen("/proc/stat", "r");
@@ -405,19 +405,22 @@ ram_used(void)
 static char *
 run_command(const char *cmd)
 {
+       char *nlptr;
        FILE *fp;
-       char buf[1024] = "n/a";
+       char buf[1024] = UNKNOWN_STR;
 
        fp = popen(cmd, "r");
        if (fp == NULL) {
                warn("Failed to get command output for %s", cmd);
                return smprintf(UNKNOWN_STR);
        }
-       fgets(buf, sizeof(buf)-1, fp);
+       fgets(buf, sizeof(buf), fp);
        pclose(fp);
-
        buf[strlen(buf)] = '\0';
-       strtok(buf, "\n");
+
+       if ((nlptr = strstr(buf, "\n")) != NULL) {
+               nlptr[0] = '\0';
+       }
 
        return smprintf("%s", buf);
 }
@@ -440,7 +443,7 @@ swap_free(void)
        buf[bytes_read] = '\0';
        fclose(fp);
        if (bytes_read == 0 || bytes_read == sizeof(buf)) {
-               warn("Failed to read /proc/meminfo\n");
+               warn("Failed to read from /proc/meminfo");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -474,7 +477,7 @@ swap_perc(void)
        buf[bytes_read] = '\0';
        fclose(fp);
        if (bytes_read == 0 || bytes_read == sizeof(buf)) {
-               warn("Failed to read /proc/meminfo\n");
+               warn("Failed to read from /proc/meminfo");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -511,7 +514,7 @@ swap_total(void)
        buf[bytes_read] = '\0';
        fclose(fp);
        if (bytes_read == 0 || bytes_read == sizeof(buf)) {
-               warn("Failed to read /proc/meminfo\n");
+               warn("Failed to read from /proc/meminfo");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -542,7 +545,7 @@ swap_used(void)
        buf[bytes_read] = '\0';
        fclose(fp);
        if (bytes_read == 0 || bytes_read == sizeof(buf)) {
-               warn("Failed to read /proc/meminfo\n");
+               warn("Failed to read from /proc/meminfo");
                return smprintf(UNKNOWN_STR);
        }
 
@@ -826,8 +829,13 @@ main(int argc, char *argv[])
                 * subtract delay time spend in function
                 * calls from the actual global delay time
                 */
-               sleep(UPDATE_INTERVAL - delay);
-               delay = 0;
+               if ((UPDATE_INTERVAL - delay) <= 0) {
+                       delay = 0;
+                       continue;
+               } else {
+                       sleep(UPDATE_INTERVAL - delay);
+                       delay = 0;
+               }
        }
 
        if (!oflag) {