在C中实现具有多个管道的shell

时间:2014-11-06 08:03:19

标签: c pipe posix

我正在尝试创建一个可以处理具有多个管道和输入/输出重定向的命令的shell。管道大部分都在工作,但问题出在我输入的时候:

ls | more | wc 
or 
ls | more | wc > output.txt 

输出是:

[linux]$ ./a.out 

shell> ls | more | wc   <---- I entered the command here 
In Child executing 0
First Child executing 0
In Child executing 2
Last Child executing 2
In Child executing 1
Middle Child executing 1

shell>      20      20     137   <----- The output is on the shell command line
                             <------ The cursor ends up here so I pressed enter
Segmentation fault
[linux]$ ./a.out   <--- started the shell back up

shell> ls | more | wc > output.txt    <----- entered in the command with output redirection
In Child executing 0
First Child executing 0
In Child executing 1
Middle Child executing 1
In Child executing 2
Last Child executing 2
wc: >: No such file or directory

shell> 

代码:

    numOfCommands = numOfPipes + 1;
    int pid = 0;
    int leftpipe[2];
    int rightpipe[2];
    for (i = 0; i <= numOfPipes; i++) // loop for the pipes
    {
        if (numOfPipes > 0) {
            pipe(rightpipe); // create right pipe
        }
        pid = fork();
        if (pid > 0) //parent
        {
            if (i > 0) {
                close(leftpipe[0]);
                close(leftpipe[1]);
            }
            leftpipe[0] = rightpipe[0];
            leftpipe[1] = rightpipe[1];
        } else if (pid == 0) //child
        {
            fprintf(stdout, "In Child executing %d\n", i);
            fflush(stdout);
            if (i == 0 && in_red == 1) // first child and input redirection found
            {
                fprintf(stdout, "In Child input redir %d\n", i);
                fflush(stdout);
                close(0); //close stdin
                fp = open(argv[2], O_RDWR | O_CREAT | O_APPEND, 0600);
                dup2(fp, 0);
                argv[1] = NULL; // set the file name to NULL
                argv[2] = NULL; // set < to NULL
            }
            if (numOfPipes > 0) {
                if (i == 0) { //first child
                    fprintf(stdout, "First Child executing %d\n", i);
                    fflush(stdout);
                    close(1); //close stdout
                    dup(rightpipe[1]); //make stdout=rightpipe
                    close(rightpipe[1]); //close write end of rightpipe
                    close(rightpipe[0]); //close read in of rightpipe

                }
                if (i > 0 && i < numOfPipes) { //middle child
                    fprintf(stdout, "Middle Child executing %d\n", i);
                    fflush(stdout);
                    close(0);
                    dup(leftpipe[0]);
                    close(leftpipe[0]);
                    close(leftpipe[1]);

                    close(1);
                    dup(rightpipe[1]);
                    close(rightpipe[1]);
                    close(rightpipe[0]);

                }
                if (i == numOfPipes) { //last child
                    fprintf(stdout, "Last Child executing %d\n", i);
                    fflush(stdout);
                    close(0);
                    dup(leftpipe[0]);
                    close(leftpipe[0]);
                    close(leftpipe[1]);

                }

            }
            if (i == numOfPipes && out_red == 1) // output redirection found
            {
                close(1); // close stdout
                fp = open(argv[sizeOfArgs - 1], O_RDWR | O_CREAT | O_APPEND,
                        0600);
                dup2(fp, 1); // dup stdout to the file
                argv[sizeOfArgs - 1] = NULL; // set the file name to null
                argv[sizeOfArgs - 2] = NULL; // set > to null

            }
            indexOfCommands = startOfCommands[i]; //each index of startOfCommands holds the placement to the next command in argv
            execvp(argv[indexOfCommands], argv + indexOfCommands);
        }

    }

        wait(&pid, 0, 0);
    close(fp); // close the file
 }
}

管道正在正确执行,除了重定向,但由于某种原因,它将转向stdin而不是stdout然后导致分段错误,不知道为什么会这样。 任何帮助将不胜感激。

0 个答案:

没有答案