title: Redirect forked process output to NULL
body: <p>I need to create a function that, among other things, spawns a child process.
  I'd like to pass to this function an optional list of file discriptors so that, depending on the user needs, the child process input/output can be redirected.</p>
  
  <p>I have seen dozens of people talking about how to do this using <code>dup2</code> somewhat like this:</p>
  
  <pre><code>if( pid &lt; 0 )   // error forking.
  {
  //...
  }
  else if( pid != 0 ) // Parent process.
  {
      ret = waitpid( pid, &amp;status, 0 );
      return  WEXITSTATUS( status );
  }
  else    // Child process.
  {
      dup2( fd, STDIN_FILENO );   // Clone passed file discriptor.
      close( fd );            // Close the passed one, since we have already cloned.
      execvp( arglist[ 0 ], arglist );
  }
  </code></pre>
  
  <p>Alright. All of this is in the Internet.
  My question now is, how (or what is the best way) to redirect to <code>/dev/null</code>?</p>
  
  <p>Should I force the user to <code>open( /dev/null)</code> and pass it as <code>fd</code> or is there any better way?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>This is not as pretty as I wanted, but I could not find any better way, so I ended up passing an array of file names to wherever a user could want to redirect, respectively, STDIN, STDOUT and STDERR:</p>
  
  <pre><code>static int  do_exec( arglist_t arglist, const char *fio[ 3 ] )
  {
  DEBUG__( OSU_DEBUG_LEVEL_1, "fio = %p\n", fio );
  
      if ( fio )
      {
          if ( fio[ STDIN_FILENO ] )
          {
              int fd = open( fio[ STDIN_FILENO ], O_RDONLY );
  
              if ( -1 &lt; fd )
              {
                  dup2( fd, STDIN_FILENO );
                  close( fd );
              }
          }
  
          if ( fio[ STDOUT_FILENO ] )
          {
              int fd = open( fio[ STDOUT_FILENO ], O_WRONLY | O_CREAT | O_APPEND );
  
              if ( -1 &lt; fd )
              {
                  dup2( fd, STDOUT_FILENO );
                  close( fd );
              }
          }
  
          if ( fio[ STDERR_FILENO ] )
          {
              int fd = open( fio[ STDERR_FILENO ], O_WRONLY | O_CREAT | O_APPEND );
  
              if ( -1 &lt; fd )
              {
                  dup2( fd, STDERR_FILENO );
                  close( fd );
              }
          }
      }
  
      return  execvp( arglist[ 0 ], arglist );
  }
  </code></pre>
  
  <ul>
  <li>I haven't yet fully tested it, so it may have some bugs.</li>
  </ul>
  
  <p>Really thanks to @Zack and @gbulmer.</p>