From e1787b1a85f25b97f9ea81ab40112a6257b3ab19 Mon Sep 17 00:00:00 2001 From: coastal-hiker Date: Mon, 26 Jun 2017 18:40:28 +0200 Subject: [PATCH] last: don't show negative time Under strange circumstances, the output of command 'last reboot' showed the last time as a negative time, with both the hours and the mins value having a minus sign. Example, taken from my workstation: $last reboot [...] reboot system boot 4.4.0-79-generic Wed Jun 14 09:20 - 07:33 (-1:-47) [...] I am aware this should happen only infrequently. Nevertheless, I propose a more robust behaviour: show a minus sign only for the most significant value (days or hours) and show the rest always as positive. In the special case of ((secs < 0) && (secs >= -59)), print mins as "-00". Signed-off-by: Karel Zak --- login-utils/last.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/login-utils/last.c b/login-utils/last.c index 1218a3018d..ad70b38fb1 100644 --- a/login-utils/last.c +++ b/login-utils/last.c @@ -435,7 +435,7 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t errx(EXIT_FAILURE, _("preallocation size exceeded")); /* log-out time */ - secs = logout_time - utmp_time; + secs = logout_time - utmp_time; /* Under strange circumstances, secs < 0 can happen */ mins = (secs / 60) % 60; hours = (secs / 3600) % 24; days = secs / 86400; @@ -454,9 +454,13 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t sprintf(length, "running"); } } else if (days) { - sprintf(length, "(%d+%02d:%02d)", days, hours, mins); + sprintf(length, "(%d+%02d:%02d)", days, abs(hours), abs(mins)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */ + } else if (hours) { + sprintf(length, " (%02d:%02d)", hours, abs(mins)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */ + } else if (secs > 0) { + sprintf(length, " (%02d:%02d)", hours, mins); } else { - sprintf(length, " (%02d:%02d)", hours, mins); + sprintf(length, " (-00:%02d)", abs(mins)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */ } switch(what) { -- 2.47.2