I want to implement multi pipes in c so I can do something like this, where |||
means duplicate the stdin to N pipe commands):
cat /tmp/test.log ||| wc -l ||| grep test1 ||| grep test2 | grep test3
This will return to me the number of lines in the file and the lines in the file that contain 'test1' string and the lines in the file that contain 'test2' && 'test3' string
In other words this would have the effect of these 3 regular pipelines:
cat /tmp/test.log | wc -l --> stdout
| grep test1 --> stdout
| grep test2 | grep test3 --> stdout
Has someone already implementated something like this? I didn't fin开发者_开发知识库d anything... NOTE: I know it can be done with scripting languages or with bash multiple file descriptors, but I am searching C code to do it.
Thanks!
Maybe your should start off with the tee
command and examine their code.
Because it is impossible in C to have more than one process (or thread) read the same file descriptor without draining the data read, all solutions will have to store the data read in a temporary file and then each read the temp file.
精彩评论