blob: 58d40df415a67c78c6215dc10a21a819ed90982c (
plain) (
blame)
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
|
#include <stdio.h>
#include "memory.h"
int memory(void) {
FILE *file = fopen("/proc/meminfo", "r");
if (!file) {
fclose(file);
perror("fopen");
return 1;
}
char line[256];
long total = 0;
long available = 0;
// read per line
while (fgets(line, sizeof(line), file)) {
if (sscanf(line, "MemTotal: %ld kB", &total) == 1) continue;
if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) continue;
}
fclose(file);
printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0));
return 0;
}
|