I am having trouble with using execvp()
. execvp()
expects type char * const* as second parameter. I want to parse arguments passed to application (in argv
) and make an array of that type. For example, user is invoking the binary as given below:
./myapp "ls -a -l"
And then I make the below array from it:
{"ls", "-a", "-l", NULL}
Now, my code looks like:
const char* p[10];
char temp[255] = "ls -a -l";
p[0] = strtok(temp, " ");
for(i=0; i<9; i++) {
if( p[i] != NULL ) {
p[i+1] = strtok(NULL, " ");
} else {
break;
}
}
It works, but I am getting the warning:
main.c:47: warning: passing argument 2 of ‘execvp’ from incompatible pointer type
/usr/include/unistd.h:573: note: expected ‘char * const*’ but argument is of type ‘const char **’
How to do it correctly?
execvp()
? What istest
variable doing instrtok()
where I guesstemp
variable should be. Where have you defined thetest
variable? – Dungeon