将用户输入管道输入C中的命令

时间:2017-05-09 09:28:57

标签: c shell piping

所以我创建了一个简单的shell程序,它通过cmd从用户那里获取输入,然后创建一个新任务并将输入作为命令执行。例如:

  

ls -l / home / user / Downloads | sort -u> listing.txt

但我的程序只能处理一个管道。我可以以某种方式使其工作没有限制管道?例如:

  

ls -l / home / user / Downloads | sort -u | wc -l> count.txt

到目前为止,我一直无法实现类似的东西。

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

int main(void) 
{
    pid_t pid1,pid2,waitPid;
    int  status,n,j,k,in,out,pdes[2],current_out,current_in,returnValue;
    char *token;
    char array[256];
    char *temp[256];
    char *temp2[256];
    current_out=dup(1);
    current_in=dup(0);


    for(;;)
    {
        if(dup2(current_out,1) < 0)
        {
            printf("Error restoring output");
        }
        if(dup2(current_in, 0) < 0)
        {
            printf("Error restoring input");
        }
        int i=0;
        k=0;
        printf("mysh4>");
        int check = scanf("%d",&n);
        if(check == EOF)
        {
            printf("You now exit.\n");
            break; 
        }
        else
        {
            fgets(array,sizeof(array),stdin);
            array[strcspn(array,"\n")] = 0;
            token = strtok(array, " ");

            while(token != NULL)
            { 
                temp[i] = token;
                token = strtok(NULL, " ");
                i++;
                k++;
            }
            temp[i]= NULL;
            int p=0;
            int qq;

            for(i=0;i<k;i++)
            {
                if(strcmp(temp[i],"|") == 0)
                {
                    for(qq=i+1; qq<k; qq++)
                    {
                        temp2[p]=temp[qq];  
                        temp[qq-1] = NULL;  
                        printf("Temp2[%d] is: %s\n" , p , temp2[p]);    
                        printf("Temp1[%d] is: %s\n" , p , temp[p]); 
                        p++;
                    }
                    temp2[p]= NULL;
                }
            }

            returnValue=pipe(pdes);

            if (returnValue == -1) 
            {
                printf("ERROR: Pipe command failed.\n");
                return -1;
            }
            close(pdes[0]);
            close(pdes[1]);
            pid1 = fork();

            if (pid1 < 0) 
            {
                perror("ERROR: Fork failed.\n");
                return -1;
            }

            //child process
            if (pid1 == 0) 
            {
                close(pdes[0]);
                dup2(pdes[1],1);
                printf("Array:%s", array);
                execvp(array,temp);
                printf("Invalid Code\n");
                return -1;
            }

            pid2 = fork();
            if(pid2 < 0)
            {
                perror("Error: Fork2 failed.\n");
                return -1;
            }

            //child process 2
            if(pid2 == 0)
            {
                close(pdes[1]);
                dup2(pdes[0],0);
                for(j=0;j<k;j++)
                {
                    if(strcmp(temp[j],">") == 0)
                    {
                        out=j+1; 
                        printf("Output is %s\n", temp[out]);
                        temp[j] = temp[out];
                        pdes[1]=open(temp[out],
                         O_APPEND | O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
                        if(dup2(pdes[1],1) < 0)
                        {
                            printf("error in dup2");
                            return -1;
                        }
                    }
                }
                execvp(temp2[0],temp2);
                printf("Invalid Code 2\n");
                return -1;
            }

            //father process
            else 
            {
                //father waits child process to finish
                waitPid = wait(NULL);
                if (waitPid == -1) 
                {
                    perror("ERROR: Waitpid failed.\n");
                    return -1;
                }
            }
        }    
    }

    return 0;
}

0 个答案:

没有答案