From patchwork Mon Jun 10 14:07:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Greenberg X-Patchwork-Id: 10985007 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BB9426C5 for ; Mon, 10 Jun 2019 14:07:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A8F4528521 for ; Mon, 10 Jun 2019 14:07:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9D6BD28574; Mon, 10 Jun 2019 14:07:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8CE232856B for ; Mon, 10 Jun 2019 14:07:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390660AbfFJOHP (ORCPT ); Mon, 10 Jun 2019 10:07:15 -0400 Received: from mail-eopbgr750042.outbound.protection.outlook.com ([40.107.75.42]:32320 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2390384AbfFJOHP (ORCPT ); Mon, 10 Jun 2019 10:07:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mymailpomona.onmicrosoft.com; s=selector1-mymailpomona-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=q4L3CEDcZGYUOHEIgVW4xNsGuKnGkTrLV0NP6mqNb4A=; b=ZwyenGDC2XToKBX85EfQEZJt/QmOWKK16pzbqmqfKRqco0sddlpLAzhOKu5hLk63Fj1oiJ3RvvpvVvANKeYtJLU248LUifRe1Ag1hli/yhREH1QSQYTtxmE++kN38B1GxQmEVpJWLeJyhM54mFgGwGd10R4uTTOpqETUp41CKNE= Received: from CY4PR03MB3080.namprd03.prod.outlook.com (10.171.245.145) by CY4PR03MB3320.namprd03.prod.outlook.com (10.171.247.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1965.12; Mon, 10 Jun 2019 14:07:12 +0000 Received: from CY4PR03MB3080.namprd03.prod.outlook.com ([fe80::a923:d552:164c:db8d]) by CY4PR03MB3080.namprd03.prod.outlook.com ([fe80::a923:d552:164c:db8d%4]) with mapi id 15.20.1965.017; Mon, 10 Jun 2019 14:07:12 +0000 From: Michael Greenberg To: "dash@vger.kernel.org" Subject: [PATCH] Bug in times builtin Thread-Topic: [PATCH] Bug in times builtin Thread-Index: AQHVH5XMwkTjM6J59E6E1vcN25PCUQ== Date: Mon, 10 Jun 2019 14:07:11 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MN2PR04CA0023.namprd04.prod.outlook.com (2603:10b6:208:d4::36) To CY4PR03MB3080.namprd03.prod.outlook.com (2603:10b6:910:53::17) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Michael.Greenberg@pomona.edu; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [73.68.184.48] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ed648a22-118c-49d7-e76c-08d6edaceec2 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328)(7193020);SRVR:CY4PR03MB3320; x-ms-traffictypediagnostic: CY4PR03MB3320: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:1122; x-forefront-prvs: 0064B3273C x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(136003)(366004)(376002)(346002)(396003)(39850400004)(53754006)(199004)(189003)(256004)(486006)(14454004)(68736007)(2616005)(66066001)(476003)(26005)(186003)(72206003)(478600001)(44832011)(71200400001)(52116002)(71190400001)(6486002)(36756003)(6436002)(99286004)(2351001)(25786009)(88552002)(6512007)(786003)(316002)(5640700003)(66476007)(66446008)(64756008)(66946007)(73956011)(66556008)(5660300002)(2501003)(7736002)(6916009)(102836004)(386003)(75432002)(1730700003)(81166006)(8936002)(305945005)(8676002)(86362001)(81156014)(2906002)(6116002)(3846002)(6506007)(53936002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR03MB3320;H:CY4PR03MB3080.namprd03.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: pomona.edu does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: xBhTT94fFYuvY8bdUSUFrbsfxe2ngm+37MWQcnWtcIGVSb0Rhg3FRpYuU1sgaTCk0nCzaR1VsotIY6cbdikl6p0G7KtLq+RSl9JQUGpI40bcu5PfFVKX3jY0fhz3+7VumwB3x/PVzjLlUu6DkdyyZ4WhiYKoxkhcvNl88xrCeeE3aU4S0LHbgzguWjQY/u9yVNtGlJ++59XV4kf/ZI2CFwbLygf9nsbVZehWTxT78mb1gx7poInz4za7Gf/Khcvq1di/HWCPabs/KUrkeMwQYbLdR6lk+teHJ5wYlhGyRIib1O73aASybDxbkNvLC99JfgYh5Y+pLP5jrmrylJlCbiNzHLXh8dUVYt9n0k7M+YUKNUCpDnt95rqFa0qomagT1GVD1rKdeNw9XymfvdSAhX9JOcyudqbRa0YsLfBVLSQ= MIME-Version: 1.0 X-OriginatorOrg: pomona.edu X-MS-Exchange-CrossTenant-Network-Message-Id: ed648a22-118c-49d7-e76c-08d6edaceec2 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Jun 2019 14:07:12.0043 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 817f5904-3904-4ee8-b3a5-a65d4746ff70 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: mgeb2015@pomona.edu X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR03MB3320 Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Hi all, The `times` builtin in dash fails to compute the seconds part properly. To see the bug, I ran the following script: ```sh delay() { case "$#" in ( 0 ) secs=1 ;; ( 1 ) secs=$(($1+0)) ;; ( * ) exit 2 ;; esac start=$(date "+%s") while now=$(date "+%s") ; [ $((now - start)) -lt "$secs" ] do : done } times delay $((5 * 60)) times ``` The script implements a 5min sleep. An unpatched dash produces the following output: ``` 0m0.000000s 0m0.000000s 0m0.000000s 0m0.000000s 0m8.330000s 0m26.690000s 1m97.670000s 1m107.150000s ``` With the patch, the seconds are computed correctly (NB that this is a different run so times vary slightly): ``` 0m0.000000s 0m0.000000s 0m0.000000s 0m0.000000s 0m8.260000s 0m27.050000s 1m38.130000s 1m45.960000s ``` If folks are opposed to including math.h for some reason, I'm sure the computation could be done another way. Cheers, Michael Signed-off-by: Michael Greenberg diff --git a/src/bltin/times.c b/src/bltin/times.c index 8eabc1f..7e66f90 100644 --- a/src/bltin/times.c +++ b/src/bltin/times.c @@ -3,6 +3,7 @@ * This file contains code for the times builtin. */ +#include #include #include #ifdef USE_GLIBC_STDIO @@ -19,12 +20,12 @@ int timescmd() { 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, + fmod(((double) buf.tms_utime) / clk_tck, 60), (int) (buf.tms_stime / clk_tck / 60), - ((double) buf.tms_stime) / clk_tck, + fmod(((double) buf.tms_stime) / clk_tck, 60), (int) (buf.tms_cutime / clk_tck / 60), - ((double) buf.tms_cutime) / clk_tck, + fmod(((double) buf.tms_cutime) / clk_tck, 60), (int) (buf.tms_cstime / clk_tck / 60), - ((double) buf.tms_cstime) / clk_tck); + fmod(((double) buf.tms_cstime) / clk_tck, 60)); return 0; }