title: execv* and write in stdin
body: <p>I'm trying to run a program with a specific standard input. I succeed by using a file descriptor of a file where there is what I want to put in the stdin, but I fail to write directly on the stdin :</p>
  
  <pre><code>$cat input.test
  echo Hello
  $
  </code></pre>
  
  <p>Code C :</p>
  
  <pre><code>int main(int argc, char **argv)
  {
    int fd = 0;
  
    fd = open("input.test", O_CREAT);
    close(STDIN_FILENO);
    dup2(fd, STDIN_FILENO);
    char *const args[] = { "bash", NULL };
    execvp("bash", args);
  }
  </code></pre>
  
  <p>That works :</p>
  
  <pre><code>$./a.out
  Hello
  $
  </code></pre>
  
  <p>But if I try to write directly on the STDIN using pipe the program displays nothing and keeps running :</p>
  
  <pre><code>int main(int argc, char **argv)
  {
    int fds[2];
  
    pipe(fds);
    close(STDIN_FILENO);
    dup2(fds[1], STDIN_FILENO);
    write(fds[1], "echo Hello;", 11); // Résults are identics with fds[0]
    char *const args[] = { "bash", NULL };
    execvp("bash", args);
  }
  </code></pre>
  
  <p>Thanks for your help</p>
  
  <p>Cordially,
  Bastien.</p>
  
  <p>EDIT Problem solved:</p>
  
  <p>Thanks for your answers, here the code which works :</p>
  
  <pre><code>int main(void)
  {
      int fd[2];
      pid_t pid;
  
      if (pipe(fd) &lt; 0)
          return EXIT_FAILURE;
  
      if ((pid = fork()) &lt; 0)
          return EXIT_FAILURE;
      else if (pid != 0) { /* father */
          close(fd[1]);
          dup2(fd[0], STDIN_FILENO);
          execlp("bash", "bash", (char *)0);
      } else { /* son */
          close(fd[0]);
          write(fd[1], "echo hello\n", 11);
      }
  
      return EXIT_SUCCESS;
  }
  </code></pre>