Using pipe to have one command read from another , command1 | command2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dblack305
    New Member
    • Mar 2010
    • 1

    Using pipe to have one command read from another , command1 | command2

    Im having difficulty writing this program, basically im trying to use a Pipe to read from one command and write to another. For this example i have been trying ls | sort -r . Its executing the ls command but not writing it into the sort -r command. Below is is what i have so far, Can anyone assist, Thanks.


    CPipe(char* command1[], char* command2[])

    {

    pid_t pid;
    int fd[2];

    /* get a pipe
    if (pipe(fd) == -1)
    {
    error("cissh: error creating pipe");
    exit(1);


    pid = fork();

    if (pid < 0) {
    error("cissh: error creating fork");
    exit(1);
    }



    if ( pid > 0 ) {

    close(fd[1]);

    if(dup(fd[0]) < 0)
    {
    error("cissh: error duplicating standard input ");
    perror("dup()") ;
    exit(1);

    }

    close(fd[0]);
    execvp(command2[0],command2);

    }



    close(fd[0]);

    if(dup(fd[1]) < 0)
    {
    error("cissh: error duplicating standard output ");
    perror("dup()") ;
    exit(1);
    }

    close(fd[1]);
    execvp(command1[0],command1);
    }
Working...