diff options
| author | Tommi Rantala <tommi.t.rantala@nokia.com> | 2017-04-28 17:54:14 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-28 17:54:14 +0200 |
| commit | 5fe5be210c7d22c94f07036d4933dae0b2010286 (patch) | |
| tree | 874b6624e99d108dc48745f5a8f72b938501c21f /miscutils | |
| parent | 854174f7ddc21350c0dbf826c76400910b33806c (diff) | |
| download | busybox-w32-5fe5be210c7d22c94f07036d4933dae0b2010286.tar.gz busybox-w32-5fe5be210c7d22c94f07036d4933dae0b2010286.tar.bz2 busybox-w32-5fe5be210c7d22c94f07036d4933dae0b2010286.zip | |
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>
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/time.c | 41 |
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 @@ | |||
| 21 | //kbuild:lib-$(CONFIG_TIME) += time.o | 21 | //kbuild:lib-$(CONFIG_TIME) += time.o |
| 22 | 22 | ||
| 23 | //usage:#define time_trivial_usage | 23 | //usage:#define time_trivial_usage |
| 24 | //usage: "[-vp] PROG ARGS" | 24 | //usage: "[-vpa] [-o FILE] PROG ARGS" |
| 25 | //usage:#define time_full_usage "\n\n" | 25 | //usage:#define time_full_usage "\n\n" |
| 26 | //usage: "Run PROG, display resource usage when it exits\n" | 26 | //usage: "Run PROG, display resource usage when it exits\n" |
| 27 | //usage: "\n -v Verbose" | 27 | //usage: "\n -v Verbose" |
| 28 | //usage: "\n -p POSIX output format" | 28 | //usage: "\n -p POSIX output format" |
| 29 | //usage: "\n -o FILE Write result to FILE" | ||
| 30 | //usage: "\n -a Append (else overwrite)" | ||
| 29 | 31 | ||
| 30 | #include "libbb.h" | 32 | #include "libbb.h" |
| 31 | #include <sys/resource.h> /* getrusage */ | 33 | #include <sys/resource.h> /* getrusage */ |
| @@ -414,28 +416,47 @@ int time_main(int argc UNUSED_PARAM, char **argv) | |||
| 414 | { | 416 | { |
| 415 | resource_t res; | 417 | resource_t res; |
| 416 | const char *output_format = default_format; | 418 | const char *output_format = default_format; |
| 419 | char *output_filename; | ||
| 420 | int output_fd; | ||
| 417 | int opt; | 421 | int opt; |
| 422 | int ex; | ||
| 423 | enum { | ||
| 424 | OPT_v = (1 << 0), | ||
| 425 | OPT_p = (1 << 1), | ||
| 426 | OPT_a = (1 << 2), | ||
| 427 | OPT_o = (1 << 3), | ||
| 428 | }; | ||
| 418 | 429 | ||
| 419 | opt_complementary = "-1"; /* at least one arg */ | 430 | opt_complementary = "-1"; /* at least one arg */ |
| 420 | /* "+": stop on first non-option */ | 431 | /* "+": stop on first non-option */ |
| 421 | opt = getopt32(argv, "+vp"); | 432 | opt = getopt32(argv, "+vpao:", &output_filename); |
| 422 | argv += optind; | 433 | argv += optind; |
| 423 | if (opt & 1) | 434 | if (opt & OPT_v) |
| 424 | output_format = long_format; | 435 | output_format = long_format; |
| 425 | if (opt & 2) | 436 | if (opt & OPT_p) |
| 426 | output_format = posix_format; | 437 | output_format = posix_format; |
| 438 | output_fd = STDERR_FILENO; | ||
| 439 | if (opt & OPT_o) { | ||
| 440 | output_fd = xopen(output_filename, | ||
| 441 | (opt & OPT_a) /* append? */ | ||
| 442 | ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND) | ||
| 443 | : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC) | ||
| 444 | ); | ||
| 445 | } | ||
| 427 | 446 | ||
| 428 | run_command(argv, &res); | 447 | run_command(argv, &res); |
| 429 | 448 | ||
| 430 | /* Cheat. printf's are shorter :) */ | 449 | /* Cheat. printf's are shorter :) */ |
| 431 | xdup2(STDERR_FILENO, STDOUT_FILENO); | 450 | xdup2(output_fd, STDOUT_FILENO); |
| 432 | summarize(output_format, argv, &res); | 451 | summarize(output_format, argv, &res); |
| 433 | 452 | ||
| 453 | ex = WEXITSTATUS(res.waitstatus); | ||
| 454 | /* Impossible: we do not use WUNTRACED flag in wait()... | ||
| 434 | if (WIFSTOPPED(res.waitstatus)) | 455 | if (WIFSTOPPED(res.waitstatus)) |
| 435 | return WSTOPSIG(res.waitstatus); | 456 | ex = WSTOPSIG(res.waitstatus); |
| 457 | */ | ||
| 436 | if (WIFSIGNALED(res.waitstatus)) | 458 | if (WIFSIGNALED(res.waitstatus)) |
| 437 | return WTERMSIG(res.waitstatus); | 459 | ex = WTERMSIG(res.waitstatus); |
| 438 | if (WIFEXITED(res.waitstatus)) | 460 | |
| 439 | return WEXITSTATUS(res.waitstatus); | 461 | fflush_stdout_and_exit(ex); |
| 440 | fflush_stdout_and_exit(EXIT_SUCCESS); | ||
| 441 | } | 462 | } |
