Perhaps you could use fork, exec, pipe, read, write, and similar system calls, to execute grep and feed data through it, without resorting to any shell or any shell wrapper such as the system(3) library call.
Or, if that would work, except for being rather fussy to code, then consider Colin Watson's libpipeline: http://libpipeline.nongnu.org -- Paul Jackson p...@usa.net