-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_info.c
More file actions
54 lines (38 loc) · 1.26 KB
/
memory_info.c
File metadata and controls
54 lines (38 loc) · 1.26 KB
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
#include <stdio.h>
#include <string.h>
#include <ncurses.h> //TUI
typedef struct memory {
double total_val;
double avalaible_val; //defaulted to kb
} memory ;
void get_memory_info() {
FILE *proc_mem = fopen("/proc/meminfo", "r"); // read the file
if (proc_mem == NULL) {
perror("fopen");
return;
}
memory dict = {-1,-1};
char line[256];
int row = 3, col=5;
while(fgets(line, sizeof line,proc_mem)) {
int val;
char label[64];
char size_unit[16];
sscanf(line,"%s %d %s",label, &val, size_unit); // format the data
mvprintw(row++,col,"%s", line);
if (strcmp(label, "MemTotal:") == 0) dict.total_val = val;
else if (strcmp(label, "MemAvailable:") == 0) dict.avalaible_val = val;
if (dict.avalaible_val != -1 && dict.total_val != -1) break;
}
fclose(proc_mem);
// % consumption of memory
float usedPercentage = (1 - (dict.avalaible_val / dict.total_val)) * 100;
int strikes = usedPercentage/5;
// ASCII Art
mvprintw(row++, col, "Usage: [");
attron(COLOR_PAIR(1));
for(int i=0; i<strikes; i++) addch('#');
attroff(COLOR_PAIR(1));
for(int i=strikes; i<20; i++) addch('-');
printw("] %.2f%%", usedPercentage);
}