summary refs log tree commit diff
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2020-01-20 17:46:48 +0800
committerHerbert Xu <herbert@gondor.apana.org.au>2020-04-29 16:04:17 +1000
commitfd5311ace04bcc2fea0a330c95a3423931ec45fa (patch)
tree1830fdb98b741752a341271160c3821cc1989f7a
parentredir: Clear saved redirections in subshell (diff)
downloaddash-fd5311ace04bcc2fea0a330c95a3423931ec45fa.tar.gz
dash-fd5311ace04bcc2fea0a330c95a3423931ec45fa.zip
builtin: Fix seconds part of times(1)
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>
-rw-r--r--src/bltin/times.c30
1 files changed, 21 insertions, 9 deletions
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;
 }