[comp.bugs.sys5] Another fgrep bug

rogerc@ncrcae.Columbia.NCR.COM (Roger Collins) (12/19/87)

This is SysV.2 fgrep with all the changes posted by guy@gorodish.Sun.COM 
(Guy Harris).

Execute the following:

echo asd | fgrep 'asdf
asder
asd'

The output should be "asd", but no output is produced.

The cause is that fgrep expects a newline after the last key-word.

An easy fix is to kludge the getargc() function to add a newline
at the end of a file *if* newline is not already there.

Here is the new getargc():

----------
getargc()
{
	static char lastc = 0;
	register c;
	if (wordf) {
		c = getc(wordf);
		if (c == EOF) {
			if (ferror(wordf)) {
				fprintf(stderr,"fgrep: Read error\n");
				exit(1);
			}
			fclose(wordf);
		}
	}
	else {
		if ((c = *argptr++) == '\0') {
			c = EOF;
			--argptr;
		}
	}
	if (c == EOF && lastc != '\n')
		c = '\n';

	return(lastc=c);
}
----------

This works whether the word list is input on the command line or
with the "-f" option.

Merry Christmas,

--
Roger Collins
NCR - E&M Columbia
rogerc@ncrcae.Columbia.NCR.COM
Disclaimer:  Everything here is opinion and all opinions expressed here are 
	     my own and do not reflect those of my employer or anyone else.