summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-12-22 12:36:13 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-12-22 12:36:13 +0000
commitf93ab47c4560b2a1a3130ac17d0bfb886f09ece8 (patch)
tree4af247ff3701729e8a9730052ecc675dff911c04
parent1d14e6f48c266947f78620976697cafc670796f8 (diff)
downloadbusybox-w32-f93ab47c4560b2a1a3130ac17d0bfb886f09ece8.tar.gz
busybox-w32-f93ab47c4560b2a1a3130ac17d0bfb886f09ece8.tar.bz2
busybox-w32-f93ab47c4560b2a1a3130ac17d0bfb886f09ece8.zip
time: fix option parsing bug, size optimizations
-rw-r--r--miscutils/time.c50
1 files changed, 22 insertions, 28 deletions
diff --git a/miscutils/time.c b/miscutils/time.c
index 3f1451c83..7682a2679 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -65,11 +65,12 @@ static const char *const long_format =
65 "\tSocket messages sent: %s\n" 65 "\tSocket messages sent: %s\n"
66 "\tSocket messages received: %r\n" 66 "\tSocket messages received: %r\n"
67 "\tSignals delivered: %k\n" 67 "\tSignals delivered: %k\n"
68 "\tPage size (bytes): %Z\n" "\tExit status: %x"; 68 "\tPage size (bytes): %Z\n"
69 "\tExit status: %x";
69 70
70 71
71 /* Wait for and fill in data on child process PID. 72/* Wait for and fill in data on child process PID.
72 Return 0 on error, 1 if ok. */ 73 Return 0 on error, 1 if ok. */
73 74
74/* pid_t is short on BSDI, so don't try to promote it. */ 75/* pid_t is short on BSDI, so don't try to promote it. */
75static int resuse_end(pid_t pid, resource_t * resp) 76static int resuse_end(pid_t pid, resource_t * resp)
@@ -110,8 +111,7 @@ static void fprintargv(FILE * fp, char *const *argv, const char *filler)
110 fputs(filler, fp); 111 fputs(filler, fp);
111 fputs(*av, fp); 112 fputs(*av, fp);
112 } 113 }
113 if (ferror(fp)) 114 die_if_ferror(fp, "output");
114 bb_error_msg_and_die(bb_msg_write_error);
115} 115}
116 116
117/* Return the number of kilobytes corresponding to a number of pages PAGES. 117/* 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,
381 putc(*fmt++, fp); 381 putc(*fmt++, fp);
382 } 382 }
383 383
384 if (ferror(fp)) 384 die_if_ferror(fp, "output");
385 bb_error_msg_and_die(bb_msg_write_error);
386 } 385 }
387 putc('\n', fp); 386 putc('\n', fp);
388 387
389 if (ferror(fp)) 388 die_if_ferror(fp, "output");
390 bb_error_msg_and_die(bb_msg_write_error);
391} 389}
392 390
393/* Run command CMD and return statistics on it. 391/* Run command CMD and return statistics on it.
@@ -423,18 +421,16 @@ static void run_command(char *const *cmd, resource_t * resp)
423 421
424int time_main(int argc, char **argv) 422int time_main(int argc, char **argv)
425{ 423{
426 int gotone;
427 resource_t res; 424 resource_t res;
428 const char *output_format = default_format; 425 const char *output_format = default_format;
426 char c;
429 427
430 argc--; 428 goto next;
431 argv++;
432 /* Parse any options -- don't use getopt() here so we don't 429 /* Parse any options -- don't use getopt() here so we don't
433 * consume the args of our client application... */ 430 * consume the args of our client application... */
434 while (argc > 0 && **argv == '-') { 431 while (argc > 0 && argv[0][0] == '-') {
435 gotone = 0; 432 while ((c = *++*argv)) {
436 while (gotone == 0 && *++(*argv)) { 433 switch (c) {
437 switch (**argv) {
438 case 'v': 434 case 'v':
439 output_format = long_format; 435 output_format = long_format;
440 break; 436 break;
@@ -444,24 +440,22 @@ int time_main(int argc, char **argv)
444 default: 440 default:
445 bb_show_usage(); 441 bb_show_usage();
446 } 442 }
447 argc--;
448 argv++;
449 gotone = 1;
450 } 443 }
444 next:
445 argv++;
446 argc--;
447 if (!argc)
448 bb_show_usage();
451 } 449 }
452 450
453 if (argv == NULL || *argv == NULL)
454 bb_show_usage();
455
456 run_command(argv, &res); 451 run_command(argv, &res);
457 summarize(stderr, output_format, argv, &res); 452 summarize(stderr, output_format, argv, &res);
458 fflush(stderr);
459 453
460 if (WIFSTOPPED(res.waitstatus)) 454 if (WIFSTOPPED(res.waitstatus))
461 exit(WSTOPSIG(res.waitstatus)); 455 return WSTOPSIG(res.waitstatus);
462 else if (WIFSIGNALED(res.waitstatus)) 456 if (WIFSIGNALED(res.waitstatus))
463 exit(WTERMSIG(res.waitstatus)); 457 return WTERMSIG(res.waitstatus);
464 else if (WIFEXITED(res.waitstatus)) 458 if (WIFEXITED(res.waitstatus))
465 exit(WEXITSTATUS(res.waitstatus)); 459 return WEXITSTATUS(res.waitstatus);
466 return 0; 460 return 0;
467} 461}