[BusyBox-cvs] busybox/shell ash.c,1.74,1.75

Manuel Novoa III mjn3 at busybox.net
Wed Aug 13 17:48:51 UTC 2003


Update of /var/cvs/busybox/shell
In directory winder:/tmp/cvs-serv3445

Modified Files:
	ash.c 
Log Message:
Rewrite timescmd() function to avoid the use of floating point and to
correct a bug in the seconds display where something like  65 seconds
would be output as "1m65.000000s".


Index: ash.c
===================================================================
RCS file: /var/cvs/busybox/shell/ash.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -d -r1.74 -r1.75
--- ash.c	6 Aug 2003 11:20:50 -0000	1.74
+++ ash.c	13 Aug 2003 17:48:47 -0000	1.75
@@ -12417,27 +12417,35 @@
 }
 /*      $NetBSD: setmode.c,v 1.29 2003/01/15 23:58:03 kleink Exp $      */
 
-/*
- * Copyright (c) 1999 Herbert Xu <herbert at debian.org>
- * This code for the times builtin.
- */
-
 #include <sys/times.h>
 
-int timescmd(int ac, char **av) {
+static const unsigned char timescmd_str[] = {
+	' ',  offsetof(struct tms, tms_utime),
+	'\n', offsetof(struct tms, tms_stime),
+	' ',  offsetof(struct tms, tms_cutime),
+	'\n', offsetof(struct tms, tms_cstime),
+	0
+};
+
+static int timescmd(int ac, char **av)
+{
+	long int clk_tck, s, t;
+	const unsigned char *p;
 	struct tms buf;
-	long int clk_tck = sysconf(_SC_CLK_TCK);
 
+	clk_tck = sysconf(_SC_CLK_TCK);
 	times(&buf);
-	out1fmt("%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);
+
+	p = timescmd_str;
+	do {
+		t = *(clock_t *)(((char *) &buf) + p[1]);
+		s = t / clk_tck;
+		out1fmt("%ldm%ld.%.3lds%c",
+			s/60, s%60,
+			((t - s * clk_tck) * 1000) / clk_tck,
+			p[0]);
+	} while (*(p += 2));
+
 	return 0;
 }
 




More information about the busybox-cvs mailing list