dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Greenberg <Michael.Greenberg@pomona.edu>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "dash@vger.kernel.org" <dash@vger.kernel.org>
Subject: Re: [PATCH] builtin: Fix seconds part of times(1)
Date: Mon, 20 Jan 2020 15:09:08 +0000	[thread overview]
Message-ID: <m2lfq26u7h.fsf@pomona.edu> (raw)
In-Reply-To: <20200120094648.b23lt2izmg5cz34g@gondor.apana.org.au>

Looks great! Thanks for getting to this. :)

Cheers,
Michael

On Mon, Jan 20 2020, Herbert Xu wrote:

> 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 <michael.greenberg@pomona.edu>
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
>
> 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;
>  }
> --
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: https://nam01.safelinks.protection.outlook.com/?url=http:%2F%2Fgondor.apana.org.au%2F~herbert%2F&amp;data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cdd48fafec4ff4860dd3d08d79d8dae5a%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637151104157970338&amp;sdata=50I8unmDcArdkRhrzECnv4S05daM6cq1az7SBENGpG4%3D&amp;reserved=0
> PGP Key: https://nam01.safelinks.protection.outlook.com/?url=http:%2F%2Fgondor.apana.org.au%2F~herbert%2Fpubkey.txt&amp;data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cdd48fafec4ff4860dd3d08d79d8dae5a%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637151104157970338&amp;sdata=1wrzxGBArZ5YVQCKV%2BdYH03e0v0wevKHFnfYcCgwQWM%3D&amp;reserved=0
> ________________________________
>
> [EXTERNAL EMAIL] Exercise caution before clicking on links or opening attachments.

      reply	other threads:[~2020-01-20 15:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m2r281wn3m.fsf@pomona.edu>
2020-01-20  9:46 ` [PATCH] builtin: Fix seconds part of times(1) Herbert Xu
2020-01-20 15:09   ` Michael Greenberg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2lfq26u7h.fsf@pomona.edu \
    --to=michael.greenberg@pomona.edu \
    --cc=dash@vger.kernel.org \
    --cc=herbert@gondor.apana.org.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).