Bug in 'grep'

Andrew Arensburger arensb at cvl.umd.edu
Thu Jul 14 02:45:01 AEST 1988


	There is a bug in 'grep': if the input file does not end with a
newline, and your pattern is in the last line, 'grep' will not find it.
The routine 'execute', which reads in lines from the input file and compares
them to the pattern should be changed from


execute(file)
char *file;
{
	register char *p1, *p2;
	register c;

	[...]

	for (;;) {
		lnum++;
		p1 = linebuf;
		while ((c = getchar()) != '\n') {
			if (c == EOF) {
				[...]
				return;
			}
		[...]


to


execute(file)
char *file;
{
	register char *p1, *p2;
	register c;
	char eof;			/* 'end-of-file' flag */

	[...]

	for (eof = 0; !eof;) {		/* While not end of file */
		lnum++;
		p1 = linebuf;
		while ((c = getchar()) != '\n') {
			if (c == EOF) {
				[...]
				eof = 1;
				break;
			}
		[...]

which neatly circumvents the problem.

					/AA/



More information about the Comp.unix.wizards mailing list