Created
October 24, 2012 20:18
-
-
Save jontonsoup/3948588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void RunCmdPipeRecurse(commandTLinked* cmd){ | |
int fd[2]; | |
pipe(fd); | |
int grandchild_status = -1; | |
if(cmd->next != NULL){ | |
pid_t grandchild; | |
grandchild = fork(); | |
if(grandchild == 0) // grandchild process | |
{ | |
close(fd[0]); //close read from pipe | |
dup2(fd[1], STDOUT_FILENO); // Replace stdout with the write end of the pipe | |
close(fd[1]); | |
RunCmdPipeRecurse(cmd->next); | |
ResolveExternalCmd(cmd->cmd); | |
grandchild_status = execv(cmd->cmd->name, cmd->cmd->argv); | |
if(grandchild_status == -1){ | |
perror("Execv Fail"); | |
} | |
exit(1); | |
} | |
else //child process | |
{ | |
close(fd[1]); //close write to pipe | |
dup2(fd[0], STDIN_FILENO); // Replace stdin with the read end of the pipe | |
close(fd[0]); | |
grandchild_status = waitpid(grandchild, &grandchild_status, 0); | |
if(grandchild_status == -1){ | |
perror("Wait Fail"); | |
} | |
grandchild_status = WEXITSTATUS(grandchild_status); | |
exit(grandchild_status); | |
} | |
} | |
else{ | |
close(fd[0]); //close read from pipe | |
dup2(fd[1], STDOUT_FILENO); // Replace stdout with the write end of the pipe | |
close(fd[1]); | |
ResolveExternalCmd(cmd->cmd); | |
grandchild_status = execv(cmd->cmd->name, cmd->cmd->argv); | |
if(grandchild_status == -1){ | |
perror("Execv Fail"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment