From 8723e8b8c69c08bae8abe02ea1f4a49758b8bdfc Mon Sep 17 00:00:00 2001 From: drkhsh Date: Thu, 24 Jul 2025 22:41:25 +0200 Subject: more concise memory calculation on Linux more flexible parsing for /proc/meminfo to take shared and reclaimable memory into account. this matches the output with free(1). additionally this could fix some corner cases, as the order of fields in /proc/meminfo is not strictly defined. slstatus: percent 81% free 2.5 Gi total 23.4 Gi used 19.0 Gi free(1): total used free shared buff/cache available Mem: 23Gi 19Gi 2.5Gi 1.3Gi 3.2Gi 3.6Gi --- util.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index bca9b2e..d33cf9d 100644 --- a/util.c +++ b/util.c @@ -139,3 +139,20 @@ pscanf(const char *path, const char *fmt, ...) return (n == EOF) ? -1 : n; } + +int +lscanf(FILE *fp, const char *key, const char *fmt, void *res) +{ + int n; + char line[256]; + + n = -1; + while (fgets(line, sizeof(line), fp)) + if (strncmp(line, key, strlen(key)) == 0) { + n = sscanf(line + strlen(key), fmt, res); + break; + } + + rewind(fp); + return (n == 1) ? 1 : -1; +} -- cgit v1.2.3