mullan123 0 Newbie Poster

after tokenising my string i am the tokens to the method below and counting the tokens. if token number is one, that means i have one command only and pass it from execvp.. if i have a second token then pass that token from the second execvp..

This gives me no error as well as no output... Also it will not work with 3 or more commands i suppose.

Please show me the way how I can fix it...

Thanks...

count ++ ;

    if(count == 1) 
    {
            command1 = token;

    }
    if (count == 2) 
    {
            command2 =  token;

    }

    int pipedes[2];         // pipe file descriptors
pid_t pid1, pid2;       // child process pids

if (pipe(pipedes) == -1)    // open a pipe
{
    fprintf(stderr, "%s: cannot open pipe: %s\n", argv[0],
        strerror(errno));
    exit(EXIT_FAILURE);
}





if ((pid1 = fork()) == -1)
{
    fprintf(stderr, "%s: cannot create child 1: %s\n", argv[0],
        strerror(errno));
}
else if (pid1 == 0) {
    close(pipedes[0]);      // close the reading end of the pipe

            if (dup2(pipedes[1], STDOUT_FILENO) < 0)
            {
                    perror("child 1: cannot redirect stdout to pipe");
            }
            close(pipedes[1]);      // this is now STDOUT_FILENO

            execvp(*command1, command1);

    }


if ((pid2 = fork()) == -1)
{
    fprintf(stderr, "%s: cannot create child 2: %s\n", argv[0],
        strerror(errno));
}
else if (pid2 == 0) {
    close(pipedes[1]);      // close the writing end of the pipe

            if (dup2(pipedes[0], STDIN_FILENO) < 0) {

                    perror("child 2: cannot redirect stdin from pipe");
            }
            close(pipedes[0]);      // this is now STDIN_FILENO

            execvp(*command2, command2);
    }

close(pipedes[0]);
close(pipedes[1]);

pid_t pid;
int status;
while ((pid = wait(&status)) >= 0)
{
    if (WEXITSTATUS(status) != 0)
        fprintf(stderr, "%s: child %d exited with code %d\n",
            argv[0], pid, WEXITSTATUS(status));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.