Skip to content

Commit 5c677a4

Browse files
committed
Refactor the new 'times' builtin; zero-pad seconds (re: 65d363f)
The output format is now identical to mksh's except for the locale-dependent radix point ('.' or ','). src/cmd/ksh93/bltins/misc.c: - Output format tweak: pad seconds with initial zero if < 10. - Use "too many operands" (e_toomanyops) error msg from 3ba4900 if there are operands, instead of "bad syntax" (e_badsyntax). - Consolidate repetitive calculating and printing code into print_times(). - Get rid of some excessive variables. src/cmd/ksh93/tests/builtins.sh: - Update regression tests to match the above. src/cmd/ksh93/data/builtins.c: - Update sh_opttimes[] version string.
1 parent 57ff467 commit 5c677a4

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

src/cmd/ksh93/bltins/misc.c

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -464,55 +464,34 @@ int b_jobs(register int n,char *argv[],Shbltin_t *context)
464464
/*
465465
* times command
466466
*/
467+
static void print_times(clock_t uticks, clock_t sticks, Shbltin_t *context)
468+
{
469+
int clk_tck = context->shp->gd->lim.clk_tck;
470+
double utime = (double)uticks / clk_tck, stime = (double)sticks / clk_tck;
471+
sfprintf(sfstdout, "%dm%05.2fs %dm%05.2fs\n",
472+
(int)floor(utime / 60), fmod(utime, 60),
473+
(int)floor(stime / 60), fmod(stime, 60));
474+
}
467475
int b_times(int argc, char *argv[], Shbltin_t *context)
468476
{
469-
Shell_t *shp = context->shp;
470-
const char *cmd = argv[0];
471477
struct tms cpu_times;
472-
clock_t rv;
473-
double utime, stime, utime_min, utime_sec, stime_min, stime_sec;
474-
475-
while (argc = optget(argv, sh_opttimes)) switch (argc)
478+
/* No options or operands are supported, except --man, etc. */
479+
if (argc = optget(argv, sh_opttimes)) switch (argc)
476480
{
477481
case ':':
478482
errormsg(SH_DICT, 2, "%s", opt_info.arg);
479-
break;
480-
case '?':
483+
errormsg(SH_DICT, ERROR_usage(2), "%s", optusage((char*)0));
484+
default:
481485
errormsg(SH_DICT, ERROR_usage(0), "%s", opt_info.arg);
482486
return(2);
483-
default:
484-
break;
485487
}
486-
if (error_info.errors)
487-
errormsg(SH_DICT, ERROR_usage(2), "%s", optusage((char*)0));
488-
489-
argv += opt_info.index;
490-
if (*argv)
491-
errormsg(SH_DICT, ERROR_exit(3), e_badsyntax);
492-
493-
rv = times(&cpu_times);
494-
if (rv == (clock_t)-1)
495-
errormsg(SH_DICT, ERROR_exit(2), "times(3) failed: errno %d: %s",
496-
errno, strerror(errno));
497-
498-
/* First line: user and system times used by the shell */
499-
utime = (double)cpu_times.tms_utime / shp->gd->lim.clk_tck;
500-
utime_min = floor(utime / 60);
501-
utime_sec = fmod(utime, 60);
502-
stime = (double)cpu_times.tms_stime / shp->gd->lim.clk_tck;
503-
stime_min = floor(stime / 60);
504-
stime_sec = fmod(stime, 60);
505-
sfprintf(sfstdout, "%dm%.2fs %dm%.2fs\n", (int)utime_min, utime_sec, (int)stime_min, stime_sec);
506-
507-
/* Second line: same for the shell's child processes */
508-
utime = (double)cpu_times.tms_cutime / shp->gd->lim.clk_tck;
509-
utime_min = floor(utime / 60);
510-
utime_sec = fmod(utime, 60);
511-
stime = (double)cpu_times.tms_cstime / shp->gd->lim.clk_tck;
512-
stime_min = floor(stime / 60);
513-
stime_sec = fmod(stime, 60);
514-
sfprintf(sfstdout, "%dm%.2fs %dm%.2fs\n", (int)utime_min, utime_sec, (int)stime_min, stime_sec);
515-
488+
if (argv[opt_info.index])
489+
errormsg(SH_DICT, ERROR_exit(2), e_toomanyops);
490+
/* Get & print the times */
491+
if (times(&cpu_times) == (clock_t)-1)
492+
errormsg(SH_DICT, ERROR_exit(1), "times(3) failed: %s", strerror(errno));
493+
print_times(cpu_times.tms_utime, cpu_times.tms_stime, context);
494+
print_times(cpu_times.tms_cutime, cpu_times.tms_cstime, context);
516495
return(0);
517496
}
518497

src/cmd/ksh93/data/builtins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ USAGE_LICENSE
18361836
;
18371837

18381838
const char sh_opttimes[] =
1839-
"[-1c?@(#)$Id: times (ksh93) 2020-06-06 $\n]"
1839+
"[-1c?@(#)$Id: times (ksh93) 2020-06-24 $\n]"
18401840
"[+NAME?times - display CPU usage by the shell and child processes]"
18411841
"[+DESCRIPTION?\btimes\b displays the accumulated user and system CPU times, "
18421842
"one line with the times used by the shell and another with those used by "

src/cmd/ksh93/tests/builtins.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ fi
671671
# ======
672672
# 'times' builtin
673673
674-
expect=$'0m0.0[0-9]s 0m0.0[0-9]s\n0m0.00s 0m0.00s'
674+
expect=$'0m00.0[0-9]s 0m00.0[0-9]s\n0m00.00s 0m00.00s'
675675
actual=$("$SHELL" -c times)
676676
[[ $actual == $expect ]] || err_exit "times output: expected $(printf %q "$expect"), got $(printf %q "$actual"))"
677677
678-
expect=$'*: times: incorrect syntax'
678+
expect=$'*: times: too many operands'
679679
actual=$(set +x; eval 'times Extra Args' 2>&1)
680680
[[ $actual == $expect ]] || err_exit "times with args: expected $(printf %q "$expect"), got $(printf %q "$actual"))"
681681

0 commit comments

Comments
 (0)