diff options
author | 諏訪子 <suwako@076.moe> | 2024-06-17 22:41:46 +0900 |
---|---|---|
committer | 諏訪子 <suwako@076.moe> | 2024-06-17 22:44:17 +0900 |
commit | cf13f2d8c1622a46f6499dcb762ea2b9370d99fc (patch) | |
tree | e3a3cefd85fd1bd3d77e76f074990f8452888dc9 /src |
最初コミット
Diffstat (limited to 'src')
-rw-r--r-- | src/distro.c | 48 | ||||
-rw-r--r-- | src/distro.h | 8 | ||||
-rw-r--r-- | src/host.c | 77 | ||||
-rw-r--r-- | src/host.h | 6 | ||||
-rw-r--r-- | src/os.c | 52 | ||||
-rw-r--r-- | src/os.h | 8 | ||||
-rw-r--r-- | src/user.c | 52 | ||||
-rw-r--r-- | src/user.h | 7 |
8 files changed, 258 insertions, 0 deletions
diff --git a/src/distro.c b/src/distro.c new file mode 100644 index 0000000..e94922b --- /dev/null +++ b/src/distro.c @@ -0,0 +1,48 @@ +#if defined(__linux__) +#include "distro.h" + +#include <stdio.h> +#include <string.h> + +#include <unistd.h> + +void display_distro() { + char buf[64]; + const char *cmd; + + if (access("/bedrock/etc/bedrock-release", F_OK) != -1) { + cmd = "cat /bedrock/etc/bedrock-release"; + } else if (access("/etc/redstar-release", F_OK) != -1) { + cmd = "echo \"Red Star OS\" && cat /etc/redstar-release | awk -F'[^0-9]' '$0=$2'"; + } else if (access("/etc/siduction-version", F_OK) != -1) { + cmd = "echo \"Siduction\" && lsb_release -sic"; + } else if (access("/etc/mcst_version", F_OK) != -1) { + cmd = "echo \"OS Elbrus\" && cat /etc/mcst_version"; + } else if (access("/etc/GoboLinuxVersion", F_OK) != -1) { + cmd = "echo \"GoboLinux\" && cat /etc/GoboLinuxVersion"; + } else if (access("/etc/os-release", F_OK) != -1) { + cmd = "cat /etc/os-release"; + } else if (access("/usr/lib/os-release", F_OK) != -1) { + cmd = "cat /usr/lib/os-release"; + } else if (access("/etc/openwrt_release", F_OK) != -1) { + cmd = "cat /etc/openwrt_release"; + } else if (access("/etc/lsb-release", F_OK) != -1) { + cmd = "cat /etc/lsb-release"; + } else { + perror("不明なディストリビューション。"); + } + + FILE *p = popen(cmd, "r"); + if (!p) { + fprintf(stderr, "ディストロを見つけられるコマンドを実効に失敗: %s", cmd); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} +#endif diff --git a/src/distro.h b/src/distro.h new file mode 100644 index 0000000..c4ecfd6 --- /dev/null +++ b/src/distro.h @@ -0,0 +1,8 @@ +#if defined(__linux__) +#ifndef DISTRO_H +#define DISTRO_H + +void display_distro(); + +#endif +#endif 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 +} diff --git a/src/host.h b/src/host.h new file mode 100644 index 0000000..77b8ba5 --- /dev/null +++ b/src/host.h @@ -0,0 +1,6 @@ +#ifndef HOST_H +#define HOST_H + +void display_host_model(); + +#endif diff --git a/src/os.c b/src/os.c new file mode 100644 index 0000000..4662acd --- /dev/null +++ b/src/os.c @@ -0,0 +1,52 @@ +#include "os.h" + +#include <stdio.h> +#include <string.h> + +void display_os_name() { + char buf[64]; + FILE *p = popen("uname -s", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_os_vers() { + char buf[16]; + FILE *p = popen("uname -r", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_os_arch() { + char buf[16]; + FILE *p = popen("uname -m", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} diff --git a/src/os.h b/src/os.h new file mode 100644 index 0000000..690ed47 --- /dev/null +++ b/src/os.h @@ -0,0 +1,8 @@ +#ifndef OS_H +#define OS_H + +void display_os_name(); +void display_os_vers(); +void display_os_arch(); + +#endif diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..27a6b79 --- /dev/null +++ b/src/user.c @@ -0,0 +1,52 @@ +#include "user.h" + +#include <stdio.h> +#include <string.h> + +void display_user_name() { + char buf[128]; + FILE *p = popen("whoami", "r"); + if (!p) { + fprintf(stderr, "「whoami」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_user_host() { + char buf[64]; + const char *filename; +#ifdef __OpenBSD__ + filename = "/etc/myname"; +#else + filename = "/etc/hostname"; +#endif + FILE *f = fopen(filename, "r"); + if (!f) { + snprintf(buf, sizeof(buf), "「%s」を見つけられません。", filename); + perror(buf); + return; + } + + while (fgets(buf, sizeof(buf), f) != NULL) { + printf("%s", buf); + } + + /* char hostname[128]; */ + /* int cnt = 0; */ + /* for (int i = 0; i < 128; i++) { */ + /* unsigned char key; */ + /* fread(&key, sizeof(key), 1, f); */ + /* hostname[i] = key; */ + /* cnt += 1; */ + /* } */ + + /* hostname[cnt] = '\0'; */ + fclose(f); +} diff --git a/src/user.h b/src/user.h new file mode 100644 index 0000000..fc63853 --- /dev/null +++ b/src/user.h @@ -0,0 +1,7 @@ +#ifndef USER_H +#define USER_H + +void display_user_name(); +void display_user_host(); + +#endif |