summaryrefslogtreecommitdiff
path: root/src/host.c
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-17 22:41:46 +0900
committer諏訪子 <suwako@076.moe>2024-06-17 22:44:17 +0900
commitcf13f2d8c1622a46f6499dcb762ea2b9370d99fc (patch)
treee3a3cefd85fd1bd3d77e76f074990f8452888dc9 /src/host.c
最初コミット
Diffstat (limited to 'src/host.c')
-rw-r--r--src/host.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/host.c b/src/host.c
new file mode 100644
index 0000000..2cc02ff
--- /dev/null
+++ b/src/host.c
@@ -0,0 +1,77 @@
+#include "host.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#if defined(__linux__)
+#include <unistd.h>
+#elif defined(__APPLE__)
+#include <stdlib.h>
+#endif
+
+void run_command(const char *command) {
+ char buf[64];
+
+ FILE *p = popen(command, "r");
+ if (!p) {
+ fprintf(stderr, "ホストコマンドを実効に失敗: %s", command);
+ return;
+ }
+
+ while (fgets(buf, sizeof(buf), p) != NULL) {
+ buf[strcspn(buf, "\n")] = '\0';
+ printf("%s", buf);
+ }
+
+ pclose(p);
+}
+
+void display_host_model() {
+#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}'");
+#elif defined(__linux__)
+ const char *cmd1, *cmd2;
+ if (access("/system/app/", F_OK) != -1) {
+ cmd1 = "getprop ro.product.brand";
+ cmd2 = "getprop ro.product.model";
+ } else if (
+ access("/sys/devices/virtual/dmi/id/product_name", F_OK) != -1 ||
+ access("/sys/devices/virtual/dmi/id/product_version", F_OK) != 1
+ ) {
+ cmd1 = "cat /sys/devices/virtual/dmi/id/product_name";
+ cmd2 = "cat /sys/devices/virtual/dmi/id/product_version";
+ } else if (access("/sys/firmware/base/model", F_OK) != -1) {
+ cmd1 = "cat /sys/firmware/devicetree/base/model";
+ } else if (access("/tmp/sysinfo/model", F_OK) != 1) {
+ cmd1 = "cat /tmp/sysinfo/model";
+ }
+
+ if (!cmd1) {
+ printf("Unknown");
+ } else {
+ run_command(cmd1);
+ }
+
+ if (cmd2) {
+ printf(" ");
+ run_command(cmd2);
+ }
+#elif defined(__APPLE__)
+ run_command("sysctl -n hw.model");
+
+ FILE *p = popen("kextstat | grep -F -e \"FakeSMC\" -e \"VirtualSMC\"", "r");
+ if (!p) {
+ fprintf(stderr, "ホストコマンドを実効に失敗");
+ return;
+ }
+
+ while (fgets(buf, sizeof(buf), p) != NULL) {
+ buf[strcspn(buf, "\n")] = '\0';
+ printf("%s", buf);
+ }
+
+ pclose(p);
+#endif
+}