From 7a392b2a6351e527789fa7718505b14afb4b224a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Thu, 20 Jun 2024 00:47:11 +0900 Subject: =?UTF-8?q?=E8=B5=B7=E5=8B=95=E6=99=82=E9=96=93=E3=81=A70=E6=97=A5?= =?UTF-8?q?=E3=82=82=E5=8F=AF=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/uptime.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'src/uptime.c') diff --git a/src/uptime.c b/src/uptime.c index 56ea6b7..269cdda 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -2,12 +2,40 @@ #include "common.h" #include +#include +#include -void display_days() { - printf("%s", run_command_s("uptime | awk '{print $3}' && echo \" days\"")); +const char *display_days() { + const char *days = run_command_s("uptime | awk '{for (i=1; i<=NF; i++) " + "if ($i ~ /day/) print $(i-1), $i}'"); + + if (days == NULL || strlen(days) == 0) { + return strndup("0 days,", 7); + } + + return days; } -void display_time() { - printf("%s", run_command_s("uptime | awk '{print $5}' | sed 's/,//' | " - "sed 's/:/ hours, /' && echo \" mins\"")); +const char *display_time() { + const char *uptime = run_command_s("uptime"); + if (uptime == NULL) { + return NULL; + } + + char *days = strstr(uptime, "day"); + int nodays = 0; + if (days == NULL) { + nodays = 1; + days = strstr(uptime, "days"); + } + + free((void *)uptime); + + if (nodays) { + return run_command_s("uptime | awk '{print $3}' | sed 's/,//' | " + "sed 's/:/ hours, /' && echo \" mins\""); + } + + return run_command_s("uptime | awk '{print $5}' | sed 's/,//' | " + "sed 's/:/ hours, /' && echo \" mins\""); } -- cgit v1.2.3