title: How do I write a filter program in C?
body: <p>Since UNIX has all those wonderful filter-like programs (such as <code>grep</code>, <code>sed</code>, <code>tr</code> and so forth), what's the easiest way to write one of those in standard C?</p>
  
  <p>By filter, I mean a program which reads standard input, performs some manipulation of the data, and then writes it to standard output. This is useful in constructing pipelines of commands, with each performing some additional manipulation of the data, such as:</p>
  
  <pre><code>grep xyzzy input.file | tr '[A-Z]' '[a-z]' | sed 's/plugh/PLUGH/g'
  </code></pre>
  
  <p>(each of the <code>|</code> pipe symbols connects the standard output of the previous command to the standard input of the next, hence the pipeline metaphor).</p>
  
  <p>Let's say I needed one that converted all uppercase characters to lowercase. And, yes, I realise this <em>particular</em> problem can be solved with the UNIX:</p>
  
  <pre><code>tr '[A-Z]' '[a-z]'
  </code></pre>
  
  <p>but that's just an <em>example.</em></p>
  
  <p>What I'm actually after is the simplest standard C source code to do such a filter.</p>