C中的壳管系统 [英] Shell pipe system in C

查看:86
本文介绍了C中的壳管系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a pipe system for my shell, but it's not working as intended.

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'无效.

If I use commands like ls | sort, it works, but if ls has any argument, it doesnt work, because for some reason, listaCommand[cont] where its == "|" is not NULL, so I just get ls: option -- 'a' invalid.

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

推荐答案

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

You don't need to pass the end argument, instead increment the pointer to your command array. You are passing the initial array to the execvp call so it tries to execute ls multiple times. Further, you need a break statement after setting the listaCommand[cont] to NULL because after the iteration cont is incremented. Also I think you need to protect the execvp call so that the parent does not call it after the processing is done.

#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;
}

这篇关于C中的壳管系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆