summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/uptime.c38
-rw-r--r--src/uptime.h4
2 files changed, 35 insertions, 7 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
-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\"");
}
diff --git a/src/uptime.h b/src/uptime.h
index 12ee668..92ba7ad 100644
--- a/src/uptime.h
+++ b/src/uptime.h
@@ -1,7 +1,7 @@
#ifndef UPTIME_H
#define UPTIME_H
-void display_days();
-void display_time();
+const char *display_days();
+const char *display_time();
#endif