git: 8cf76cbac851 - releng/15.0 - w: Trim whitespace and commas from time and uptime

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Thu, 30 Oct 2025 23:52:29 UTC
The branch releng/15.0 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=8cf76cbac85119fa8d081fdc9b7cbfd1b6728071

commit 8cf76cbac85119fa8d081fdc9b7cbfd1b6728071
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-10-23 10:28:44 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-30 23:48:28 +0000

    w: Trim whitespace and commas from time and uptime
    
    When producing formatted output, trim leading whitespace and trailing
    commas from the human-readable time and uptime before emitting them.
    The text output remains unchanged.
    
    Approved by:    re (cperciva)
    PR:             290089
    Fixes:          6e6febb54da9 ("w: Fix idle time in json output, add login/idle times to json output")
    Reviewed by:    marius.h_lden.org
    Differential Revision:  https://reviews.freebsd.org/D53167
    
    (cherry picked from commit 4d5789532a940144c869d66505e756ce816f8a50)
    (cherry picked from commit ca16a115a0fa4da67b153d0b90c9ec08adc9b2fb)
---
 usr.bin/w/w.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index ac1df96077d3..502bf5a412b9 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -473,7 +473,7 @@ main(int argc, char *argv[])
 static void
 pr_header(time_t *nowp, int nusers)
 {
-	char buf[64];
+	char buf[64], *s, *e;
 	struct sbuf upbuf;
 	double avenrun[3];
 	struct timespec tp;
@@ -484,8 +484,15 @@ pr_header(time_t *nowp, int nusers)
 	 * Print time of day.
 	 */
 	if (strftime(buf, sizeof(buf),
-	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
-		xo_emit("{:time-of-day/%s} ", buf);
+	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0) {
+		s = buf;
+		if (xo_get_style(NULL) != XO_STYLE_TEXT) {
+			/* trim leading whitespace */
+			while (isspace((unsigned char)*s))
+				s++;
+		}
+		xo_emit("{:time-of-day/%s} ", s);
+	}
 	/*
 	 * Print how long system has been up.
 	 */
@@ -516,21 +523,31 @@ pr_header(time_t *nowp, int nusers)
 
 		if (days > 0)
 			sbuf_printf(&upbuf, " %ld day%s,",
-				days, days > 1 ? "s" : "");
+			    days, days > 1 ? "s" : "");
 		if (hrs > 0 && mins > 0)
 			sbuf_printf(&upbuf, " %2ld:%02ld,", hrs, mins);
 		else if (hrs > 0)
 			sbuf_printf(&upbuf, " %ld hr%s,",
-				hrs, hrs > 1 ? "s" : "");
+			    hrs, hrs > 1 ? "s" : "");
 		else if (mins > 0)
 			sbuf_printf(&upbuf, " %ld min%s,",
-				mins, mins > 1 ? "s" : "");
+			    mins, mins > 1 ? "s" : "");
 		else
 			sbuf_printf(&upbuf, " %ld sec%s,",
-				secs, secs > 1 ? "s" : "");
+			    secs, secs > 1 ? "s" : "");
 		if (sbuf_finish(&upbuf) != 0)
 			xo_err(1, "Could not generate output");
-		xo_emit("{:uptime-human/%s}", sbuf_data(&upbuf));
+		s = sbuf_data(&upbuf);
+		if (xo_get_style(NULL) != XO_STYLE_TEXT) {
+			e = s + sbuf_len(&upbuf) - 1;
+			/* trim leading whitespace */
+			while (isspace((unsigned char)*s))
+				s++;
+			/* trim trailing comma */
+			if (e > s && *e == ',')
+				*e = '\0';
+		}
+		xo_emit("{:uptime-human/%s}", s);
 		sbuf_delete(&upbuf);
 	}