From 34395be330484999874f4478957e2dd143473fe7 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Fri, 4 Sep 2015 00:00:00 -0500 Subject: [PATCH] library: suppress zero 'days' under the API The former whattime logic used to suppress that 'days' output when the system had been up less than 24 hours. But since the refactor for newlib, '0 days' was always output under those conditions. So this commit restores the former expected behavior of suppressing that item. [ plus an erroneous calculation of uphours was fixed ] [ and the clang warnings shown below were also fixed ] Reference(s): proc/uptime.c:74:10: warning: unused variable 'buf' [-Wunused-variable] char buf[256]; ^ proc/uptime.c:131:58: warning: data argument not used by format string [-Wformat-extra-args] pos += sprintf(upbuf + pos, "%d min, ", uphours, upminutes); ~~~~~~~~~~ ^ proc/uptime.c:175:15: warning: expression result unused [-Wunused-value] comma +1; ~~~~~ ^~ Signed-off-by: Jim Warner --- proc/uptime.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/proc/uptime.c b/proc/uptime.c index 02bdf1fe..ca571b9d 100644 --- a/proc/uptime.c +++ b/proc/uptime.c @@ -71,7 +71,6 @@ PROCPS_EXPORT int procps_uptime( { double up=0, idle=0; char *savelocale; - char buf[256]; FILE *fp; if ((fp = fopen(UPTIME_FILE, "r")) == NULL) @@ -122,19 +121,22 @@ PROCPS_EXPORT char *procps_uptime_sprint(void) uphours = ((int) uptime_secs / (60*60)) % 24; upminutes = ((int) uptime_secs / (60)) % 60; - pos = sprintf(upbuf, " %02d:%02d:%02d up %d %s, ", - realtime->tm_hour, realtime->tm_min, realtime->tm_sec, - updays, (updays != 1) ? "days" : "day"); + pos = sprintf(upbuf, " %02d:%02d:%02d up ", + realtime->tm_hour, realtime->tm_min, realtime->tm_sec); + + if (updays) + pos += sprintf(upbuf + pos, "%d %s", updays, (updays > 1) ? "days" : "day"); + if (uphours) pos += sprintf(upbuf + pos, "%2d:%02d, ", uphours, upminutes); else - pos += sprintf(upbuf + pos, "%d min, ", uphours, upminutes); + pos += sprintf(upbuf + pos, "%d min, ", upminutes); users = count_users(); procps_loadavg(&av1, &av5, &av15); - pos += sprintf(upbuf + pos, "%2d user%s, load average: %.2f, %.2f, %.2f", - users, users == 1 ? "" : "s", + pos += sprintf(upbuf + pos, "%2d %s, load average: %.2f, %.2f, %.2f", + users, users > 1 ? "users" : "user", av1, av5, av15); return upbuf; @@ -164,7 +166,7 @@ PROCPS_EXPORT char *procps_uptime_sprint_short(void) upyears = ((int) uptime_secs / (60*60*24*365)) % 10; upweeks = ((int) uptime_secs / (60*60*24*7)) % 52; updays = ((int) uptime_secs / (60*60*24)) % 7; - uphours = ((int) uptime_secs / (60*24)) % 24; + uphours = ((int) uptime_secs / (60*60)) % 24; upminutes = ((int) uptime_secs / (60)) % 60; strcat(shortbuf, "up "); @@ -172,7 +174,7 @@ PROCPS_EXPORT char *procps_uptime_sprint_short(void) if (updecades) { pos += sprintf(shortbuf + pos, "%d %s", updecades, updecades > 1 ? "decades" : "decade"); - comma +1; + comma += 1; } if (upyears) { -- 2.40.0