egrep bug

trb at floyd.UUCP trb at floyd.UUCP
Sat May 28 05:36:15 AEST 1983


In 4.1bsd, when egrep comes upon a file (to be searched) which it can't
open it immediately exits (2) even though there are more files to be
searched.  This is inconsistent with the behavior of grep and fgrep and
it's also inconvenient.  If I'm searching through *.c and one of the
files happens to be unreadable, I don't want egrep to stop.  The fix
for that is easy, just change the exit(2) in the error code after the
open (in function execute()) to a return.  Problem is, *grep exits with
one of the following codes:

	0 - found a match
	1 - found no match
	2 - syntax errors or inaccessible files.

Notice that it's quite possible to find a match and also have
inaccessible files.  I suppose you can return a 2 in that case.
I don't know whose job it is to care for the grep family, but I don't
know whether they want to put a flag in egrep or what, because they
previously considered inaccessible files fatal.  I think that's
ridiculous, but I don't want to break someone's shell script.

Right now, egrep exits all over the place with hard coded values,
never based upon a previous state, except for the "found a match"
flag, which looks like this

	exit(nsucc == 0);

which I consider pretty baroque.  

I hacked my egrep so that if it can't open a file it sets a new
flag cantopen=2; and returns instead of exiting.  Then, right before the
aforementioned exit(nsucc == 0); (at the end of main())
	if(cantopen == 2) exit(2);

This seems right to me, but it will change the behavior of egrep in a
semi-documented way.

	Andy Tannenbaum   Bell Labs  Whippany, NJ   (201) 386-6491



More information about the Comp.unix.wizards mailing list