Date: Wed, 2 Nov 2022 12:35:52 GMT From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= <des@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 7302f8697124 - stable/13 - w: cosmetic fixes. Message-ID: <202211021235.2A2CZqBS081252@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=7302f8697124e5b3b24b03bbea5d0e0eaa69c6fc commit 7302f8697124e5b3b24b03bbea5d0e0eaa69c6fc Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2022-10-21 16:37:44 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2022-11-02 09:41:07 +0000 w: cosmetic fixes. Sponsored by: Klara, Inc. (cherry picked from commit 04f6b9cb1822fdd6ab1cbeeafb38a6b354b61ca7) w: Fix premature rounding. If the system has been up more longer than a minute, we add 30 seconds to the uptime so that subsequent calculations will round to the nearest minute rather than truncate. However, since the introduction of libxo, we output the raw value after performing the adjustment. Rewrite so that we output the raw value first, then perform the adjustment and recalculate before outputting the humanized value. While there, reduce stack usage and avoid needless allocations. Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D37128 (cherry picked from commit deb2f1b616f1369d2e773096a6c0c302cb3a9060) --- usr.bin/w/pr_time.c | 2 +- usr.bin/w/w.c | 106 ++++++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/usr.bin/w/pr_time.c b/usr.bin/w/pr_time.c index 13f3180d511b..20b75704c309 100644 --- a/usr.bin/w/pr_time.c +++ b/usr.bin/w/pr_time.c @@ -90,7 +90,7 @@ pr_attime(time_t *started, time_t *now) if (len == width) xo_emit("{:login-time/%-7.7ls/%ls}", buf); else if (width < 7) - xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, " "); + xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, " "); else { xo_emit("{:login-time/%ls}", buf); offset = width - 7; diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index 916a77920160..b06d605438ca 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -95,9 +95,9 @@ static struct utmpx *utmp; static struct winsize ws; static kvm_t *kd; static time_t now; /* the current time of day */ -static int ttywidth; /* width of tty */ -static int fromwidth = 0; /* max width of "from" field */ -static int argwidth; /* width of arguments */ +static size_t ttywidth; /* width of tty */ +static size_t fromwidth = 0; /* max width of "from" field */ +static size_t argwidth; /* width of arguments */ static int header = 1; /* true if -h flag: don't print heading */ static int nflag; /* true if -n flag: don't convert addrs */ static int dflag; /* true if -d flag: output debug info */ @@ -140,11 +140,12 @@ main(int argc, char *argv[]) struct kinfo_proc *dkp; struct stat *stp; time_t touched; + size_t width; int ch, i, nentries, nusers, wcmd, longidle, longattime; const char *memf, *nlistf, *p, *save_p; char *x_suffix; - char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; - char fn[MAXHOSTNAMELEN]; + char errbuf[_POSIX2_LINE_MAX]; + char buf[MAXHOSTNAMELEN], fn[MAXHOSTNAMELEN]; char *dot; (void)setlocale(LC_ALL, ""); @@ -188,7 +189,7 @@ main(int argc, char *argv[]) nflag += 1; break; case 'f': case 'l': case 's': case 'u': case 'w': - warnx("[-flsuw] no longer supported"); + warnx("-%c no longer supported", ch); /* FALLTHROUGH */ case '?': default: @@ -310,8 +311,8 @@ main(int argc, char *argv[]) p = buf; } ep->from = strdup(p); - if ((i = strlen(p)) > fromwidth) - fromwidth = i; + if ((width = strlen(p)) > fromwidth) + fromwidth = width; if (save_p != p) ep->save_from = strdup(save_p); } @@ -323,10 +324,9 @@ main(int argc, char *argv[]) #define HEADER_LOGIN_IDLE "LOGIN@ IDLE " #define HEADER_WHAT "WHAT\n" #define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + fromwidth + \ - sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ + sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ - - if ((int) sizeof(HEADER_FROM) > fromwidth) + if (sizeof(HEADER_FROM) > fromwidth) fromwidth = sizeof(HEADER_FROM); fromwidth++; if (fromwidth > W_MAXHOSTSIZE) @@ -339,12 +339,11 @@ main(int argc, char *argv[]) if (wcmd == 0) { xo_close_container("uptime-information"); xo_finish(); - (void)kvm_close(kd); exit(0); } - xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}", + xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}", W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER, W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY, fromwidth, fromwidth, HEADER_FROM, @@ -427,8 +426,8 @@ main(int argc, char *argv[]) xo_open_instance("user-entry"); if (dflag) { - xo_open_container("process-table"); - xo_open_list("process-entry"); + xo_open_container("process-table"); + xo_open_list("process-entry"); for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) { const char *ptr; @@ -442,8 +441,8 @@ main(int argc, char *argv[]) "{:command/%hs}\n", dkp->ki_pid, ptr); xo_close_instance("process-entry"); } - xo_close_list("process-entry"); - xo_close_container("process-table"); + xo_close_list("process-entry"); + xo_close_container("process-table"); } xo_emit("{:user/%-*.*s/%@**@s} {:tty/%-*.*s/%@**@s} ", W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user, @@ -456,12 +455,12 @@ main(int argc, char *argv[]) if (ep->save_from) xo_attr("address", "%s", ep->save_from); xo_emit("{:from/%-*.*s/%@**@s} ", - fromwidth, fromwidth, ep->from); + (int)fromwidth, (int)fromwidth, ep->from); t = ep->utmp.ut_tv.tv_sec; longattime = pr_attime(&t, &now); longidle = pr_idle(ep->idle); xo_emit("{:command/%.*hs/%@*@hs}\n", - argwidth - longidle - longattime, + (int)argwidth - longidle - longattime, ep->args); xo_close_instance("user-entry"); @@ -479,14 +478,13 @@ main(int argc, char *argv[]) static void pr_header(time_t *nowp, int nusers) { + char buf[64]; + struct sbuf upbuf; double avenrun[3]; - time_t uptime; struct timespec tp; - int days, hrs, i, mins, secs; - char buf[256]; - struct sbuf *upbuf; + unsigned long days, hrs, mins, secs; + unsigned int i; - upbuf = sbuf_new_auto(); /* * Print time of day. */ @@ -496,38 +494,49 @@ pr_header(time_t *nowp, int nusers) /* * Print how long system has been up. */ + (void)sbuf_new(&upbuf, buf, sizeof(buf), SBUF_FIXEDLEN); if (clock_gettime(CLOCK_UPTIME, &tp) != -1) { - uptime = tp.tv_sec; - if (uptime > 60) - uptime += 30; - days = uptime / 86400; - uptime %= 86400; - hrs = uptime / 3600; - uptime %= 3600; - mins = uptime / 60; - secs = uptime % 60; xo_emit(" up"); - xo_emit("{e:uptime/%lu}", (unsigned long) tp.tv_sec); - xo_emit("{e:days/%d}{e:hours/%d}{e:minutes/%d}{e:seconds/%d}", days, hrs, mins, secs); + secs = tp.tv_sec; + xo_emit("{e:uptime/%lu}", secs); + mins = secs / 60; + secs %= 60; + hrs = mins / 60; + mins %= 60; + days = hrs / 24; + hrs %= 24; + xo_emit("{e:days/%ld}{e:hours/%ld}{e:minutes/%ld}{e:seconds/%ld}", + days, hrs, mins, secs); + + /* If we've been up longer than 60 s, round to nearest min */ + if (tp.tv_sec > 60) { + secs = tp.tv_sec + 30; + mins = secs / 60; + secs = 0; + hrs = mins / 60; + mins %= 60; + days = hrs / 24; + hrs %= 24; + } if (days > 0) - sbuf_printf(upbuf, " %d day%s,", + sbuf_printf(&upbuf, " %ld day%s,", days, days > 1 ? "s" : ""); if (hrs > 0 && mins > 0) - sbuf_printf(upbuf, " %2d:%02d,", hrs, mins); + sbuf_printf(&upbuf, " %2ld:%02ld,", hrs, mins); else if (hrs > 0) - sbuf_printf(upbuf, " %d hr%s,", + sbuf_printf(&upbuf, " %ld hr%s,", hrs, hrs > 1 ? "s" : ""); else if (mins > 0) - sbuf_printf(upbuf, " %d min%s,", + sbuf_printf(&upbuf, " %ld min%s,", mins, mins > 1 ? "s" : ""); - else - sbuf_printf(upbuf, " %d sec%s,", + else + sbuf_printf(&upbuf, " %ld sec%s,", secs, secs > 1 ? "s" : ""); - if (sbuf_finish(upbuf) != 0) + if (sbuf_finish(&upbuf) != 0) xo_err(1, "Could not generate output"); - xo_emit("{:uptime-human/%s}", sbuf_data(upbuf)); - sbuf_delete(upbuf); + xo_emit("{:uptime-human/%s}", sbuf_data(&upbuf)); + sbuf_delete(&upbuf); } /* Print number of users logged in to system */ @@ -539,13 +548,13 @@ pr_header(time_t *nowp, int nusers) if (getloadavg(avenrun, nitems(avenrun)) == -1) xo_emit(", no load average information available\n"); else { - static const char *format[] = { + static const char *format[] = { " {:load-average-1/%.2f}", " {:load-average-5/%.2f}", " {:load-average-15/%.2f}", }; xo_emit(", load averages:"); - for (i = 0; i < (int)(nitems(avenrun)); i++) { + for (i = 0; i < nitems(avenrun); i++) { if (use_comma && i > 0) xo_emit(","); xo_emit(format[i], avenrun[i]); @@ -561,10 +570,9 @@ ttystat(char *line) char ttybuf[MAXPATHLEN]; (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line); - if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) { + if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) return (&sb); - } else - return (NULL); + return (NULL); } static void
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202211021235.2A2CZqBS081252>