summaryrefslogtreecommitdiffstats
path: root/login-utils/last.c
diff options
context:
space:
mode:
authorcoastal-hiker2017-06-26 18:40:28 +0200
committerKarel Zak2017-07-10 10:02:38 +0200
commite1787b1a85f25b97f9ea81ab40112a6257b3ab19 (patch)
tree41a5e58f4d01ab10deee8007025e2c68564ba1b6 /login-utils/last.c
parentlogin: use IPv4 on IPv4-mapping-to-IPv6 (diff)
downloadkernel-qcow2-util-linux-e1787b1a85f25b97f9ea81ab40112a6257b3ab19.tar.gz
kernel-qcow2-util-linux-e1787b1a85f25b97f9ea81ab40112a6257b3ab19.tar.xz
kernel-qcow2-util-linux-e1787b1a85f25b97f9ea81ab40112a6257b3ab19.zip
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 <kzak@redhat.com>
Diffstat (limited to 'login-utils/last.c')
-rw-r--r--login-utils/last.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/login-utils/last.c b/login-utils/last.c
index 1218a3018..ad70b38fb 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) {