function extsearch( pat: PATTERN ): integer; var offs, i, m, nb, nr: integer; buff: TEXT; found: boolean; function fillbuff: integer; var j: integer; begin j := nb+1; while (j <= BUFSIZ-nb) and not eof(input) do begin read(buff[j]); j := j+1; end; fillbuff := j-nb-1; for i:=j to BUFSIZ do buff[i] := chr(0); end; begin found := FALSE; m := length(pat); if m = 0 then begin extsearch := 1; found := TRUE; end; if m >= BUFSIZ then begin {*** Buffer is too small ***} extsearch := -1; found := TRUE; end; {*** Assume that the file is open and positioned ***} offs := 0; {*** number of characters already read ***} nb := 0; {*** number of characters in buffer ***} while not found do begin if nb >= m then begin {*** try to match ***} i := search(pat,buff); if i <> 0 then begin extsearch := i+offs; {*** found ***} found := TRUE; end; for i:=1 to m-1 do buff[i] := buff[i+nb-m+2]; offs := offs + nb-m+1; nb := m-1; end; {*** read more text ***} if not found then begin nr := fillbuff; if nr <= 0 then begin extsearch := 0; {*** not found ***} found := TRUE; end; nb := nb + nr; end; end; end;