I am working to create a tiny shell in C that can accept multiple commands and pipe them together as needed
I have the below code, it is accepting commands and piping them together but it is quitting my tiny shell after running one command (I beleive an EOF is being returned). I want it to be able to accept many lines of commands.
Any tips/advice would greatly be appreciated!
int i;
pid_t children[numberOfCommands - 1];
pid_t groupId = -1;
for (i = 1; i < numberOfCommands - 1; i++)
{
int pd[2];
if(pipe(pd) < 0){
unix_error("Pipe failed");
}
pid = fork();
children[i] = pid;
if (!pid)
{
dup2(pd[1], 1);
if (execve(argv[cmds[i]], argv, environ) < 0)
{
unix_error("First Execve failed");
}
}
else
{
if (i == 0)
{
groupId = pid;
}
setpgid(pid, groupId);
}
dup2(pd[0], 0);
close(pd[0]);
close(pd[1]);
}
if (execve(argv[0], argv, environ) < 0) // I believe this is where the EOF is coming from
{
unix_error("Second Execve failed");
}
for (int j = 0; j < numberOfCommands; j++)
{
waitpid(children[j], NULL, 0);
}