title: How to capture unbuffered output from stdout without modifying the program?
body: <p>I'm writing a utility for running programs, and I need to capture unbuffered stdout and stderr from the programs. I need to:</p>
  
  <ul>
  <li>Capture stdout and stderr to separate files.</li>
  <li>Output needs to not be buffered (or be line buffered).</li>
  <li>Without modifying the source of the program being run.</li>
  </ul>
  
  <p>The problem is, when piping output to a file, the stdout stream becomes block buffered rather than line buffered. If the program crashes, the output never gets flushed, and is blank. So I need to capture stdout without buffering (or with line buffering).</p>
  
  <p>I think this can be done with pty's but I'm having difficulty finding any examples that do exactly what I want (most ignore stderr). In fact, I'm not sure I've found any pty examples in C at all; most use a higher-level interface like Python's pty and subprocess modules.</p>
  
  <p>Can anyone help (with code snippets or links)? Any help would be appreciated.</p>
  
  <p>EDIT: I think I've solved it. The following two links were pretty helpful.</p>
  
  <ul>
  <li><a href="http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.bpxbd00/posixopenpt.htm" rel="nofollow noreferrer">http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.bpxbd00/posixopenpt.htm</a></li>
  <li><a href="http://www.gidforums.com/t-3369.html" rel="nofollow noreferrer">http://www.gidforums.com/t-3369.html</a></li>
  </ul>
  
  <p>My code is available as a repository:</p>
  
  <ul>
  <li><a href="https://bitbucket.org/elliottslaughter/pty" rel="nofollow noreferrer">https://bitbucket.org/elliottslaughter/pty</a></li>
  </ul>