summaryrefslogtreecommitdiff
path: root/ram.c
diff options
context:
space:
mode:
authorAaron Marcher <me@drkhsh.at>2017-09-17 16:18:17 +0200
committerAaron Marcher <me@drkhsh.at>2017-09-17 17:26:45 +0200
commit6820631175868c277effa7cc05f9cb3197b72654 (patch)
tree22707fe4a93e8357c7944087315a70d402fd418d /ram.c
parent8e25af7dc33bdcc0df6f4f173da7a2f883141d70 (diff)
Split into multiple files
For multiple reasons the program is now split: - Make future porting to OpenBSD easier - Assign header includes to individiual functions - Make future program extensions easier - Recompile only changed parts
Diffstat (limited to 'ram.c')
-rw-r--r--ram.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/ram.c b/ram.c
new file mode 100644
index 0000000..8f7c682
--- /dev/null
+++ b/ram.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+
+#include "util.h"
+
+const char *
+ram_free(void)
+{
+ long free;
+
+ return (pscanf("/proc/meminfo", "MemFree: %ld kB\n", &free) == 1) ?
+ bprintf("%f", (float)free / 1024 / 1024) : NULL;
+}
+
+const char *
+ram_perc(void)
+{
+ long total, free, buffers, cached;
+
+ return (pscanf("/proc/meminfo",
+ "MemTotal: %ld kB\n"
+ "MemFree: %ld kB\n"
+ "MemAvailable: %ld kB\nBuffers: %ld kB\n"
+ "Cached: %ld kB\n",
+ &total, &free, &buffers, &buffers, &cached) == 5) ?
+ bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total) :
+ NULL;
+}
+
+const char *
+ram_total(void)
+{
+ long total;
+
+ return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ?
+ bprintf("%f", (float)total / 1024 / 1024) : NULL;
+}
+
+const char *
+ram_used(void)
+{
+ long total, free, buffers, cached;
+
+ return (pscanf("/proc/meminfo",
+ "MemTotal: %ld kB\n"
+ "MemFree: %ld kB\n"
+ "MemAvailable: %ld kB\nBuffers: %ld kB\n"
+ "Cached: %ld kB\n",
+ &total, &free, &buffers, &buffers, &cached) == 5) ?
+ bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024) :
+ NULL;
+}