title: C code to check if command line is redirected to /dev/null
body: <p>I'm writing a C program that outputs to <code>stdout</code> and errors to <code>stderr</code>. The program takes a command such as:</p>
  
  <pre><code>./myprogram function_to_run file_to_read
  </code></pre>
  
  <p>My program can either output to <code>stdout</code> or be directed to output a file, but it must not be redirected to <code>/dev/null</code>. For example:</p>
  
  <pre><code>./myprogram function_to_run file_to_read //OK
  ./myprogram function_to_run file_to_read &gt; file.txt //OK
  ./myprogram function_to_run file_to_read &gt; /dev/null // NOT OK, should produce error in stderr
  </code></pre>
  
  <p>I tried to use <code>isatty(1)</code>, but it only can detect if <code>stdout</code> is outputting to a terminal. Therefore, it fails for the case where <code>stdout</code> is redirected to a file, which is acceptable in my case</p>
  
  <p>Is there a way to check for this in C? If not, any suggestion how I could check for the /dev/null scenario?</p>