簡體   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