aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cpu.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/cpu.c b/cpu.c
index 9573f96..a1068c2 100644
--- a/cpu.c
+++ b/cpu.c
@@ -1,38 +1,39 @@
#include <stdio.h>
#include "block.h"
#define ICON_CPU " "
+#define FG "#222222"
+#define BG0 "#acae3299"
+#define BG1 "#ea544399"
FILE *proc_stat;
-int cpu_stat[8], cpu_delay = 6;
-char cpu_str[11];
+int cpu_delay = 6, cpu_stat[8];
+char cpu_str[12];
+int total, idle;
+double cpu_usage_p;
void cpu(void) {
if (cpu_delay++ < 6) goto show_stat;
else cpu_delay = 0;
- double prev_idle, prev_total, total, idle, usage_p;
+ int prev_idle, prev_total;
+ char buf[64];
prev_idle = idle;
prev_total = total;
if (!proc_stat) proc_stat = fopen("/proc/stat","r");
if (proc_stat) {
- int c, i;
+ int c, i = 0, j = 0;
fseek(proc_stat,5,0);
- for (i = 0;i < 8;i++) cpu_stat[i] = 0;
- for (i = 0; i < 8;) {
- c = fgetc(proc_stat);
- if (c != ' ')
+ fgets(buf,64,proc_stat);
+ while (i < 8) cpu_stat[i++] = 0;
+ for (i = 0; i < 8; i++)
+ while ((c = buf[j++]) != ' ')
cpu_stat[i] = 10 * cpu_stat[i] + (c - '0');
- else i++;
- }
total = cpu_stat[0]+cpu_stat[1]+cpu_stat[2];
idle = cpu_stat[3]+cpu_stat[4];
total += cpu_stat[5]+cpu_stat[6]+cpu_stat[7];
}
- usage_p = (idle + total) - (prev_idle + prev_total);
- usage_p -= (idle - prev_idle);
- usage_p *= 100;
- usage_p /= (idle + total) - (prev_idle + prev_total);
- snprintf(cpu_str,11,ICON_CPU"%02.2f%%",usage_p);
+ cpu_usage_p = total - prev_total;
+ cpu_usage_p *= 100;
+ cpu_usage_p /= ((float)idle + total) - (prev_idle + prev_total);
+ snprintf(cpu_str,12,ICON_CPU"%02.02f%%",cpu_usage_p);
show_stat:
- if (usage_p < 60)
- block(cpu_str,"#222222","#acae3299","#acae3299");
- else
- block(cpu_str,"#222222","#ea544399","#ea544399");
+ if (cpu_usage_p < 60) block(cpu_str,FG,BG0,BG0);
+ else block(cpu_str,FG,BG1,BG1);
}