title: Capturing exit status code of child process
body: <p>I have a function that forks a process, duplicates file descriptors for input and output buffers, and then runs <code>execl</code> on a command passed in via a string called <code>cmd</code>:</p>
  
  <pre><code>static pid_t
  c2b_popen4(const char* cmd, int pin[2], int pout[2], int perr[2], int flags)
  {
      pid_t ret = fork();
  
      if (ret &lt; 0) {
          fprintf(stderr, "fork() failed!\n");
          return ret;
      }
      else if (ret == 0) {
          /*                                                                                                                                                                                                                                                                                                                  
             Assign file descriptors to child pipes (not shown)...                                                                                                                                                                                                                                                                                               
          */
          execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
          fprintf(stderr, "execl() failed!\n");
          exit(EXIT_FAILURE);
      }
      else {
          /*                                                                                                                                                                                                                                                                                                                  
             Close parent read and write pipes (not shown)...                                                                                                                                                                                                                                                                                              
          */
          return ret;
      }
      return ret;
  }
  </code></pre>
  
  <p>Each of the <code>cmd</code> instances process my data correctly, so long as my test inputs are correct. </p>
  
  <p>When bad data is passed to a child process, my parent program will run to completion and exit with a non-error status code of 0.</p>
  
  <p>If I deliberately put in bad input — to purposefully try to get one of the <code>cmd</code> instances to fail in an expected way — I'd like to know how to capture the exit status of that <code>cmd</code> so that I can issue the correct error status code from the parent program, before termination. </p>
  
  <p>How is this generally done?</p>