git: 5572a45c0f61 - stable/14 - w: Trim whitespace and commas from time and uptime

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 28 Oct 2025 11:09:07 UTC
The branch stable/14 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=5572a45c0f6122477e218e54a4b88e63563f3359

commit 5572a45c0f6122477e218e54a4b88e63563f3359
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-10-23 10:28:44 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-10-28 11:08:14 +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.
    
    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)
---
 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 69ff8c33cbbf..69387f44449a 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -481,7 +481,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;
@@ -492,8 +492,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.
 	 */
@@ -524,21 +531,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);
 	}