Hello guys!
I am writing my own shell, and while the basics are complete I came upon a problem.
At the current moment Im trying to implement multiple Pipes and redirections in my command line. I am unsure of how to handle this, as there could be 2 pipes or 3 pipes, and even redirections along with the pipe. I've been looking on pipe() and etc. and came up with an example test below:
Code:
int main(int argc, char **argv, char **env)
{
int p[2];
char *ls[] = {"/bin/ls", "-la", NULL};
char *wc[] = {"/usr/bin/wc", NULL};
char *less[] = {"/usr/bin/less", NULL};
pipe(p);
if (fork() == 0)
{
close(p[0]);
dup2(p[1], STDOUT_FILENO);
execve("/bin/ls", ls, env);
}
if (fork() == 0)
{
close(p[1]);
dup2(p[0], STDIN_FILENO);
dup2(p[1], STDOUT_FILENO);
close(p[0]);
execve("/usr/bin/wc", wc, env);
}
if (fork() == 0)
{
close(p[1]);
dup2(p[0], STDIN_FILENO);
execve("/usr/bin/less", less, env);
}
close(p[0]);
close(p[1]);
wait(NULL);
wait(NULL);
wait(NULL);
return (0);
}
This is just an example of 2 pipes and 3 commands. But how could I make it so that it is adaptable at the number of pipes? And what about redirections? All Im looking is for some advice from anyone who has or hasn't experience in this type of thing.
Thanks guys in advance!