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 --- src/uptime.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/uptime.c (limited to 'src/uptime.c') 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\""); +} -- cgit v1.2.3