title: Can I get the access mode of a `FILE*`?
body: <p>I have to duplicate a <code>FILE*</code> in C on Mac OS X (using POSIX <code>int</code> file descriptors all the way is unfortunately out of question), so I came up with the following function:</p>
  
  <pre><code>static FILE* fdup(FILE* fp, const char* mode)
  {
      int fd = fileno(fp);
      int duplicated = dup(fd);
      return fdopen(duplicated, mode);
  }
  </code></pre>
  
  <p>It works very well, except it has that small ugly part where I ask for the file mode again, because <code>fdopen</code> apparently can't determine it itself.</p>
  
  <p>This issue isn't critical, since basically, I'm just using it for <code>stdin</code>, <code>stdout</code> and <code>stderr</code> (and obviously I know the access modes of those three). However, it <em>would</em> be more elegant if I didn't have to know it myself; and this is probably possible since the <code>dup</code> call doesn't need it.</p>
  
  <p>How can I determine the access mode of a <code>FILE*</code> stream?</p>