summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-18 00:16:45 +0900
committer諏訪子 <suwako@076.moe>2024-06-18 00:16:45 +0900
commit11c97887ef28e9b073d9abf1c2da70cd01491c9e (patch)
tree41c7b4d79d99733b879f6c63c8c731484022fc7e
parent3d418e666955695052d25e32c6b2f296b7af6859 (diff)
メモリ
-rw-r--r--main.c14
-rw-r--r--src/host.c3
-rw-r--r--src/memory.c52
-rw-r--r--src/memory.h6
4 files changed, 69 insertions, 6 deletions
diff --git a/main.c b/main.c
index 5280995..12b2809 100644
--- a/main.c
+++ b/main.c
@@ -8,6 +8,7 @@
#if defined(__linux__)
#include "src/distro.h"
#endif
+#include "src/memory.h"
const char *sofname = "farfetch";
const char *version = "0.0.1";
@@ -26,16 +27,20 @@ int main() {
display_os_arch();
printf("\n");
- printf("Host: ");
- display_host_model();
- printf("\n");
-
#if defined(__linux__)
printf("Distro: ");
display_distro();
printf("\n");
#endif
+ printf("Host: ");
+ display_host_model();
+ printf("\n");
+
+ printf("Memory: ");
+ display_memory();
+ printf("\n");
+
// TODO:
// * ロゴ
// * カーネル(LinuxとIllumosのみ)
@@ -47,7 +52,6 @@ int main() {
// * 端末
// * CPU
// * GPU
- // * メモリー
// * ストレージ
return 0;
}
diff --git a/src/host.c b/src/host.c
index f10cc52..ebafe13 100644
--- a/src/host.c
+++ b/src/host.c
@@ -27,7 +27,8 @@ void run_command(const char *command) {
}
void display_host_model() {
-#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__minix)
+#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
+ defined(__DragonFly__) || defined(__minix)
run_command("sysctl -n hw.vendor hw.product");
#elif defined(__sun)
run_command("prtconf -b | awk -F':' '/banner-name/ {printf $2}'");
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 0000000..833fa97
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,52 @@
+#include "memory.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;
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+ memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+#elif defined(__NetBSD__)
+ memused = run_mem_command("top | grep \"Memory:\" | awk '{print $2}' | "
+ "sed 's/M//'");
+ memtotal = run_mem_command("sysctl -n hw.physmem64") / 1024LL / 1024LL;
+#elif defined(__minix)
+ memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+#elif defined(__linux__)
+ long long int memaval = run_mem_command("cat /proc/meminfo | grep MemAvailable | "
+ "awk '{print $2}'") / 1024LL;
+ memtotal = run_mem_command("cat /proc/meminfo | grep MemTotal | "
+ "awk '{print $2}'") / 1024LL;
+ memused = memtotal - memaval;
+ /* memused = run_mem_command("cat /proc/meminfo | grep MemFree | "
+ "awk '{print $2}'") / 1024LL; */
+#endif
+
+ printf("%lld MiB / %lld MiB", memused, memtotal);
+}
diff --git a/src/memory.h b/src/memory.h
new file mode 100644
index 0000000..16d89de
--- /dev/null
+++ b/src/memory.h
@@ -0,0 +1,6 @@
+#ifndef MEMORY_H
+#define MEMORY_H
+
+void display_memory();
+
+#endif