From 7effecbcd3fd8869d32448d6ae6b5f1ccb0efe08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Tue, 18 Jun 2024 13:39:02 +0900 Subject: =?UTF-8?q?=E8=B5=B7=E5=8B=95=E6=99=82=E9=96=93=E3=81=A8CPU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 15 ++++++++++++--- src/cpu.c | 35 +++++++++++++++++++++++++++++++++++ src/cpu.h | 6 ++++++ src/uptime.c | 31 +++++++++++++++++++++++++++++++ src/uptime.h | 7 +++++++ 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/cpu.c create mode 100644 src/cpu.h create mode 100644 src/uptime.c create mode 100644 src/uptime.h diff --git a/main.c b/main.c index b1aede5..3cb7faf 100644 --- a/main.c +++ b/main.c @@ -5,9 +5,11 @@ #include "src/user.h" #include "src/os.h" #include "src/host.h" +#include "src/uptime.h" #if defined(__linux__) #include "src/distro.h" #endif +#include "src/cpu.h" #include "src/memory.h" const char *sofname = "farfetch"; @@ -37,20 +39,27 @@ int main() { display_host_model(); printf("\n"); + printf("Uptime: "); + display_days(); + printf(", "); + display_time(); + printf("\n"); + + printf("CPU: "); + display_cpu(); + printf("\n"); + printf("Memory: "); display_memory(); printf("\n"); // TODO: // * ロゴ - // * カーネル(LinuxとIllumosのみ) - // * 起動時間 // * パッケージ // * libc // * シェル // * 解像度 // * 端末 - // * CPU // * GPU // * ストレージ return 0; diff --git a/src/cpu.c b/src/cpu.c new file mode 100644 index 0000000..9b79eca --- /dev/null +++ b/src/cpu.c @@ -0,0 +1,35 @@ +#include "memory.h" + +#include +#include +#include + +void run_cpu_command(const char *command) { + char buf[128]; + + 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_cpu() { +#if defined(__NetBSD__) + run_cpu_command("sysctl -n machdep.cpu_brand | sed 's/(R)//' | " + "sed 's/(TM)//' | sed 's/CPU //'"); + run_cpu_command("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\""); +#elif defined(__FreeBSD__) || defined(__OpenBSD__) + run_cpu_command("sysctl -n hw.model | sed 's/(R)//' | " + "sed 's/(TM)//' | sed 's/CPU //'"); + run_cpu_command("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\""); +#elif defined(__linux__) +#endif +} diff --git a/src/cpu.h b/src/cpu.h new file mode 100644 index 0000000..89a810a --- /dev/null +++ b/src/cpu.h @@ -0,0 +1,6 @@ +#ifndef CPU_H +#define CPU_H + +void display_cpu(); + +#endif diff --git a/src/uptime.c b/src/uptime.c new file mode 100644 index 0000000..da19270 --- /dev/null +++ b/src/uptime.c @@ -0,0 +1,31 @@ +#include "memory.h" + +#include +#include +#include + +void run_uptime_command(const char *command) { + char buf[128]; + + 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_days() { + run_uptime_command("uptime | awk '{print $3}' && echo \" days\""); +} + +void display_time() { + run_uptime_command("uptime | awk '{print $5}' | sed 's/,//' | " + "sed 's/:/ hours, /' && echo \" mins\""); +} diff --git a/src/uptime.h b/src/uptime.h new file mode 100644 index 0000000..12ee668 --- /dev/null +++ b/src/uptime.h @@ -0,0 +1,7 @@ +#ifndef UPTIME_H +#define UPTIME_H + +void display_days(); +void display_time(); + +#endif -- cgit v1.2.3