builtin: Fix seconds part of times(1)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 builtin: Fix seconds part of times(1)
Quoted 3 lines · Michael Greenberg
If folks are opposed to including
math.hfor 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.
src/bltin/times.c +21 -9
@@ -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 ; }
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Looks great! Thanks for getting to this. :)
Cheers,
Michael
Quoted 67 lines · Herbert Xu
Quoted 3 lines · Michael Greenberg
If folks are opposed to including
math.hfor 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 <[email protected]>Signed-off-by Herbert Xu <[email protected]>src/bltin/times.c +21
-10@@ -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 <[email protected]>
Home Page: https://nam01.safelinks.protection.outlook.com/?url=http:%2F%2Fgondor.apana.org.au%2F~herbert%2F&data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cdd48fafec4ff4860dd3d08d79d8dae5a%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637151104157970338&sdata=50I8unmDcArdkRhrzECnv4S05daM6cq1az7SBENGpG4%3D&reserved=0
PGP Key: https://nam01.safelinks.protection.outlook.com/?url=http:%2F%2Fgondor.apana.org.au%2F~herbert%2Fpubkey.txt&data=02%7C01%7CMichael.Greenberg%40pomona.edu%7Cdd48fafec4ff4860dd3d08d79d8dae5a%7C817f590439044ee8b3a5a65d4746ff70%7C0%7C0%7C637151104157970338&sdata=1wrzxGBArZ5YVQCKV%2BdYH03e0v0wevKHFnfYcCgwQWM%3D&reserved=0
________________________________[EXTERNAL EMAIL] Exercise caution before clicking on links or opening attachments.