C程序执行shell管道和重定向

时间:2016-02-22 04:22:03

标签: c shell unix redirect piping

对此有点新鲜。我正在编写一个C程序,允许用户输入UNIX命令,如

ls –la | wc -l  and ls | head > file.txt

我实现了管道,并完成了少量的重定向代码。目前,我只能执行像cat file1.txt>这样的命令。 file2.txt,或带有重定向的END的其他命令。

如果我注释掉这一部分 - 所有管道链都能正常工作。

  char * srch;
  srch = strchr(cmd, '>');
  *srch++ = '\0';
  while(*srch == ' ')
  {
      ++srch;
  }

  if(srch)
  {
    dup2(open(srch, O_RDWR|O_CREAT), STDOUT_FILENO);
  }

当我尝试将管道和重定向结合到相同的命令中时,我的问题出现了,例如cmd1 p1 p2 p3< file1 | cmd2>文件2。甚至更简单的命令,如ls |头> file.txt的

任何人都可以帮我实现在同一命令中添加重定向和管道的功能。我一直在努力,但我无法理解。

这是我到目前为止所拥有的:

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

/*get args function*/

#define MAXARGS 256
char ** getargs(char * cmd) {
    // assumes that cmd ends with NULL
    char** argsarray;
    int nargs = 0;
    int nlen = strlen(cmd);
    int i = 0;
    argsarray = (char**) malloc(sizeof(char*) * MAXARGS);
    argsarray[0] = strtok(cmd," ");
    i = 0;
    while (argsarray[i] != NULL){
        i++;
        argsarray[i] = strtok(NULL," ");
    }
    return argsarray;
}


int main(void){

  pid_t childpid;
  int fd[256][2];
  char cmd[256];
  char * sepCmd[256];
  char * pch;

  printf("Please enter a command sequence: \n");
  gets(cmd);
  //scanf("%s", cmd);
  printf("You have entered: %s \n", cmd);


  printf("Attempting to split up command: \n");
  pch = strtok (cmd, "|");


  //problems here...
  char * srch;
  srch = strchr(cmd, '>');
  *srch++ = '\0';
  while(*srch == ' ')
  {
      ++srch;
  }

  if(srch)
  {
    dup2(open(srch, O_RDWR|O_CREAT), STDOUT_FILENO);
  }




  int count = 0;  
    while (pch != NULL && count < 256) {
      printf("%s\n", pch);
      sepCmd[count] = pch;
      printf("The value in this array value is: %s\n", sepCmd[count]);
      pch = strtok (NULL, "|");
      count++;
  }

  char ** argue;
  int k;

  /* Block that deals with the first command given by the user */
  k = 0;
  pipe(fd[k]);
  if(!fork()) {
        dup2(fd[k][1], STDOUT_FILENO);
        close(fd[k][0]);
        argue = getargs(sepCmd[k]);
        execvp(argue[0], argue);
        perror(argue[0]);
        exit(0);
  }

  /*Loop that will control all other comands except the last*/
  for(k = 1; k <= count - 2; k++) {
      close(fd[k-1][1]);
      pipe(fd[k]);

      if(!fork()) {
          close(fd[k][0]);
          dup2(fd[k-1][0], STDIN_FILENO);
          dup2(fd[k][1], STDOUT_FILENO);
          argue = getargs(sepCmd[k]);
          execvp(argue[0], argue);
          perror(argue[0]);
          exit(0);
      }
  }


  /*Block that will take care of the last command in the sequence*/
  k = count - 1;

  //  if(reDir){
  //argue = getargs(sepCmd[k]);
  //open(argue[count], O_RDWR);

  //if(!fork()){
  //  close(fd[0]);
  //  close(fd[1]);
  //  dup2(fd[k-1][0], STDOUT_FILENO);
  //  execl("/bin/>", argue[count]);}
  //}
  //else{

  close(fd[k-1][1]);
  if(!fork()) {
      dup2(fd[k-1][0], STDIN_FILENO);
      argue = getargs(sepCmd[k]);
      execvp(argue[0], argue);
      perror(argue[0]);
      exit(0);
  }
  // }
  while(waitpid(-1, NULL, 0) != -1);
}

1 个答案:

答案 0 :(得分:2)

评论代码错误。您正在寻找'&gt;' (srch = strchr(cmd, '>'))并且没有检查结果,使用'\ 0'终止预期结果。因此,当没有重定向时,用户会暂时获得Segmentation故障,并且当没有任何好处发生时。顺便说一句,我建议摆脱gets,因为它已被弃用。使用implicit declaration of function ‘gets’进行编译时,您将获得--std=gnu11。至于有关实施的提示,查看谷歌会显示许多不错的结果,例如:https://github.com/dan-f/my_shellhttps://github.com/jmreyes/simple-c-shell,这样您就可以查看他们是如何做到的。