[comp.os.minix] Bug in fgrep + fix

bob@its63b.ed.ac.uk (ERCF08 Bob Gray) (06/02/88)

There is a bug in the fgrep posted recently.
If the read in getlin() fails for any reason, the code will
go into an infinite loop. A fix is in the diffs at the end
of this posting.

The diffs also show annother slight change which removes
an unneeded test of the endline flag. It won't make fgrep
run very much faster, but every little helps.
	Bob.
------------------------------------------------------------

*** fgrep.c.old	Thu Jun  2 11:42:47 1988
--- fgrep.c	Thu Jun  2 11:46:09 1988
***************
*** 272,280 ****
  
      } /* exhausted buffer or found \n */
  
!     if (endline) break;     /* don't continue if \n found */
      status=read(infile,mybuf,2048);
!     if (status==0) break;
  
      low=mybuf;
      high= &mybuf[status];
--- 272,286 ----
  
      } /* exhausted buffer or found \n */
  
!     /* don't continue if \n found */
!     if (endline)
!     {
!       *(p-1)='\0';
!       return (p-buf-1);
!     }
! 
      status=read(infile,mybuf,2048);
!     if (status<=0) break;
  
      low=mybuf;
      high= &mybuf[status];
***************
*** 281,294 ****
  
    }
  
-   if (endline)
-   {
-     *(p-1)='\0';
-     return (p-buf-1);
-   }
- 
    /* empty line or a bit filled ? */
    *p='\0';
    if (p-buf) return(p-buf);
    return(EOF);
  
--- 287,298 ----
  
    }
  
    /* empty line or a bit filled ? */
    *p='\0';
+   if (status<0){
+     perror("read error");
+     return(EOF);
+   }
    if (p-buf) return(p-buf);
    return(EOF);