From nobody Wed Oct 26 15:56:49 2022 X-Original-To: dev-commits-src-main@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 4MyD2T3Fp6z4gVVZ; Wed, 26 Oct 2022 15:56:49 +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 4MyD2T2ZWgz3S2T; Wed, 26 Oct 2022 15:56:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666799809; 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=g+MC86ZucSH3drChpqwjRNFYAR44DZzzGIuDWb8D6VM=; b=RS4oGsgaxRWWAEB2rTSnIrTOt93FH5Bb2xZa9TBA/nGUf5HaHdOqPNEbJWTMIKTNcRN/yM r2/laaprqyw7T89aXwUaamJRAFEtvvxvUN0Tw50glXPx4VZ8l0X7Nswm8UI3CJZX+1ApPN KFS/ph4vqklym4ef8JuWj1P3b7Ef6rWUtOA+j3fBPgdPdgOHpTFbeJ5z1oxEdy+l138Wl2 dY2z5kOq8/8i4yr1wtaJBy3zyWTeTvOuRlbOjB8/XVmCOmKNZ5WumFctR0mx79tthlh+Jm Ekh1Td0WhJzzc3WhXytMKkoHC0p1dy252P/W0bL+dcDcnuGBUBI3MeBtfX9V+w== 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 4MyD2T1QkszNc5; Wed, 26 Oct 2022 15:56:49 +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 29QFunux012420; Wed, 26 Oct 2022 15:56:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29QFunnf012419; Wed, 26 Oct 2022 15:56:49 GMT (envelope-from git) Date: Wed, 26 Oct 2022 15:56:49 GMT Message-Id: <202210261556.29QFunnf012419@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: deb2f1b616f1 - main - w: Fix premature rounding. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: deb2f1b616f1369d2e773096a6c0c302cb3a9060 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666799809; 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=g+MC86ZucSH3drChpqwjRNFYAR44DZzzGIuDWb8D6VM=; b=HDj3nS16rwn/ljY5OpLicpqMU/s7AuXegfoNrqJK0j9D18ErmYULP5ZiE1ZNfSct3A2dJJ hu9LcJWJKsx+031cHfuabgSOyFtz7n15uWEkxCTgNbG1YY3vHt1GO/Z5NehowLUq2+gZsn irPxsv8r29b7y1lx9KuSWtDHz9+7IKZpP/0kgrVV9IjqhTWUONsS11z9M5cE+GwNAV9hQG M9KK30bhP8DjhIP68rVmK1xMcV8/OovMXCjLvZ2vnsheZBDIJFT9BNvXzJ/AHEGjz3QDoj x6LvfYk+/QTlV5ziSm6xNjrAmsoi+3rvWoxhGHj3TBPNs5zrBAxch92caPkDZw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666799809; a=rsa-sha256; cv=none; b=c1NyTGyEpczblHzZkyn+jcb+i5j7Uxry6B15f2t4yhRy1Y/lA9RyKAhV5/cIoiessRnLVR tFyi6+Sgjuvq1qVyqiUrikUcSdJFEoObgjMiNm2OCuatNDZAo/o5T3h15LUA/yXwSDNwng 3fL3Xe7e/RJ0r9bLWhJktlnV88WXeJn6N0KKa6BKfIUXLrQecmqTfUYzwwEvxElN2/0Yo2 hOsgRs+NP5qAeGqevOTddL5aSPYoWQhXQ+BLaNCqOQT8zfJ4FCaED9rq9SaJJU7favInIq OMgB7MHiT6+KPKP8M75OL/quLICreBHHG1Bv55Kyvw+yjZIEP1Vmmay2zd4JOg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=deb2f1b616f1369d2e773096a6c0c302cb3a9060 commit deb2f1b616f1369d2e773096a6c0c302cb3a9060 Author: Dag-Erling Smørgrav AuthorDate: 2022-10-24 14:02:11 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2022-10-26 15:56:40 +0000 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 --- usr.bin/w/w.c | 59 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index 4199e64e90f6..45e624c29460 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -481,14 +481,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. */ @@ -498,39 +497,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}", + 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,", + 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 */ @@ -548,7 +557,7 @@ pr_header(time_t *nowp, int nusers) " {: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]);