title: How do I suppress output while using a dynamic library?
body: <p>I actually have <em>a</em> solution to this problem, but I'm wondering if there is a slicker one.</p>
  
  <p>I have the need to load in a library to my utility using <code>dlopen</code> and then call one of the functions.</p>
  
  <p>Unfortunately, the function spews a whole bunch of information out onto STDOUT and this I do not want.</p>
  
  <p>I have a solution that is non-portable and I'm wondering if there's a better, more generic solution that I could use. </p>
  
  <p>Here's what I have (<strong>NB</strong>: This is C) :</p>
  
  <pre><code>/*
   * Structure for retaining information about a stream, sufficient to
   * recreate that stream later on
   */
  struct stream_info {
      int fd;
      fpos_t pos;
  };
  #define STDOUT_INFO 0
  #define STDERR_INFO 1
  
  struct stream_info s_info[2];
  point_stream_to_null(stdout, &amp;s_info[STDOUT_INFO]);
  point_stream_to_null(stderr, &amp;s_info[STDERR_INFO]);
  
  void *output = noisy_function();
  
  reset_stream(stderr, &amp;s_info[STDERR_INFO]);
  reset_stream(stdout, &amp;s_info[STDOUT_INFO]);
  
  /*
   * Redirects a stream to null and retains sufficient information to restore the stream to its original location
   *** NB ***
   * Not Portable
   */
  void point_stream_to_null(FILE *stream, struct stream_info *info) {
      fflush(stream);
      fgetpos(stream, &amp;(info-&gt;pos));
      info-&gt;fd = dup(fileno(stream));
      freopen("/dev/null", "w", stream);
  }
  
  /*
   * Resets a stream to its original location using the info provided
   */
  void reset_stream(FILE *stream, struct stream_info *info) {
      fflush(stream);
      dup2(info-&gt;fd, fileno(stream));
      close(info-&gt;fd);
      clearerr(stream);
      fsetpos(stream, &amp;(info-&gt;pos));
  }
  </code></pre>
  
  <p>Any suggestions?</p>