fsck: more clever (->smaller) handling of parameters

function                                             old     new   delta
fsck_device                                          213     449    +236
new_args                                               -      46     +46
fsck_main                                           1870    1815     -55
execute                                              289       -    -289
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 1/1 up/down: 282/-344)          Total: -62 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2009-09-13 05:52:46 +02:00
parent 6683745bf7
commit 7649bd024c

View File

@@ -490,60 +490,55 @@ static int wait_many(int flags)
static void execute(const char *type, const char *device, static void execute(const char *type, const char *device,
const char *mntpt /*, int interactive */) const char *mntpt /*, int interactive */)
{ {
char *argv[num_args + 4]; /* see count below: */
int argc;
int i; int i;
struct fsck_instance *inst; struct fsck_instance *inst;
pid_t pid; pid_t pid;
argv[0] = xasprintf("fsck.%s", type); /* 1 */ args[0] = xasprintf("fsck.%s", type);
for (i = 0; i < num_args; i++)
argv[i+1] = args[i]; /* num_args */
argc = num_args + 1;
#if DO_PROGRESS_INDICATOR #if DO_PROGRESS_INDICATOR
if (progress && !progress_active()) { if (progress && !progress_active()) {
if (strcmp(type, "ext2") == 0 if (strcmp(type, "ext2") == 0
|| strcmp(type, "ext3") == 0 || strcmp(type, "ext3") == 0
) { ) {
argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */ args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
inst->flags |= FLAG_PROGRESS; inst->flags |= FLAG_PROGRESS;
} }
} }
#endif #endif
argv[argc++] = (char*)device; /* 1 */ args[num_args - 2] = (char*)device;
argv[argc] = NULL; /* 1 */ /* args[num_args - 1] = NULL; - already is */
if (verbose || noexecute) { if (verbose || noexecute) {
printf("[%s (%d) -- %s]", argv[0], num_running, printf("[%s (%d) -- %s]", args[0], num_running,
mntpt ? mntpt : device); mntpt ? mntpt : device);
for (i = 0; i < argc; i++) for (i = 0; args[i]; i++)
printf(" %s", argv[i]); printf(" %s", args[i]);
bb_putchar('\n'); bb_putchar('\n');
} }
/* Fork and execute the correct program. */ /* Fork and execute the correct program. */
pid = -1; pid = -1;
if (!noexecute) { if (!noexecute) {
pid = spawn(argv); pid = spawn(args);
if (pid < 0) if (pid < 0)
bb_simple_perror_msg(argv[0]); bb_simple_perror_msg(args[0]);
} }
#if DO_PROGRESS_INDICATOR #if DO_PROGRESS_INDICATOR
free(argv[num_args + 1]); free(args[XXX]);
#endif #endif
/* No child, so don't record an instance */ /* No child, so don't record an instance */
if (pid <= 0) { if (pid <= 0) {
free(argv[0]); free(args[0]);
return; return;
} }
inst = xzalloc(sizeof(*inst)); inst = xzalloc(sizeof(*inst));
inst->pid = pid; inst->pid = pid;
inst->prog = argv[0]; inst->prog = args[0];
inst->device = xstrdup(device); inst->device = xstrdup(device);
inst->base_device = base_device(device); inst->base_device = base_device(device);
#if DO_PROGRESS_INDICATOR #if DO_PROGRESS_INDICATOR
@@ -899,6 +894,12 @@ static void compile_fs_type(char *fs_type)
} }
} }
static char **new_args(void)
{
args = xrealloc_vector(args, 2, num_args);
return &args[num_args++];
}
int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int fsck_main(int argc UNUSED_PARAM, char **argv) int fsck_main(int argc UNUSED_PARAM, char **argv)
{ {
@@ -922,11 +923,9 @@ int fsck_main(int argc UNUSED_PARAM, char **argv)
opts_for_fsck = doall = notitle = 0; opts_for_fsck = doall = notitle = 0;
devices = NULL; devices = NULL;
num_devices = 0; num_devices = 0;
/* in bss, so already zeroed new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
args = NULL; /* instance_list = NULL; - in bss, so already zeroed */
num_args = 0;
instance_list = NULL;
*/
while (*++argv) { while (*++argv) {
int j; int j;
int optpos; int optpos;
@@ -944,8 +943,7 @@ int fsck_main(int argc UNUSED_PARAM, char **argv)
} }
if (arg[0] != '-' || opts_for_fsck) { if (arg[0] != '-' || opts_for_fsck) {
args = xrealloc_vector(args, 2, num_args); *new_args() = arg;
args[num_args++] = arg;
continue; continue;
} }
@@ -1022,8 +1020,7 @@ int fsck_main(int argc UNUSED_PARAM, char **argv)
if (optpos) { if (optpos) {
options[0] = '-'; options[0] = '-';
options[optpos + 1] = '\0'; options[optpos + 1] = '\0';
args = xrealloc_vector(args, 2, num_args); *new_args() = options;
args[num_args++] = options;
} }
} }
if (getenv("FSCK_FORCE_ALL_PARALLEL")) if (getenv("FSCK_FORCE_ALL_PARALLEL"))
@@ -1031,6 +1028,8 @@ int fsck_main(int argc UNUSED_PARAM, char **argv)
tmp = getenv("FSCK_MAX_INST"); tmp = getenv("FSCK_MAX_INST");
if (tmp) if (tmp)
max_running = xatoi(tmp); max_running = xatoi(tmp);
new_args(); /* args[num_args - 2] will be replaced by <device> */
new_args(); /* args[num_args - 1] is the last, NULL element */
if (!notitle) if (!notitle)
puts("fsck (busybox "BB_VER", "BB_BT")"); puts("fsck (busybox "BB_VER", "BB_BT")");