Refactor ram.c

This commit is contained in:
Aaron Marcher 2018-05-20 00:42:07 +02:00
parent 7873b3b132
commit c12b9cc50b
1 changed files with 37 additions and 24 deletions

View File

@ -9,12 +9,15 @@
{ {
long free; long free;
return (pscanf("/proc/meminfo", if (pscanf("/proc/meminfo",
"MemTotal: %ld kB\n" "MemTotal: %ld kB\n"
"MemFree: %ld kB\n" "MemFree: %ld kB\n"
"MemAvailable: %ld kB\n", "MemAvailable: %ld kB\n",
&free, &free, &free) == 3) ? &free, &free, &free) != 3) {
fmt_human_2(free * 1024, "B") : NULL; return NULL;
}
return fmt_human_2(free * 1024, "B");
} }
const char * const char *
@ -22,15 +25,17 @@
{ {
long total, free, buffers, cached; long total, free, buffers, cached;
return (pscanf("/proc/meminfo", if (pscanf("/proc/meminfo",
"MemTotal: %ld kB\n" "MemTotal: %ld kB\n"
"MemFree: %ld kB\n" "MemFree: %ld kB\n"
"MemAvailable: %ld kB\nBuffers: %ld kB\n" "MemAvailable: %ld kB\nBuffers: %ld kB\n"
"Cached: %ld kB\n", "Cached: %ld kB\n",
&total, &free, &buffers, &buffers, &cached) == 5) ? &total, &free, &buffers, &buffers, &cached) != 5) {
bprintf("%d%%", 100 * ((total - free) - (buffers + cached)) / return NULL;
total) : }
NULL;
return bprintf("%d%%", 100 * ((total - free) -
(buffers + cached)) / total);
} }
const char * const char *
@ -38,8 +43,12 @@
{ {
long total; long total;
return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ? if (pscanf("/proc/meminfo", "MemTotal: %ld kB\n",
fmt_human_2(total * 1024, "B") : NULL; &total) != 1) {
return NULL;
}
return fmt_human_2(total * 1024, "B");
} }
const char * const char *
@ -47,13 +56,17 @@
{ {
long total, free, buffers, cached; long total, free, buffers, cached;
return (pscanf("/proc/meminfo", if (pscanf("/proc/meminfo",
"MemTotal: %ld kB\n" "MemTotal: %ld kB\n"
"MemFree: %ld kB\n" "MemFree: %ld kB\n"
"MemAvailable: %ld kB\nBuffers: %ld kB\n" "MemAvailable: %ld kB\nBuffers: %ld kB\n"
"Cached: %ld kB\n", "Cached: %ld kB\n",
&total, &free, &buffers, &buffers, &cached) == 5) ? &total, &free, &buffers, &buffers, &cached) != 5) {
fmt_human_2((total - free - buffers - cached) * 1024, "B") : NULL; return NULL;
}
return fmt_human_2((total - free - buffers - cached) * 1024,
"B");
} }
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#include <stdlib.h> #include <stdlib.h>