From nobody Wed Nov 02 12:35:52 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4N2RFN49JQz4h4GT; Wed, 2 Nov 2022 12:35:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4N2RFN3DHlz43kg; Wed, 2 Nov 2022 12:35:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667392552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DRqQ82lARoRP6cmkQBN4laD5jAvYZzBlwYIwRzdcBn0=; b=TivcVqWMQ6+UW7eUvievi3cVQ2OOkNS7lObccHboGnn/ApRnTQEXxFLwXPF403rlmmfrxX nt0tYK6N9QFkjWhru+SH5bpR5c58x1x/Zz/SSgo37eeeKgcdWC1cU/1CZZfW1S1bXQsiNx sPlMp8wFdSAKWLQHxUhavozFAsFI+pWoefJ1Ubiw4kZuVzubERgl9XeQxRpwJIAVU28p7H KiBqX1Q+mBH0BU9fa5tAWPElPafVXZaXiXNFVI4npJRHZBl4T7i1Hwic1Yao+zRu6EnOOa KHJMxq7qkFZPXthm8XftIrOuxoTDHg1ZUhU66MqIAn9D1PQ/PV+UuAkzDxB+EQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4N2RFN1qTbzvrW; Wed, 2 Nov 2022 12:35:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2A2CZq72081253; Wed, 2 Nov 2022 12:35:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2A2CZqBS081252; Wed, 2 Nov 2022 12:35:52 GMT (envelope-from git) Date: Wed, 2 Nov 2022 12:35:52 GMT Message-Id: <202211021235.2A2CZqBS081252@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: 7302f8697124 - stable/13 - w: cosmetic fixes. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7302f8697124e5b3b24b03bbea5d0e0eaa69c6fc Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667392552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DRqQ82lARoRP6cmkQBN4laD5jAvYZzBlwYIwRzdcBn0=; b=hROe1zVhHl+6h81GtM+Mt/H7qmhE5F9Qhc1N2b1enoFvF4ct3TFMzgigBjhx/5yp63BozD 2pPawTg5necbeNuwX6ubzE2uHUzE8H9duZyM32JV4MvWkw1orPiyMNjWMboprX25r4/ixe kTtYMSrStfPU1brHBb/HDg2hZ1pE408qAzPCsY8zIXCMquA+BbkdHS2ZJrMOMXzv81a6TL mfht0iVho/uIO8l/wjSYZoHUSAojjo9kRat6fMlXkjdfR/nIHL92I1NGioc61VoZE3VJjm pBEMTYhYCoOUbPpQR9sfmSCmzIQC8sSU1zqShdP4g74aYuV2S9aDKdQ/Xxba/Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1667392552; a=rsa-sha256; cv=none; b=wmHctyRV3llJjbyT6JrTWEmY6C0I04tX0Rv6JUjLDLJIpP/MolWgfU8iOXAuermRZqaJDE rpCSxvrH4UIRYc1aXJGBp5rLw5NtHTCjfO2byxe6s1choxfU2QvFwD+p0sFUqXn6F1TGdP g7U+b/mCxAdxuBXUC5ctrXg3qqisLqGYl9Tjzk+w5Q3EFa5ueuBe6IpCjymlRhDM34Zxm3 3mSeboOpfJz/IdwYBhDBq8BWuc4e8wFb3eDN7d69Dn68IKQICMt4A1roknIB9gMldXCBnm y4lLD+5MpnKuWH8ObpySihH1BT/foLK7KQlZJA+4UXPptTkUjt6qBMKh9+HCOQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N 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 AuthorDate: 2022-10-21 16:37:44 +0000 Commit: Dag-Erling Smørgrav 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