home *** CD-ROM | disk | FTP | other *** search
- /*
- SEARCH.C Search a file for a text string.
- R.J.P. 9-5/90
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <kernel.h>
- #include <swis.h>
- #include "grep.h"
-
- void SearchFile(char *CharPattern, char *dr,char *fnL,char *fnB )
- {/*0*/
-
- FILE *fh ;
- int ln = 1 ;
- char *cp , *fty ;
- char ltxt[134] ;
- char utxt[134] ;
- char *TX ;
- /*
- Is given the path for a file.
- It checks that the file is one of a type likely to hold
- text, and then searches it line-by line for the first ocurrence
- of the given pattern.
- */
- char *path ;
- if( ( path = (char *)malloc( strlen(dr)+strlen(fnL) + 2) ) == 0)
- {
- fprintf(stderr,"**Search Path memory Failure.\n") ;
- return ;
- }
-
- if(B_AnyCase) TX = utxt ;
- else TX = ltxt ;
- sprintf(path,"%s.%s" , dr,fnL) ;
- if( (fty = ValidObject(CharPattern,path,fnB) ),fty==0 )
- { free(path) ; return ;}
-
- if( B_FileOnly )
- { fprintf(stderr,"%s<%s>\n",path,fty) ; return ; }
-
- if( (fh = fopen(path,"r") ) == 0 )
- {
- fprintf(stderr,"** Open Failed : %s\n" , path ) ;
- free(path) ;
- return;
- }
-
- for( ;; )
- {
- if (feof(fh)) break ;
-
- ++N_Lines ;
- fgets(ltxt,132,fh) ;
- if( B_AnyCase)
- {
- strcpy(utxt,ltxt); strupper(utxt);
- }
-
- if ( ( cp = strstr( TX , CharPattern )) != 0 )
- {
- ++N_Hits ;
- #if SHOW_ALL_LINE
- printf("%s<%s>[%4d] %s",path,fty,ln,TX) ;
- #else
- printf("%s<%s>[%4d] %s",path,fty,ln,cp) ;
- #endif
- }
- ++ln ;
- }
-
- fclose( fh ) ; free(path) ;
- }
-
-
-