Bfind

                The new windows xp home changed the search command.  On previous systems it was possible to search for all files of type *.c or *.for containing “string1”.  It is surprising how dependent I can become on such a simple command.  The new search searches all of the zips for the file name, but looks inside only those files of type *.doc, *.htm*, and *.txt.  Every time something new and wonderful comes along, something old and dear goes away.  I wrote my own search routine bfind. 

                Bfind is included in watfor; it is used from the command line as

>bfind “string” *.for > bfind.lst

A single word string does not need to be in quotes.  The > bfind.lst is the standard pipe direction that tells the computer to put screen output into the file bfind.lst rather than on the screen.  The two parameters “string” and *.for need to be passed to the code.   Bfind searches all directories under the one from which it is called.

The annotated bfind.c code

                The lines of interest are marked ß ß.  These are the arguments passed from the command line.

 

cpp\test.wpj  is set up to make test.exe which is a test copy of bfind.exe.  All of the relevant files are in cpp\BFIND.ZIP for downloading.

 

cpp\bfind.c

                This is a natural part of C.  Each Fortran compiler has its own method.  For that used by watfor see Wsystem.htm.

#include <stdio.h>  standard C input/output

#include <string.h>  standard C commands for handling strings

#include <stdlib.h>

int main(int argc,char *argv[])   note that the main code has arguments argc and argv[]

{  char strt[127],cmdline[128],fname2[128],fname3[128];

   char *ptr,*searchstr,*dirtext,*fname,*fgett;

   long int i;

   FILE *fp,*fpdir,*fpout;               these are used to store information needed for opening files

   strcpy(cmdline,"dir /B /O:N /S ");   

// in linux the above should be "find "

   if(argc < 3)   standard c method for giving directions

     {printf(" bfind arg1 arg2 searches all lower directiories \n");

      printf(" for the first occurence of arg1 in files of type \n");

      printf("   arg2, i.e. arg2 = *.for \n");

      printf(" the directory of arg2 is left as dir.lst \n");

      printf(" the output is left in seout.lst \n");

      return 0;}

   for (i=0;i<argc;i++)    standard do loop in c

     {printf(" args %s \n",argv[i]); 

       if(i == 1)

         {searchstr=argv[1];  ß ßThis is the first argument to the code. ß ß

// stlrw changes value to lower case

         strlwr(searchstr);}     C function contained in sring.h for converting the string to lower case

       if(i == 2)

         {dirtext=argv[2];   ß ß  This is the second argument to the code ß ß

         strncat(cmdline,dirtext,85);  These two lines produce the system command line.

         strncat(cmdline," > dir.lst ",20);  note that this command line produces a file dir.lst

// in linux " -print > dir.lst "        

/* in linux, cmdline shoule be "find -name "*.for" -print > dir.lst" */

         printf(" the cmd line is %s \n",cmdline);

         printf("About to spawn command.com and run a DOS command\n");

         printf(" command line \n %s \n",cmdline);

         system(cmdline);}    the system call

       }

   fpdir = fopen("dir.lst","r");  opening the file dir.lst for reading.

   if(fpdir == NULL)

     {printf(" cannot open file dir.lst \n");

     return -1;}

   fpout = fopen("seout.lst","w");  opening the temporary file seout.lst for writing.

   if(fpout == NULL)

     {printf(" cannot open file seout.lst \n");

     return -1;}

   Again0:

     fname=fgets(fname2, 127, fpdir);  reading

     for (i=1;i<127;i++)

       {if(fname2[i] == 10)

         {fname2[i]=0;

          break;}

       if(i == 127)printf(" no line feed ");}

     printf(" fname2= %s \n",fname2);

     if(fname == NULL)return 0;

     fp = fopen(fname2,"r");

     if(fp == NULL)

       {printf(" cannot open file %s\n",fname2);

       return -1;}

     i=0;

     Again:

       fgett=fgets(strt, 127, fp);

       strlwr(strt);

       i++;

       ptr = strstr(strt, searchstr);

       if(ptr != NULL){

         printf(" file name %s\n",fname);

         fprintf(fpout," file name %s\n",fname);

         printf("[%d]%s\n",i, strt);

         fprintf(fpout,"[%d]%s\n",i, strt);

         fclose(fp);

         goto Again0;}

       if(fgett != NULL)goto Again;

     fclose(fp);

     goto Again0;}  note that I use a goto