Since UNIX has all those wonderful filter-like programs (such as grep, sed, tr and so forth), what’s the easiest way to write one of those in standard C?
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:
grep xyzzy input.file | tr '[A-Z]' '[a-z]' | sed 's/plugh/PLUGH/g'
(each of the | pipe symbols connects the standard output of the previous command to the standard input of the next, hence the pipeline metaphor).
Let’s say I needed one that converted all uppercase characters to lowercase. And, yes, I realise this particular problem can be solved with the UNIX:
tr '[A-Z]' '[a-z]'
but that’s just an example.
What I’m actually after is the simplest standard C source code to do such a filter.
You could use
getlineas described by @hroptatyr, but you can do something a lot simpler: