grep -r for OpenSolaris
I’m working on an RFE to add a recursive option to the OpenSolaris [f]grep utilities. I had started on the project last year, but put it on the back burner for several reasons (time constraints, philosophical quandaries about the best approach, much larger scope than anticipated). After a gentle reminder email from the Sun engineer sponsoring the work, I started working on the project again last week and wanted to share my thoughts.
Main objective – Add the functionality for Solaris grep utilities (grep/fgrep, xpg4 grep) to recursively search directories.
Secondary objectives – Keep the code as modular as possible, re-use existing functions, minimize any impact on people not using the recursive functionality.
After a bit of looking around, I think I’m going to use the nftw function to perform the recursion. While I could write my own file walking routine, I’m not currently spending much time writing C code and David Korn pointed out the silliness of each different command (ls, du, find, etc) having their own specialized file walking routine. For this scenario, I think my effort is best spent getting the desired functionality in place. I’ll leave later optimization of the file walking routine as an exercise for the reader (although I think nftw will likely perform fine for the vast majority of cases).
Note: If you have the SUNWggrp package installed, you have the GNU grep command which has -r functionality available as /usr/sfw/bin/ggrep. The purpose of the RFE I am working on is to add similar functionality to /usr/bin/[f]grep and /usr/xpg4/bin/[f]grep. Due to the licensing differences between the GNU and OpenSolaris commands, I’m not borrowing any code from GNU grep.