time: fix option parsing bug, size optimizations

This commit is contained in:
Denis Vlasenko 2006-12-22 12:36:13 +00:00
parent 1d14e6f48c
commit f93ab47c45

View File

@ -65,11 +65,12 @@ static const char *const long_format =
"\tSocket messages sent: %s\n" "\tSocket messages sent: %s\n"
"\tSocket messages received: %r\n" "\tSocket messages received: %r\n"
"\tSignals delivered: %k\n" "\tSignals delivered: %k\n"
"\tPage size (bytes): %Z\n" "\tExit status: %x"; "\tPage size (bytes): %Z\n"
"\tExit status: %x";
/* Wait for and fill in data on child process PID. /* Wait for and fill in data on child process PID.
Return 0 on error, 1 if ok. */ Return 0 on error, 1 if ok. */
/* pid_t is short on BSDI, so don't try to promote it. */ /* pid_t is short on BSDI, so don't try to promote it. */
static int resuse_end(pid_t pid, resource_t * resp) static int resuse_end(pid_t pid, resource_t * resp)
@ -110,8 +111,7 @@ static void fprintargv(FILE * fp, char *const *argv, const char *filler)
fputs(filler, fp); fputs(filler, fp);
fputs(*av, fp); fputs(*av, fp);
} }
if (ferror(fp)) die_if_ferror(fp, "output");
bb_error_msg_and_die(bb_msg_write_error);
} }
/* Return the number of kilobytes corresponding to a number of pages PAGES. /* Return the number of kilobytes corresponding to a number of pages PAGES.
@ -381,13 +381,11 @@ static void summarize(FILE * fp, const char *fmt, char **command,
putc(*fmt++, fp); putc(*fmt++, fp);
} }
if (ferror(fp)) die_if_ferror(fp, "output");
bb_error_msg_and_die(bb_msg_write_error);
} }
putc('\n', fp); putc('\n', fp);
if (ferror(fp)) die_if_ferror(fp, "output");
bb_error_msg_and_die(bb_msg_write_error);
} }
/* Run command CMD and return statistics on it. /* Run command CMD and return statistics on it.
@ -423,18 +421,16 @@ static void run_command(char *const *cmd, resource_t * resp)
int time_main(int argc, char **argv) int time_main(int argc, char **argv)
{ {
int gotone;
resource_t res; resource_t res;
const char *output_format = default_format; const char *output_format = default_format;
char c;
argc--; goto next;
argv++;
/* Parse any options -- don't use getopt() here so we don't /* Parse any options -- don't use getopt() here so we don't
* consume the args of our client application... */ * consume the args of our client application... */
while (argc > 0 && **argv == '-') { while (argc > 0 && argv[0][0] == '-') {
gotone = 0; while ((c = *++*argv)) {
while (gotone == 0 && *++(*argv)) { switch (c) {
switch (**argv) {
case 'v': case 'v':
output_format = long_format; output_format = long_format;
break; break;
@ -444,24 +440,22 @@ int time_main(int argc, char **argv)
default: default:
bb_show_usage(); bb_show_usage();
} }
argc--;
argv++;
gotone = 1;
} }
next:
argv++;
argc--;
if (!argc)
bb_show_usage();
} }
if (argv == NULL || *argv == NULL)
bb_show_usage();
run_command(argv, &res); run_command(argv, &res);
summarize(stderr, output_format, argv, &res); summarize(stderr, output_format, argv, &res);
fflush(stderr);
if (WIFSTOPPED(res.waitstatus)) if (WIFSTOPPED(res.waitstatus))
exit(WSTOPSIG(res.waitstatus)); return WSTOPSIG(res.waitstatus);
else if (WIFSIGNALED(res.waitstatus)) if (WIFSIGNALED(res.waitstatus))
exit(WTERMSIG(res.waitstatus)); return WTERMSIG(res.waitstatus);
else if (WIFEXITED(res.waitstatus)) if (WIFEXITED(res.waitstatus))
exit(WEXITSTATUS(res.waitstatus)); return WEXITSTATUS(res.waitstatus);
return 0; return 0;
} }