-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
86 lines (57 loc) · 1.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <curses.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#define BUFF_SIZE 256
static long double get_cpu_util() {
long double a[4], b[4], loadavg;
FILE *stat = fopen("/proc/stat","r");
fscanf(stat, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
fclose(stat);
sleep(1);
stat = fopen("/proc/stat","r");
fscanf(stat, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
fclose(stat);
loadavg = ((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 loadavg;
}
static void get_mem(int *avaiableRAM, int *totalRAM) {
FILE *meminfo = fopen("/proc/meminfo", "rt");
fscanf(meminfo, "%*s %d\n %*s %*s %*d %*s\n %*s %d", totalRAM, avaiableRAM);
fclose(meminfo);
}
static void get_time(char *buf) {
time_t rawtime;
time(&rawtime);
strftime(buf, 32, "%a %d-%m %H:%M:%S", localtime(&rawtime));
}
int main() {
initscr();
start_color();
curs_set(false);
init_color(COLOR_BLACK, 0, 0, 0);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
attron(COLOR_PAIR(1));
bkgd(COLOR_PAIR(1));
FILE *file;
char buff[BUFF_SIZE];
char time_buff[32];
int totalRAM, avaiableRAM;
while (true) {
clear();
move(0, 0);
get_mem(&avaiableRAM, &totalRAM);
get_time(time_buff);
file = fopen("bs", "a+");
while (fgets(buff, BUFF_SIZE, file))
printw(buff, time_buff);
fclose(file);
printw("\n\tCPU Utilization: %.2Lf%%\n", get_cpu_util() * 100);
// bitshifting instead of dividing by 1024
printw("\tUsed RAM: %d MB \n\tTotal RAM: %d MB (%.2f%% used)\n", (totalRAM >> 10) - (avaiableRAM >> 10), (totalRAM >> 10), ((double) ((totalRAM >> 10) - (avaiableRAM >> 10)) / (totalRAM >> 10)) * 100);
refresh();
}
return 0;
}