mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
@@ -1,15 +1,34 @@
|
||||
#include "swap.h"
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
#include "common/io/io.h"
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
const char* ffDetectSwap(FFSwapResult* swap)
|
||||
{
|
||||
struct sysinfo info;
|
||||
if(sysinfo(&info) != 0)
|
||||
return "sysinfo() failed";
|
||||
// #620
|
||||
FF_AUTO_CLOSE_FILE FILE* meminfo = fopen("/proc/meminfo", "r");
|
||||
if (!meminfo) return "fopen(\"/proc/meminfo\", \"r\") failed";
|
||||
|
||||
swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit;
|
||||
swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit;
|
||||
uint64_t swapTotal = 0, swapFree = 0;
|
||||
|
||||
char* FF_AUTO_FREE line = NULL;
|
||||
size_t len = 0;
|
||||
uint8_t count = 0;
|
||||
|
||||
while (getline(&line, &len, meminfo) != EOF)
|
||||
{
|
||||
if (line[0] == 'S')
|
||||
{
|
||||
if(sscanf(line, "SwapTotal: %" PRIu64, &swapTotal) > 0 || sscanf(line, "SwapFree: %" PRIu64, &swapFree) > 0)
|
||||
if (++count >= 2) goto done;
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
swap->bytesTotal = swapTotal * 1024lu;
|
||||
swap->bytesUsed = (swapTotal - swapFree) * 1024lu;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user