aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/time.c41
1 files changed, 31 insertions, 10 deletions
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 <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);
}