I have written a program that gets command line arguments such as ls
, cat
and executes them. Now I need to extend this program for i/o redirection and do shell pipes as well.
Here is my program for the simple shell.
if ((pid = fork()) == -1 ) { /* error exit - fork failed */
perror("Fork failed");
exit(-1);
}
if (pid == 0) { /* this is the child */
printf("This is the child ready to execute: %s\n",argv[1]);
execvp(argv[1],&argv[1]);
perror("Exec returned");
exit(-1);
} else {
wait(pid,0,0);
printf("The parent is exiting now\n");
...
}
I don't know how to add pipes and redirection in this same program!
dup(pipeID[0]);
close(pipeID[0]);
close(pipeID[1]);
execlp(argv[3],argv[3],argv[4],0);
I know that I have to use dup()
or dup2()
for redirection and pipe()
too, but how do I do it all together in the same program?