From 5fe5be210c7d22c94f07036d4933dae0b2010286 Mon Sep 17 00:00:00 2001 From: Tommi Rantala Date: Fri, 28 Apr 2017 17:54:14 +0200 Subject: time: implement -a, -o FILE function old new delta time_main 1052 1076 +24 packed_usage 31571 31577 +6 Signed-off-by: Tommi Rantala Signed-off-by: Denys Vlasenko --- miscutils/time.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'miscutils') diff --git a/miscutils/time.c b/miscutils/time.c index 7c457a91a..dd0c0208a 100644 --- a/miscutils/time.c +++ b/miscutils/time.c @@ -21,11 +21,13 @@ //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 /* 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); } -- cgit v1.2.3