static int calc_cpu_total() { int total, t; int rc; int ps; char line[BUFFER_LEN]; int cpu, nice, system, idle; ps = open("/proc/stat", O_RDONLY); rc = read(ps, line, sizeof(line)); close(ps); if (rc < 0) return 0; sscanf(line, "%*s %d %d %d %d", &cpu, &nice, &system, &idle); total = cpu + nice + system + idle; t = total - previous_total; previous_total = total; if (t < 0) t = 0; return t; }