aboutsummaryrefslogtreecommitdiff
path: root/src/modules/memory/memory.c
blob: 62eeafef6253c6da6bbc61b7acb9a3946adf6568 (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
#include <stdio.h>
#include "memory.h"

char *memory(void) {
    FILE *file = fopen("/proc/meminfo", "r");

    if (!file) {
	perror("Error fopen meminfo");
	return NULL;
    }

    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;
}