hush: optional times builtin
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 10 Aug 2017 14:34:03 +0000 (16:34 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 10 Aug 2017 14:34:03 +0000 (16:34 +0200)
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@googlemail.com>
shell/ash.c
shell/hush.c

index fd17723515b39da93b5799de908f94e1abd5f035..5c03f1fdcff455cec33b2c8d5f85f713784cc21a 100644 (file)
@@ -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);
index af3b95b86ad741f3d4fc634cb19291d5c0cf4024..b53d1dcfbd0fcb644c1c15816eac822a7105c15c 100644 (file)
@@ -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
 //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
 #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)
 {