master ohmu/fastfetch-openbsd / src / detection / swap / swap_linux.c
ohmu  ·  2026-10-15
 1#include "swap.h"
 2
 3#include "common/debug.h"
 4#include "common/io.h"
 5#include "common/mallocHelper.h"
 6
 7#include <inttypes.h>
 8
 9static const char* detectByProcMeminfo(FFlist* result) {
10    // For Android
11    // Ref: #620
12    char buf[PROC_FILE_BUFFSIZ];
13    ssize_t nRead = ffReadFileData("/proc/meminfo", ARRAY_SIZE(buf) - 1, buf);
14    if (nRead < 0) {
15        FF_DEBUG("ffReadFileData(\"/proc/meminfo\") failed");
16        return "ffReadFileData(\"/proc/meminfo\", ARRAY_SIZE(buf)-1, buf)";
17    }
18    buf[nRead] = '\0';
19
20    uint64_t swapTotal = 0, swapFree = 0;
21
22    char* token = nullptr;
23    if ((token = strstr(buf, "SwapTotal:")) != nullptr) {
24        swapTotal = (uint64_t) strtoull(token + strlen("SwapTotal:"), nullptr, 10);
25    }
26
27    if ((token = strstr(buf, "SwapFree:")) != nullptr) {
28        swapFree = (uint64_t) strtoull(token + strlen("SwapFree:"), nullptr, 10);
29    }
30
31    FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
32    ffStrbufInitStatic(&swap->name, "Total");
33    swap->bytesTotal = swapTotal * 1024lu;
34    swap->bytesUsed = (swapTotal - swapFree) * 1024lu;
35
36    return nullptr;
37}
38
39static const char* detectByProcSwaps(FFlist* result) {
40    // Ref: #620
41    char buf[PROC_FILE_BUFFSIZ];
42    ssize_t nRead = ffReadFileData("/proc/swaps", ARRAY_SIZE(buf) - 1, buf);
43    if (nRead <= 0) {
44        FF_DEBUG("ffReadFileData(\"/proc/swaps\") failed");
45        return "ffReadFileData(\"/proc/swaps\", ARRAY_SIZE(buf)-1, buf) failed";
46    }
47    buf[nRead] = '\0';
48
49    // Skip header
50    char* line = memchr(buf, '\n', (size_t) nRead);
51
52    while (line && *++line) {
53        uint64_t total, used;
54        char name[256];
55        if (sscanf(line, "%255s %*[^\t]%" SCNu64 "%" SCNu64, name, &total, &used) != 3) {
56            FF_DEBUG("Invalid /proc/swaps format found");
57            return "Invalid /proc/swaps format found";
58        }
59
60        uint32_t nameLen = (uint32_t) strnlen(name, sizeof(name));
61        FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
62        ffStrbufInitA(&swap->name, nameLen);
63        for (size_t i = 0; i < nameLen; ++i) {
64            if (name[i] == '\\') {
65                char octal[4] = { name[i + 1], name[i + 2], name[i + 3], '\0' };
66                ffStrbufAppendC(&swap->name, (char) strtol(octal, nullptr, 8));
67                i += 3;
68            } else {
69                ffStrbufAppendC(&swap->name, name[i]);
70            }
71        }
72        swap->bytesTotal = total * 1024u;
73        swap->bytesUsed = used * 1024u;
74
75        line = memchr(line, '\n', (size_t) (nRead - (line - buf)));
76    }
77
78    return nullptr;
79}
80
81const char* ffDetectSwap(FFlist* result) {
82    if (detectByProcSwaps(result) == nullptr) {
83        return nullptr;
84    }
85    return detectByProcMeminfo(result);
86}