diff options
| author | Inqiyad Sabr <sabr@ariamath.xyz> | 2025-10-28 15:20:54 +0600 |
|---|---|---|
| committer | Inqiyad Sabr <sabr@ariamath.xyz> | 2025-10-28 15:20:54 +0600 |
| commit | 0d4272c8bc1c8d7cca4178584f4f9ad42fcc1fd6 (patch) | |
| tree | d2a3b5fcacc83c38d0f623b88348bfddb73f8ece /cpu.c | |
| parent | ac9432fd41ba67590388c844465b1aa632d3316d (diff) | |
fixed cpu module variables, and wrong value
and oh yeah it was fucked up bad... I messed up with some damn simple arithmetic and it was getting to my nerves I fucking hate this...
Diffstat (limited to 'cpu.c')
| -rw-r--r-- | cpu.c | 39 |
1 files changed, 20 insertions, 19 deletions
@@ -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); } |