繁体   English   中英

C中的壳管系统

[英]Shell pipe system in C

我正在尝试为我的外壳制作管道系统,但是它没有按预期工作。

void pipes (char *listaCommand[], int end, char **argv)  
{  
    int cont = end;  
    for (cont;listaCommand[cont]; cont++)  
    {  
        if (listaCommand[cont] != NULL)  
        {
            if (!strcmp(listaCommand[cont],"|")){
                int pid2, status;
                int pipefd[2], ret;

                listaCommand[cont] = NULL;

                ret = pipe (pipefd);
                if (ret < 0) fatal();

                /* Now fork. */

                pid2 = fork ();
                if (pid2 <0) fatal ();

                if (pid2 > 0)
                {
                    printf ("P: waiting for child\n");
                    wait (&status);     
                    close(STDIN_FILENO);
                    dup(pipefd[0]);
                    close(pipefd[0]);
                    close(pipefd[1]);
                    /*execvp (auxCommand[0], auxCommand);*/
                    pipes(listaCommand, cont+1, argv);
                    /*break;*/
                }
                else
                {
                    close (STDOUT_FILENO);
                    dup (pipefd[1]);
                    close (pipefd[1]);  
                    close (pipefd[0]);

                }

            }
        }
    }
    if (end >= 3)
    {
        printf("%s \n", listaCommand[end-1]);
    }
    execvp (listaCommand[end], listaCommand);
    printf ("%s: command not found.\n", listaCommand[end]); /* Exec failed. */
    exit(EXIT_FAILURE);
}

如果我使用ls | 排序,它可以工作,但是如果ls有任何参数,它就不工作,因为出于某种原因,listaCommand [cont]其中==“ |” 不是NULL,所以我只能得到ls:选项-'a'无效。

listaCommand have  
[0] = "ls"  
[1] = "-al"  
[2] = "|"  
[3] = "sort"  

您不需要传递end参数,而是将指针增加到命令数组。 您正在将初始数组传递给execvp调用,因此它将尝试多次执行ls 此外,在将listaCommand[cont]设置为NULL之后,您需要一个break语句,因为在迭代后cont增加了。 另外,我认为您需要保护execvp调用,以便父级在处理完成后不会调用它。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define fatal() exit(1)
void pipes (char *listaCommand[], char **argv)  
{  
    printf("pipes %s\n", listaCommand[0]);
    int cont = 0;
    for (;listaCommand[cont]; cont++)  {  
        if (listaCommand[cont][0] == '|'){
            int pid2, status;
            int pipefd[2], ret;

            listaCommand[cont] = NULL;

            ret = pipe (pipefd);
            if (ret < 0) fatal();

            /* Now fork. */

            pid2 = fork ();
            if (pid2 <0) fatal ();

            if (pid2 > 0)
            {
                printf ("P: waiting for child\n");
                wait (&status);     
                close(STDIN_FILENO);
                dup(pipefd[0]);
                close(pipefd[0]);
                close(pipefd[1]);
                /*execvp (auxCommand[0], auxCommand);*/
                pipes(listaCommand + cont + 1, argv);
                /*break;*/
            }
            else
            {
                close (STDOUT_FILENO);
                dup (pipefd[1]);
                close (pipefd[1]);  
                close (pipefd[0]);
                break;
            }

        }
    }
    if (listaCommand[0]) {
      execvp (listaCommand[0], listaCommand);
      printf ("%s: command not found.\n", listaCommand[0]); /* Exec failed. */
      exit(EXIT_FAILURE);
    }
}

int main() {
    char *args[] = { "ls", "-al", "|", "sort", "|" , "tr", "[a-z]", "[A-Z]", 0 };
    pipes(args, 0);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM