title: Passing a file descriptor from `open_memstream` to `dup2`
body: <p>I am trying to redirect output from an <code>exec()</code>ed function into a buffer, so I though I would try and use <code>open_memstream</code> to handle the dynamic buffering</p>
  <p>I put together this to test it out:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main() {
      char* buffer;
      size_t buffer_len;
      FILE* stream = open_memstream(&amp;buffer, &amp;buffer_len);
      if(!stream) perror(&quot;Something went wrong with `open_memstream`!&quot;);
      fflush(stream);
  
      puts(&quot;Start&quot;);
  
      if(dup2(fileno(stream), STDOUT_FILENO) == -1) perror(&quot;Something went wrong!&quot;);
  
      puts(&quot;Internal&quot;);
  
      fclose(stream);
  
      FILE* f = fopen(&quot;out.txt&quot;, &quot;w+&quot;);
      fputs(buffer, f);
      fclose(f);
  }
  </code></pre>
  <p>But running it gives me the error <code>bad file descriptor</code> on <code>dup2</code>, which shouldn't be the case since <code>open_memstream</code> doesn't return <code>NULL</code> which it is supposed to do on error.</p>
  <p>Is there something about the implementation of <code>open_memstream</code> that makes it nonviable to manipulate its underlying descriptor? Or am I just being dumb and using a function wrong?</p>
  <p>Cheers in advance for any help given, and if this <em>is</em> impossible to do with <code>open_memstream</code>, is there a way to handle it with <code>FILE*</code> instead of using fds directly?</p>