[git commit] hush: optional times builtin

Denys Vlasenko vda.linux at googlemail.com
Thu Aug 10 14:34:03 UTC 2017


commit: https://git.busybox.net/busybox/commit/?id=11f2e99c13b42675bb65cf2cfd3e3a98f95f2cee
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
builtin_times                                          -     108    +108
bltins1                                              360     372     +12
static.times_tbl                                       -       9      +9
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 1/0 up/down: 129/0)             Total: 129 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c  | 16 +++++++++-------
 shell/hush.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index fd17723..5c03f1f 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -13351,21 +13351,23 @@ static const unsigned char timescmd_str[] ALIGN1 = {
 static int FAST_FUNC
 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
-	unsigned long clk_tck, s, t;
+	unsigned clk_tck;
 	const unsigned char *p;
 	struct tms buf;
 
 	clk_tck = bb_clk_tck();
-	times(&buf);
 
+	times(&buf);
 	p = timescmd_str;
 	do {
+		unsigned sec, frac;
+		unsigned long t;
 		t = *(clock_t *)(((char *) &buf) + p[1]);
-		s = t / clk_tck;
-		t = t % clk_tck;
-		out1fmt("%lum%lu.%03lus%c",
-			s / 60, s % 60,
-			(t * 1000) / clk_tck,
+		sec = t / clk_tck;
+		frac = t % clk_tck;
+		out1fmt("%um%u.%03us%c",
+			sec / 60, sec % 60,
+			(frac * 1000) / clk_tck,
 			p[0]);
 		p += 2;
 	} while (*p);
diff --git a/shell/hush.c b/shell/hush.c
index af3b95b..b53d1dc 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -48,7 +48,7 @@
  *      tilde expansion
  *      aliases
  *      builtins mandated by standards we don't support:
- *          [un]alias, command, fc, getopts, times:
+ *          [un]alias, command, fc, getopts:
  *          command -v CMD: print "/path/to/CMD"
  *              prints "CMD" for builtins
  *              prints "alias ALIAS='EXPANSION'" for aliases
@@ -59,7 +59,6 @@
  *              -p: use default $PATH
  *          command BLTIN: disables special-ness (e.g. errors do not abort)
  *          getopts: getopt() for shells
- *          times: print getrusage(SELF/CHILDREN).ru_utime/ru_stime
  *          fc -l[nr] [BEG] [END]: list range of commands in history
  *          fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands
  *          fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP
@@ -265,6 +264,11 @@
 //config:	default y
 //config:	depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:
+//config:config HUSH_TIMES
+//config:	bool "times builtin"
+//config:	default y
+//config:	depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
 //config:config HUSH_READ
 //config:	bool "read builtin"
 //config:	default y
@@ -325,6 +329,7 @@
 #if ENABLE_HUSH_CASE
 # include <fnmatch.h>
 #endif
+#include <sys/times.h>
 #include <sys/utsname.h> /* for setting $HOSTNAME */
 
 #include "busybox.h"  /* for APPLET_IS_NOFORK/NOEXEC */
@@ -1011,6 +1016,9 @@ static int builtin_trap(char **argv) FAST_FUNC;
 #if ENABLE_HUSH_TYPE
 static int builtin_type(char **argv) FAST_FUNC;
 #endif
+#if ENABLE_HUSH_TIMES
+static int builtin_times(char **argv) FAST_FUNC;
+#endif
 static int builtin_true(char **argv) FAST_FUNC;
 #if ENABLE_HUSH_UMASK
 static int builtin_umask(char **argv) FAST_FUNC;
@@ -1105,6 +1113,9 @@ static const struct built_in_command bltins1[] = {
 #if BASH_SOURCE
 	BLTIN("source"   , builtin_source  , NULL),
 #endif
+#if ENABLE_HUSH_TIMES
+	BLTIN("times"    , builtin_times   , NULL),
+#endif
 #if ENABLE_HUSH_TRAP
 	BLTIN("trap"     , builtin_trap    , "Trap signals"),
 #endif
@@ -10407,6 +10418,41 @@ static int FAST_FUNC builtin_return(char **argv)
 }
 #endif
 
+#if ENABLE_HUSH_TIMES
+static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM)
+{
+	static const uint8_t times_tbl[] ALIGN1 = {
+		' ',  offsetof(struct tms, tms_utime),
+		'\n', offsetof(struct tms, tms_stime),
+		' ',  offsetof(struct tms, tms_cutime),
+		'\n', offsetof(struct tms, tms_cstime),
+		0
+	};
+	const uint8_t *p;
+	unsigned clk_tck;
+	struct tms buf;
+
+	clk_tck = bb_clk_tck();
+
+	times(&buf);
+	p = times_tbl;
+	do {
+		unsigned sec, frac;
+		unsigned long t;
+		t = *(clock_t *)(((char *) &buf) + p[1]);
+		sec = t / clk_tck;
+		frac = t % clk_tck;
+		printf("%um%u.%03us%c",
+			sec / 60, sec % 60,
+			(frac * 1000) / clk_tck,
+			p[0]);
+		p += 2;
+	} while (*p);
+
+	return EXIT_SUCCESS;
+}
+#endif
+
 #if ENABLE_HUSH_MEMLEAK
 static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
 {


More information about the busybox-cvs mailing list