I am writing a simple shell in C. It is actually coming along quite well with i/o redirection and such. One thing that I'd like to add is a way to switch between versions of the exec* functions. Right now I'm sticking to execlp(), execvp(), and execve().
I have all the arguments I want to pass in an array called argv. It is a null terminated array of null terminated strings, so it works fine with execv*, but I can't think of how to get it to work with execlp().
This is what I have now:
if (strcmp(exec_opt, "vp") == 0)
error = execvp(argv[0], argv); /* Execute vp */
else if(strcmp(exec_opt, "lp") == 0)
error = execlp(argv[0], "", argv); /* Execute lp */
else if(strcmp(exec_opt, "ve") == 0)
error = execve(argv[0], argv, environ); /* Execute ve */
else
{
// throw errors about exec_opt
}
if(error != 0)
{
// do something about it
}
In this configuration the compiler doesn't baff at the syntax, but it also doesn't work. I've also tried
error = execlp(argv[0], (char*) argv); /* As a single string */
char* argv1 = argv[1]; /* don't pass command itself */
error = execlp(argv[0], argv1);
Which do various odd but ultimately incorrect things. Is there a way for me to turn my array into a variable argument list? passing it directly (which makes the most type-sense, since variable argument lists are char* argv[]
) yields a compiler error about casting incompatible pointers.