problem with piping in my own implementation of shell

Posted by codemax on Stack Overflow See other posts from Stack Overflow or by codemax
Published on 2010-04-15T06:35:51Z Indexed on 2010/04/15 6:43 UTC
Read the original article Hit count: 332

Filed under:

Hey guys, i am implementing my own shell. I want to involve piping. i searched here and i got a code. But it is not working.Can any one help me? this is my code

#include <sys/types.h>  
#include <sys/wait.h>  
#include <sys/ipc.h>  
#include <fcntl.h>  
#include <unistd.h>  
#include <string.h>  
#include <iostream>  
#include <cstdlib>  
using namespace std;  

char temp1[81][81],temp2[81][81] ,*cmdptr1[40], *cmdptr2[40];  
void process(char**,int);  
int arg_count, count;  
int arg_cnt[2];  
int pip,tok;  
char input[81];  
int fds[2];  

void process( char* cmd[])//, int arg_count )  
{  
    pid_t pid;  
    pid = fork();    
    //char path[81];  
    //getcwd(path,81);  
    //strcat(path,"/");  
    //strcat(path,cmd[0]);  
    if(pid < 0)  
    {  
        cout << "Fork Failed" << endl;  
        exit(-1);  
    }  
    else if( pid == 0 )  
    {  
          execvp( cmd[0] , cmd );  
    }  
        else  
        {  
        wait(NULL);  
    }  
}  
void pipe(char **cmd1, char**cmd2)  
{  
    cout<<endl<<endl<<"in pipe"<<endl;  
    for(int i=0 ; i<arg_cnt[0] ; i++)  
    {  
        cout<<cmdptr1[i]<<" ";  
    }  
    cout<<endl;  
    for(int i=0 ; i<arg_cnt[1] ; i++)  
    {  
        cout<<cmdptr2[i]<<" ";  
    }  
    pipe(fds);  
if (fork() == 0 ) {  
  dup2(fds[1], 1);  
  close(fds[0]);  
  close(fds[1]);  
  process(cmd1);  
}  
if (fork() == 0) {  
  dup2(fds[0], 0);  
  close(fds[0]);  
  close(fds[1]);  
  process(cmd2);  
}  
close(fds[0]);  
close(fds[1]);  
wait(NULL);  
}  

void pipecommand(char** cmd1, char** cmd2)   
{  
    cout<<endl<<endl;  
    for(int i=0 ; i<arg_cnt[0] ; i++)  
    {  
        cout<<cmd1[i]<<" ";  
    }  
    cout<<endl;  
    for(int i=0 ; i<arg_cnt[1] ; i++)  
    {  
        cout<<cmd2[i]<<" ";  
    }  
    int fds[2]; // file descriptors  
    pipe(fds);  
    // child process #1  
    if (fork() == 0)  
    {  
            // Reassign stdin to fds[0] end of pipe.  
            dup2(fds[0], STDIN_FILENO);  
            close(fds[1]);  
            close(fds[0]);  
            process(cmd2);  
            // child process #2  
            if (fork() == 0)   
        {  
                // Reassign stdout to fds[1] end of pipe.  
                dup2(fds[1], STDOUT_FILENO);  
                close(fds[0]);  
                close(fds[1]);  
                // Execute the first command.  
                process(cmd1);  
            }  
            wait(NULL);  

        }  
        close(fds[1]);  
        close(fds[0]);  
        wait(NULL);  
}  

void splitcommand1()  
{  
    tok++;  
    int k,done=0,no=0;  
    arg_count = 0;  
    for(int i=count ; input[i] != '\0' ; i++)  
    {  
        k=0;  
        while(1)  
        {     
            count++;              
            if(input[i] == ' ')  
            {  
                break;  
            }  
            if((input[i] == '\0'))  
            {  
                done = 1;  
                break;  
            }  
            if(input[i] == '|')  
            {  
                pip = 1;  
                done = 1;  
                break;  
            }  
            temp1[arg_count][k++] = input[i++];  
        }  
        temp1[arg_count][k++] = '\0';  
        arg_count++;  
        if(done == 1)  
        {  
            break;  
        }  

    }  
    for(int i=0 ; i<arg_count ; i++)  
    {  
        cmdptr1[i] = temp1[i];  
    }  

    arg_cnt[tok] = arg_count;  

}  
void splitcommand2()  
{  
    tok++;  

    cout<<"count is :"<<count<<endl;  
    int k,done=0,no=0;  
    arg_count = 0;  
    for(int i=count ; input[i] != '\0' ; i++)  
    {  
        k=0;  
        while(1)  
        {     
            count++;              
            if(input[i] == ' ')  
            {  
                break;  
            }  
            if((input[i] == '\0'))  
            {  
                done = 1;  
                break;  
            }  
            if(input[i] == '|')  
            {  
                pip = 1;  
                done = 1;  
                cout<<"PIP";  
                break;  
            }  

            temp2[arg_count][k++] = input[i++];  
        }  
        temp2[arg_count][k++] = '\0';  
        arg_count++;  
        if(done == 1)  
        {  
            break;  
        }  

    }  
    for(int i=0 ; i<arg_count ; i++)  
    {  
        cmdptr2[i] = temp2[i];  
    }  
    arg_cnt[tok] = arg_count;  

}  
int main()  
{  
    cout<<endl<<endl<<"Welcome to unique shell !!!!!!!!!!!"<<endl;  
    tok=-1;  
    while(1)  
    {  
        cout<<endl<<"***********UNIQUE**********"<<endl;  
        cin.getline(input,81);  
        count = 0,pip=0;  
        splitcommand1();  


        if(pip == 1)  
        {  
            count++;  
            splitcommand2();  
        }  
        cout<<endl<<endl;  



        if(strcmp(cmdptr1[0], "exit") == 0 )  
        {  
            cout<<endl<<"EXITING UNIQUE SHELL"<<endl;  
            exit(0);  
        }  
        //cout<<endl<<"Arg count is :"<<arg_count<<endl;  
        if(pip == 1)  
        {  
            cout<<endl<<endl<<"in main :";  
        for(int i=0 ; i<arg_cnt[0] ; i++)  
        {  
            cout<<cmdptr1[i]<<" ";  
        }  
        cout<<endl;  
        for(int i=0 ; i<arg_cnt[1] ; i++)  
        {  
            cout<<cmdptr2[i]<<" ";  
        }  
            pipe(cmdptr1, cmdptr2);  
        }  
        else  
        {  
            process (cmdptr1);//,arg_count);  
        }  
    }  
}  

I know it is not well coded. But try to help me :(

© Stack Overflow or respective owner

Related posts about shell