summaryrefslogtreecommitdiff
path: root/src/memory.c
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-19 23:52:56 +0900
committer諏訪子 <suwako@076.moe>2024-06-19 23:52:56 +0900
commitb6e3f6cb5d1051777c35988f201e6730525e3389 (patch)
tree98679f1995f7b103f3314ce54fdf0d1c76df6dbd /src/memory.c
parent8f378429801162d690dc68c0f15b483c1d291838 (diff)
掃除
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c49
1 files changed, 14 insertions, 35 deletions
diff --git a/src/memory.c b/src/memory.c
index 7fa55be..879fef7 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -1,60 +1,39 @@
#include "memory.h"
+#include "common.h"
#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-long long int run_mem_command(const char *command) {
- char buf[128];
- long long int res = 0;
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "メモリコマンドを実効に失敗: %s", command);
- return 0;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- }
-
- res = atoll(buf);
- pclose(p);
-
- return res;
-}
void display_memory() {
long long int memused = 0;
long long int memtotal = 0;
#if defined(__OpenBSD__)
- memused = run_mem_command("vmstat | awk 'END {printf $3}' | sed 's/M//'");
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memused = run_command_lld("vmstat | awk 'END {printf $3}' | sed 's/M//'");
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
// 13M Active, 200M Inact, 184M Laundry,
- long long int used1 = run_mem_command("top | grep \"Mem:\" | awk '{print $2}' | "
+ long long int used1 = run_command_lld("top | grep \"Mem:\" | awk '{print $2}' | "
"sed 's/M//'");
- long long int used2 = run_mem_command("top | grep \"Mem:\" | awk '{print $4}' | "
+ long long int used2 = run_command_lld("top | grep \"Mem:\" | awk '{print $4}' | "
"sed 's/M//'");
- long long int used3 = run_mem_command("top | grep \"Mem:\" | awk '{print $6}' | "
+ long long int used3 = run_command_lld("top | grep \"Mem:\" | awk '{print $6}' | "
"sed 's/M//'");
- long long int used4 = run_mem_command("top | grep \"ARC:\" | awk '{print $6}' | "
+ long long int used4 = run_command_lld("top | grep \"ARC:\" | awk '{print $6}' | "
"sed 's/M//'");
memused = used1 + used2 + used3 + used4;
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__NetBSD__)
- memused = run_mem_command("top | grep \"Memory:\" | awk '{print $2}' | "
+ memused = run_command_lld("top | grep \"Memory:\" | awk '{print $2}' | "
"sed 's/M//'");
- memtotal = run_mem_command("sysctl -n hw.physmem64") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem64") / 1024LL / 1024LL;
#elif defined(__minix)
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__linux__)
- long long int memaval = run_mem_command("cat /proc/meminfo | grep MemAvailable | "
+ long long int memaval = run_command_lld("cat /proc/meminfo | grep MemAvailable | "
"awk '{print $2}'") / 1024LL;
- memtotal = run_mem_command("cat /proc/meminfo | grep MemTotal | "
+ memtotal = run_command_lld("cat /proc/meminfo | grep MemTotal | "
"awk '{print $2}'") / 1024LL;
memused = memtotal - memaval;
- /* memused = run_mem_command("cat /proc/meminfo | grep MemFree | "
+ /* memused = run_command_lld("cat /proc/meminfo | grep MemFree | "
"awk '{print $2}'") / 1024LL; */
#endif