      program find_string

      dimension length(21)

C_salford      integer*2 kk

      character*132 text(21),line,string,underline
      character*1   found

    5 write(6,'('' enter search string : '',$)')
      read(5,'(a)',err=5)string
      ls=kslength(string,132)
      if (ls.eq.0) go to 5

   10 write(6,'('' enter filename(s)   : '',$)')
      read(5,'(a)',err=10)line
      l=kslength(line,132)
      if (l.eq.0) go to 10

   15 write(6,'('' enter number of before & after lines : '',$)')
      read(5,*,err=15)n1,n2
      if (n1.gt.20) go to 15

   16 write(6,'('' enter output filename (if desired)   : '',$)')
      read(5,'(a)',err=16)text(1)
      k=kslength(text(1),132)

        if (k.eq.0) then
          kch=6
        else
          kch=16
          open(unit=16,file=text(1)(1:k),status='new')
        end if

      open(unit=15,file='FILES.LIS',status='old',iostat=ierr)
      if (ierr.eq.0) close(unit=15,status='delete')

C_salford      call cissue('dir/b '//line(1:l)//' > FILES.LIS',kk)

      call system('dir/b '//line(1:l)//' > FILES.LIS')

      open(unit=15,file='FILES.LIS',status='old')

      nline=0
      nfile=0
      ntotal=0
      write(kch,*)
   20 read(15,'(a)',end=999)line
      l=kslength(line,132)

      open(unit=25,file=line(1:l),status='old',iostat=ierr)

        if (ierr.ne.0) then
          write(kch,'('' error opening file : '',a)')line(1:l)
          go to 20
        end if

      nfile=nfile+1
      nfound=0
      found=' '
      last=0
      kount=0
      k=0
   25 k=k+1
      if (k.eq.22) k=1
      read(25,'(a)',end=99)text(k)

      length(k)=kslength(text(k),132)

      kount=kount+1

        if (length(k).ge.ls) then
          m=index(text(k)(1:length(k)),string(1:ls))
        else
          m=0
        end if

        if (m.gt.0) then
          nfound=nfound+1

            if (found.eq.' ') then
              found='y'
              write(kch,'(/,'' file : '',a)')line(1:l)
            end if

          if (kount.gt.last) write(kch,'(60(''=''))')

          do 30 i=kount-n1,kount-1
          if (i.lt.1.or.i.le.last) go to 30
          j=k+i-kount
          if (j.lt.1) j=j+21
          write(kch,'(i6,a)')i,text(j)(1:length(j))
   30     continue
          last=kount+n2

          write(kch,'(i6,a)')kount,text(k)(1:length(k))
          underline=' '
          do 35 i=m,ls+m-1
          underline(i:i)='*'
   35     continue
          write(kch,'(6x,a)')underline(1:ls+m-1)
        else if (last.ge.kount) then
          write(kch,'(i6,a)')kount,text(k)(1:length(k))
        end if

      go to 25

   99 close(unit=25)

      if (found.eq.'y') write(kch,'(60(''=''))')
      write(kch,'(i6,'' occurences in file : '',a)')nfound,line(1:l)

      ntotal=ntotal+nfound
      nline=nline+kount
      go to 20

  999 close(unit=15,status='delete')

      write(kch,'(/,i6,'' total occurences'')')ntotal
      write(kch,'(i6,'' files'',i8,'' lines'')')nfile,nline

      stop 'search complete'
      end

      Function kslength(STRING,L)

      CHARACTER*(*) STRING

      kslength=L
      DO 10 I=1,L
      IF (STRING(kslength:kslength).NE.' ') RETURN
      kslength=kslength-1
   10 CONTINUE

      END

