title: execve("/bin/sh", 0, 0); in a pipe
body: <p>I have the following example program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int
  main(int argc, char ** argv){
      char buf[100];
  
      printf("Please enter your name: ");
      fflush(stdout);
      gets(buf);
      printf("Hello \"%s\"\n", buf);
  
      execve("/bin/sh", 0, 0);
  }
  </code></pre>
  
  <p>I and when I run without any pipe it works as it should and returns a <code>sh</code> promt:</p>
  
  <pre><code>bash$ ./a.out
  Please enter your name: warning: this program uses gets() which is unsafe.
  testName
  Hello "testName"
  $ exit
  bash$
  </code></pre>
  
  <p>But this does not work in a pipe, i think I know why that is, but I cannot figure out a solution. Example run bellow.</p>
  
  <pre><code>bash$ echo -e "testName\npwd" | ./a.out
  Please enter your name: warning: this program uses gets() which is unsafe.
  Hello "testName"
  bash$
  </code></pre>
  
  <p>I figure this has something to do with the fact that <code>gets</code> empties <code>stdin</code> in such a way that <code>/bin/sh</code> receives a EOF and promtly quits without an error message.</p>
  
  <p>But how do I get around this (without modifying the program, if possible, and not removing <code>gets</code>, if not) so that I get a promt even though I supply input through a pipe?</p>
  
  <p>P.S. I am running this on a FreeBSD (4.8) machine D.S.</p>