I must be missing something here. I'm trying to use execvp to execute a command that a user passes to it. The way I'm doing this is by creating an arg_vector that stores the tokens of the command. Here's the code fragment:
Code:
char **arg_vector = NULL;
char *running = NULL;
char *token = NULL;
int i = 0;
int total_tokens = 0;
/* count_tokens returns the correct number of tokens delimited by whitespace */
total_tokens = count_tokens(command, DELIM);
arg_vector = (char **)malloc(sizeof(char **) *(total_tokens + 1));
if (arg_vector == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
running = strdupa(command);
/* fill up arg_vector with the tokens */
while ((token = strsep(&running, DELIM)) != NULL) {
arg_vector[i] = token;
i++;
}
arg_vector[total_tokens + 1] = NULL;
execvp(arg_vector[0], arg_vector);
If I pass the command as one of the following, they work just fine:
ls -l
ls -l /etc
ls /etc /bin
However, if I pass "ls -l /etc /bin" it fails. I'm not sure what it could be. Any hints would be much appreciated.
Thanks.