I have a function that takes an array of strings and the size of the array. I have a loop that tokenizes the string into command and arguements and then forks and executes the commands one by one. For each command I need to pipe the output to the next commands input. I'm not entirely sure if my dup/close calls are correct. Any help would be appreciated. I have it set to not pipe if only one command in array.
void runcmds(char **cmds, int count){
int fd[2];
int index;
pid_t pID;
for (index = 0; index < count; index++){
//tokenize string
token *newToken = mytoken(cmds[index]);
if (count > 1){ //pipe if more than one command
pipe(fd);
}
pID = fork();
if (pID < 0){ //fork failed
printf("Error: fork\n");
exit(1);
}
else if (pID == 0){
if (count == 1){
; //no need to pipe
}
//if first command of multiple commands
else if ((index == 0) && (count > 1)){
close(1); //close stdout
dup(fd[1]); //replace stdout with pipe write
close(fd[0]);//close pipe read
}
//if last command
else if (index == (count - 1)){
close(0); //close stdin
dup(fd[0]); //replace stdin with pipe read
close(fd[1]); //close pipe write
//if middle command
else{
close(0);
dup(fd[0]);
close(1);
dup(fd[1]);
}
//execute command
if (execvp(newToken->command, newToken->args) < 0){
//execvp failed
printf("Error: execvp\n");
exit(1);
}
}
waitpid(pID, NULL, 0);
if ((index == 0) && (count > 1)){
close(fd[1]);
}
else if (index == (count - 1)){
close(fd[0]);
}
else{
; //middle commands need both open
}
}
return;
}