time: implement -a, -o FILE
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>
diff --git a/miscutils/time.c b/miscutils/time.c
index 7c457a9..dd0c020 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 @@
{
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);
}