1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

time: Fix precision bug in times(3) fallback (#425)

In the times(3) fallback for the time keyword (which can be enabled
in xec.c by undefining _lib_getrusage and timeofday), ksh will
print the obtained time incorrectly if TIMEFORMAT is set to use a
precision level of three:
   $ TIMEFORMAT=$'\nreal\t%3lR'
   $ time sleep .080
   real 0m00.008s  # Should be '00.080s'
This commit corrects that issue by using 10^precision to get the
correct fractional scaling. Note that the fallback still doesn't
support a true precision level of three (times(3) alone doesn't
support it), so this in effect pads a zero to the end of the output
when the precision level is three.

Additional change to tests/builtins.sh:
- While fixing the above issue I found out that ksh93v- broke
  support for passing microseconds to the sleep builtin in the form
  of <num>U. I've added a regression test for that bug to ensure it
  isn't backported to ksh93u+m by accident.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2022-01-13 04:17:36 -08:00 committed by Martijn Dekker
parent 40b2c3c3d4
commit 307bc3edce
2 changed files with 6 additions and 1 deletions

View file

@ -225,7 +225,7 @@ static void l_time(Sfio_t *outfile,register clock_t t,int precision)
if(precision)
{
frac = t%sh.lim.clk_tck;
frac = (frac*100)/sh.lim.clk_tck;
frac = (frac*(int)pow(10,precision))/sh.lim.clk_tck;
}
t /= sh.lim.clk_tck;
sec = t%60;

View file

@ -1516,5 +1516,10 @@ if builtin tail 2> /dev/null; then
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
fi
# ======
# ksh93v- accidentally broke the sleep builtin's support for
# using microseconds in the form of <num>U.
got=$(sleep 1U 2>&1) || err_exit "sleep builtin cannot handle microseconds in the form of <num>U (got $(printf %q "$got"))"
# ======
exit $((Errors<125?Errors:125))