time: implement -a, -o FILE
authorTommi Rantala <tommi.t.rantala@nokia.com>
Fri, 28 Apr 2017 15:54:14 +0000 (17:54 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 28 Apr 2017 15:54:14 +0000 (17:54 +0200)
function                                             old     new   delta
time_main                                           1052    1076     +24
packed_usage                                       31571   31577      +6

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/time.c

index 7c457a91a6c5ba5be9c1f417703746b3d0f73520..dd0c0208ad8813b80b45f90b36bb6f39a21515ea 100644 (file)
 //kbuild:lib-$(CONFIG_TIME) += time.o
 
 //usage:#define time_trivial_usage
-//usage:       "[-vp] PROG ARGS"
+//usage:       "[-vpa] [-o FILE] PROG ARGS"
 //usage:#define time_full_usage "\n\n"
 //usage:       "Run PROG, display resource usage when it exits\n"
 //usage:     "\n       -v      Verbose"
 //usage:     "\n       -p      POSIX output format"
+//usage:     "\n       -o FILE Write result to FILE"
+//usage:     "\n       -a      Append (else overwrite)"
 
 #include "libbb.h"
 #include <sys/resource.h> /* getrusage */
@@ -414,28 +416,47 @@ int time_main(int argc UNUSED_PARAM, char **argv)
 {
        resource_t res;
        const char *output_format = default_format;
+       char *output_filename;
+       int output_fd;
        int opt;
+       int ex;
+       enum {
+               OPT_v = (1 << 0),
+               OPT_p = (1 << 1),
+               OPT_a = (1 << 2),
+               OPT_o = (1 << 3),
+       };
 
        opt_complementary = "-1"; /* at least one arg */
        /* "+": stop on first non-option */
-       opt = getopt32(argv, "+vp");
+       opt = getopt32(argv, "+vpao:", &output_filename);
        argv += optind;
-       if (opt & 1)
+       if (opt & OPT_v)
                output_format = long_format;
-       if (opt & 2)
+       if (opt & OPT_p)
                output_format = posix_format;
+       output_fd = STDERR_FILENO;
+       if (opt & OPT_o) {
+               output_fd = xopen(output_filename,
+                       (opt & OPT_a) /* append? */
+                       ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND)
+                       : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC)
+               );
+       }
 
        run_command(argv, &res);
 
        /* Cheat. printf's are shorter :) */
-       xdup2(STDERR_FILENO, STDOUT_FILENO);
+       xdup2(output_fd, STDOUT_FILENO);
        summarize(output_format, argv, &res);
 
+       ex = WEXITSTATUS(res.waitstatus);
+       /* Impossible: we do not use WUNTRACED flag in wait()...
        if (WIFSTOPPED(res.waitstatus))
-               return WSTOPSIG(res.waitstatus);
+               ex = WSTOPSIG(res.waitstatus);
+       */
        if (WIFSIGNALED(res.waitstatus))
-               return WTERMSIG(res.waitstatus);
-       if (WIFEXITED(res.waitstatus))
-               return WEXITSTATUS(res.waitstatus);
-       fflush_stdout_and_exit(EXIT_SUCCESS);
+               ex = WTERMSIG(res.waitstatus);
+
+       fflush_stdout_and_exit(ex);
 }