From: Todd C. Miller Date: Mon, 11 Dec 1995 22:09:10 +0000 (+0000) Subject: now copy command args directly from Argv X-Git-Tag: SUDO_1_4_0~50 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=206899d1dd24ac114237d47c33650805bd549481;p=sudo now copy command args directly from Argv --- diff --git a/sudo.c b/sudo.c index 1d8eeb301..16ce00687 100644 --- a/sudo.c +++ b/sudo.c @@ -639,8 +639,6 @@ static void add_env() * This function sets the cmnd and cmnd_args global variables based on Argv */ -#define ARG_INC BUFSIZ - static void load_cmnd(sudo_mode) int sudo_mode; { @@ -670,30 +668,32 @@ static void load_cmnd(sudo_mode) * Find the length of cmnd_args and allocate space, then fill it in. */ if (Argc > arg_start) { - int len=0; /* sum length of all args */ - char **cur_arg; /* current command line arg */ - char *pos; /* position in the cmnd_args string */ + size_t arg_len; /* sum length of all args */ + char *from, *to; /* loop variables for string copy */ + int i; /* venerable loop variable */ - /* how much space do we need? */ - for (cur_arg = &Argv[arg_start]; *cur_arg; cur_arg++) { - len += strlen(*cur_arg) + 1; - } + /* + * How much space do we need? + * This assumes that Argv (aka argv) is contiguous. + */ + arg_len = (size_t) Argv[Argc-1] + strlen(Argv[Argc-1]) + 1 - + (size_t) Argv[arg_start]; - if ((pos = cmnd_args = (char *) malloc(len)) == NULL) { + if ((cmnd_args = (char *) malloc(arg_len)) == NULL) { perror("malloc"); (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); exit(1); } /* - * It would be quicker to store the result of strlen() in - * an array when it is computed the first time (above). - * Then we wouldn't need to duplicate the effort... XXX + * Copy from Argv to cmnd_args, replacing NULL's with spaces. + * This assumes that Argv (aka argv) is contiguous. */ - for (cur_arg = &Argv[arg_start]; *cur_arg; cur_arg++) { - (void) strcpy(pos, *cur_arg); - pos += strlen(*cur_arg); + for (i=0, to=cmnd_args, from=Argv[arg_start]; i < arg_len; i++) { + if ((*to++ = *from++) == NULL) + *(to-1) = ' '; } + cmnd_args[arg_len-1] = '\0'; /* need a NULL at the end */ } /*