Implementing Pipes in a C shell (Unix)

Basically I have created a shell using standard POSIX commands, I want to be able to Implement Piping as well. Right now it handles commands correctly, and can do background processing with &. But I need to be able to pipe using | and >> as well. For example something like this: cat file1 file2 >> file3 cat file1 file2 | more more file1 | grep stuff Here is the code I have currently. I also want to AVOID "SYSTEM" calls. I know U need to use dup2, but the way I did my code is a bit odd, so im hoping if someone can tell me if it is feasible to implement pipes in this code? thanks! I know dup2 is used, but also im def. confused at how to implement >> as WELL as |

#include #include #include #include #include #include #include #include using namespace std; void Execute(char* command[],bool BG) < //Int Status is Used Purely for the waitpid, fork() is set up like normal. int status; pid_t pid = fork(); switch(pid) < case 0: execvp(command[0], command); if(execvp(command[0], command) == -1) < cout default: if(BG == 0) < waitpid(pid, &status, 0); //Debug cout > > bool ParseArg(char* prompt, char* command[], char Readin[],bool BG) < fprintf(stderr, "myshell>"); cin.getline(Readin,50); prompt = strtok(Readin, " "); int i = 0; while(prompt != NULL) < command[i] = prompt; if(strcmp(command[i], "&") == 0)< //Debug cout //Debug cout return false; > void Clean(char* command[]) < //Clean Array for(int a=0; a < 50; a++) < command[a] = NULL; >> int main() < char* prompt; char* command[50]; char Readin[50]; bool BG = false; while(command[0] != NULL) < Clean(command); BG = ParseArg(prompt, command, Readin, BG); if(strcmp(command[0], "exit") == 0 || strcmp(command[0], "quit") == 0 ) < break; >else < Execute(command,BG); >> return 1; > 
asked Oct 14, 2010 at 5:30 user475353 user475353

Why were you trying to avoid system calls? Portability? You may stick with the POSIX specified system calls as much as possible. Also, your shell is a strange mix of C and C++.