I am trying to do something quite easy: fill an char** with arguments I want to use in a execvp call in C.
This is how I am doing:
if(argc >=1)
{
*nargv = "--action";
while(argc--)
{
printf("nargv1 => %s \t argv1 => %s \n", *nargv, *argv);
*++nargv = *argv++;
printf("nargv2 => %s \t argv2 => %s \n", *nargv, *argv);
}
printf("nargv3 => %s \t argv3 => %s \n", *nar开发者_开发百科gv, *argv);
*nargv++ = '\0';
printf("nargv4 => %s \t argv4 => %s \n", *nargv, *argv);
}
The output gives me:
nargv1 => --action argv1 => backup
nargv2 => backup argv2 => --help
nargv1 => backup argv1 => --help
nargv2 => --help argv2 => (null)
nargv3 => --help argv3 => (null)
nargv4 => (null) argv4 => (null)
This sounds ok (nargv is filled correctly, at least that's what I thought)> But when I do execvp("command",nargv) my arguments are not passed through. What's wrong ? I have tried to play with gdb with no success.
Regards
Since you do ++nargv
, your nargv pointer ends up pointing past the end of the array. Retain a pointer to the initial member and pass that to exec. Also, *nargv++ = '\0'
looks like a bug, since you assign a char to a pointer to char.
精彩评论