From patchwork Mon Jan 20 09:46:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 11341601 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6EE9E924 for ; Mon, 20 Jan 2020 09:46:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 56AFE207E0 for ; Mon, 20 Jan 2020 09:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726642AbgATJqx (ORCPT ); Mon, 20 Jan 2020 04:46:53 -0500 Received: from helcar.hmeau.com ([216.24.177.18]:35516 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726039AbgATJqw (ORCPT ); Mon, 20 Jan 2020 04:46:52 -0500 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1itTdz-0007xm-H8; Mon, 20 Jan 2020 17:46:51 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1itTdw-0006HM-Oc; Mon, 20 Jan 2020 17:46:48 +0800 Date: Mon, 20 Jan 2020 17:46:48 +0800 From: Herbert Xu To: Michael Greenberg Cc: "dash@vger.kernel.org" Subject: [PATCH] builtin: Fix seconds part of times(1) Message-ID: <20200120094648.b23lt2izmg5cz34g@gondor.apana.org.au> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org On Mon, Jun 10, 2019 at 02:07:11PM +0000, Michael Greenberg wrote: > > If folks are opposed to including math.h for some reason, I'm sure the > computation could be done another way. Thanks for the patch. Yes I would like to avoid the libm dependency. How about something like this: ---8<--- The seconds part of the times(1) built-in is wrong as it does not exclude the minutes part of the result. This patch fixes it. This problem was first noted by Michael Greenberg who also sent a similar patch. Reported-by: Michael Greenberg Signed-off-by: Herbert Xu diff --git a/src/bltin/times.c b/src/bltin/times.c index 8eabc1f..1166a68 100644 --- a/src/bltin/times.c +++ b/src/bltin/times.c @@ -15,16 +15,28 @@ int timescmd() { struct tms buf; long int clk_tck = sysconf(_SC_CLK_TCK); + int mutime, mstime, mcutime, mcstime; + double utime, stime, cutime, cstime; times(&buf); - printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", - (int) (buf.tms_utime / clk_tck / 60), - ((double) buf.tms_utime) / clk_tck, - (int) (buf.tms_stime / clk_tck / 60), - ((double) buf.tms_stime) / clk_tck, - (int) (buf.tms_cutime / clk_tck / 60), - ((double) buf.tms_cutime) / clk_tck, - (int) (buf.tms_cstime / clk_tck / 60), - ((double) buf.tms_cstime) / clk_tck); + + utime = (double)buf.tms_utime / clk_tck; + mutime = utime / 60; + utime -= mutime * 60.0; + + stime = (double)buf.tms_stime / clk_tck; + mstime = stime / 60; + stime -= mstime * 60.0; + + cutime = (double)buf.tms_cutime / clk_tck; + mcutime = cutime / 60; + cutime -= mcutime * 60.0; + + cstime = (double)buf.tms_cstime / clk_tck; + mcstime = cstime / 60; + cstime -= mcstime * 60.0; + + printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", mutime, utime, mstime, stime, + mcutime, cutime, mcstime, cstime); return 0; }