home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / assembler / tasm / GREP / C / search < prev    next >
Encoding:
Text File  |  1992-03-04  |  1.6 KB  |  78 lines

  1. /*
  2. SEARCH.C   Search a file for a text string.
  3. R.J.P.    9-5/90
  4. */
  5.  
  6. #include       <stdio.h>
  7. #include       <string.h>
  8. #include       <stdlib.h>
  9. #include       <kernel.h>
  10. #include       <swis.h>
  11. #include       "grep.h"
  12.  
  13. void SearchFile(char *CharPattern, char *dr,char *fnL,char *fnB )
  14. {/*0*/
  15.  
  16.   FILE  *fh  ;
  17.   int    ln   =  1 ;
  18.   char   *cp , *fty ;
  19.   char   ltxt[134] ;
  20.   char   utxt[134] ;
  21.   char   *TX  ;
  22. /*
  23.          Is given the path for a file.
  24.          It checks that the file is one of a type likely to hold
  25.          text, and then searches it line-by line for the first ocurrence
  26.          of the given pattern.
  27. */
  28.   char   *path ;
  29.   if( ( path = (char *)malloc( strlen(dr)+strlen(fnL) + 2) ) == 0)
  30.   {
  31.     fprintf(stderr,"**Search Path memory Failure.\n") ;
  32.     return ;
  33.   }
  34.           
  35.   if(B_AnyCase)    TX = utxt  ;
  36.   else             TX = ltxt ;
  37.   sprintf(path,"%s.%s" , dr,fnL) ;
  38.   if( (fty = ValidObject(CharPattern,path,fnB) ),fty==0 )
  39.   {    free(path) ;         return ;}
  40.  
  41.   if( B_FileOnly )
  42.   {    fprintf(stderr,"%s<%s>\n",path,fty) ;    return ;  }
  43.  
  44.   if( (fh = fopen(path,"r") ) == 0 )
  45.   {
  46.     fprintf(stderr,"** Open Failed : %s\n" , path ) ;
  47.     free(path) ;
  48.     return;
  49.   }
  50.   
  51.   for( ;; )
  52.   {
  53.     if (feof(fh))      break  ;
  54.      
  55.     ++N_Lines ;
  56.     fgets(ltxt,132,fh) ;
  57.     if( B_AnyCase)
  58.     {
  59.       strcpy(utxt,ltxt);      strupper(utxt);
  60.     }
  61.     
  62.     if ( ( cp = strstr( TX , CharPattern )) != 0 ) 
  63.     {
  64.       ++N_Hits ;
  65. #if  SHOW_ALL_LINE 
  66.       printf("%s<%s>[%4d] %s",path,fty,ln,TX) ;  
  67. #else
  68.       printf("%s<%s>[%4d] %s",path,fty,ln,cp) ;  
  69. #endif
  70.     }
  71.     ++ln ;   
  72.   }
  73.   
  74.   fclose( fh ) ;        free(path) ;
  75. }
  76.  
  77.  
  78.