title: grep: (standard input): Bad file descriptor
body: <p>I am trying to change the stdin to a pipe outlet and stdout to another file and using grep.  I get the following error:</p>
  
  <pre><code>grep: (standard input): Bad file descriptor
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/stat.h&gt;
  
  int main()
  {
          pid_t childpid;
          int fds[2];
          int stat;
          int fd = open("temp", O_WRONLY | O_APPEND);
          int old_in = dup(STDIN_FILENO);
          int old_out = dup(STDOUT_FILENO);
          if ((childpid = fork()) != 0)
          {
                  wait(&amp;stat);
                  close(fds[1]);
                  dup2(fds[0], STDIN_FILENO);
                  dup2(fd, STDOUT_FILENO);
                  system("grep hello");
                  dup2(old_in, STDIN_FILENO);
                  dup2(old_out, STDOUT_FILENO);
                  close(fds[0]);
                  close(fd);
          }
  
          else if (childpid == 0)
          {
                  close(fds[0]);
                  write(fds[1], "hello how are you", 100);
                  close(fds[1]);
                  return 0;
  
          }
  
          else
          {
                  fprintf(stderr, "ERROR:\n");
                  exit(1);
          }
          return 0;
  }
  </code></pre>
  
  <p>So basically I am writing a string to the write outlet of a pipe and using the other outlet of the pipe (the read) as stdin for grep, and outputting stdout to another file.  I've removed the return from the childpid and also tried using another file as the stdin instead, but still get the same error.</p>