Files
fastfetch/src/detection/memory/memory_linux.c
T

49 lines
1.3 KiB
C
Raw Normal View History

#include "memory.h"
#include <stdlib.h>
2022-09-08 14:51:31 +02:00
#include <string.h>
void ffDetectMemoryImpl(FFMemoryResult* memory)
{
2022-09-08 14:51:31 +02:00
memset(memory, 0, sizeof(FFMemoryResult));
FILE* meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL)
return;
char* line = NULL;
size_t len = 0;
uint32_t memTotal = 0,
shmem = 0,
memFree = 0,
buffers = 0,
cached = 0,
2022-09-08 14:51:31 +02:00
sReclaimable = 0,
swapTotal = 0,
swapFree = 0;
while (getline(&line, &len, meminfo) != EOF)
{
sscanf(line, "MemTotal: %u", &memTotal);
sscanf(line, "Shmem: %u", &shmem);
sscanf(line, "MemFree: %u", &memFree);
sscanf(line, "Buffers: %u", &buffers);
sscanf(line, "Cached: %u", &cached);
sscanf(line, "SReclaimable: %u", &sReclaimable);
2022-09-08 14:51:31 +02:00
sscanf(line, "SwapTotal: %u", &swapTotal);
sscanf(line, "SwapFree: %u", &swapFree);
}
if(line != NULL)
free(line);
fclose(meminfo);
2022-09-08 14:51:31 +02:00
memory->ram.bytesTotal = memTotal * (uint64_t) 1024;
memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
memory->swap.bytesTotal = swapTotal * (uint64_t) 1024;
memory->swap.bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024;
}