管道错误:int不是函数

时间:2016-09-13 17:40:11

标签: c shell pipe

我正在使用此代码尝试在一个简单的shell中实现我自己的管道。但是,它不会编译,因为它告诉我fd不是一个函数。我看到这个实现的每个其他地方都以fd作为pipe()的参数以完全相同的方式。我不知道为什么我会收到这个错误。

int startProcess (StringArray sa)
{
  int pid; 
  int status;
  int fd1;
  int fd2; 
  int current_in;
  int current_out;
  int fd0;
  int fd00;
  int in = 0;
  int out = 0; 
  char input[64]="";
  char output[64]="";
  char cmd1[64] ="";
  char cmd2[64] ="";
  int pipe = 0; 
  int fd[2]; 

  switch( pid = fork()){
 case -1://This is an error 
   perror("Failure of child.");
   return 1;
 case 0: // This is the child
   // Redirection

   /* finds where '<' or '>' or '|' occurs and make that sa[i] = NULL ,
      to ensure that command wont' read that*/

    for(int i=0;sa[i]!='\0';i++)
    {
        if(strcmp(sa[i],"<")==0)
        {        
            sa[i]=NULL;
            strcpy(input,sa[i+1]);
            in=2;           
        }               

        if(strcmp(sa[i],">")==0)
        {      
            sa[i]=NULL;
            strcpy(output,sa[i+1]);
            out=2;
        }
        if(strcmp(sa[i],"|")==0)
        {
            sa[i]=NULL;
            strcpy(cmd1,sa[i-1]);
            strcpy(cmd2,sa[i+1]);
            pipe=2; 
        }

    }

    //if '<' char was found in string inputted by user
   (erased for brevity) 


    //if '>' char was found in string inputted by user 
    (erased for brevity) 

     //if '|' char was found in string inputted by user
    if(pipe)
    {
        pipe(fd); 
        if (!fork)
        {
            close(1);
            dup(fd[1]); 
            close(fd[0]);
            int error; 
            error = 0; 
            if (fork() == 0){
                error = execvp(cmd1, sa); 

            } if (error == -1) {
                printf("ERROR: unknown command (%s)\n)", cmd1); 
                exit(0); 
            } else {
                waitpid(0,NULL,0); 
            }
        }
    } else {
        close(0);
        dup(fd[0]);
        close(fd[1]);
        execvp(cmd2, sa); 
    }

          execvp(sa[0], sa);
          perror("execvp");
          _exit(1);


    printf("Could not execute '%s'\n", sa[0]);
  default:// This is the parent 
   wait(&status);
   return (status == 0) ? 0: 1;
  }
}

1 个答案:

答案 0 :(得分:2)

首先你有

int pipe = 0; 

然后你也有

pipe(fd);

两者都不正确。

我建议你重命名变量。