/*  find1f_c    Feb 23   8:45 am  
*  from Tim Swensons Hacker's Journal Feb 1991 page 1-4
*  trying another approach seems to work ungets()?
*/

#include <stdio_h>
#define LOOKLEN 21
#define BUFSIZE LOOKLEN   /*  * see K&R page 79 Ex. 4-7  *  */
char buf[BUFSIZE], *s;  
int fd, b, bufp, len;
main()

{
char c, l, search[LOOKLEN], lookfor[LOOKLEN], initok[LOOKLEN], file[42];
int  file_count, str_len, i, pat_find, fd2, ; 
   /*  abcdefghijklmnopqrstuvwxyz  0123456789 xyxyz test string  */ 
   /*  aabbccaaabbbccc a ab abc aababc vividvivid    more_tests  */
   /*  ABC Abc ABc aBC abC  aaaaaaaaaxYzaxyZaaaaaaaaaaABC   */
file_count = pat_find = bufp = 0;   /*  Initialize to Zero  */
strset(search,'\0');
strset(lookfor,'\0');
strset(initok,'\0');
strset(file,'\0');
strset(buf,'\0');
printf("Enter name of file to be searched : ");
gets(file);
printf("%s\n",file);
printf("Enter string (%d characters or less) to be matched\n",LOOKLEN-1);
printf("      Search will be case insensitive \n");
gets(search);
if (strlen(search)>=LOOKLEN)      
   {
   strncpy(initok,search,LOOKLEN-1);
   strncpy(search,initok,LOOKLEN-1);
   strset(initok,'\0');
   }
fd = fopen(file,"r");
if (fd == NULL)
   {
   printf("Did not open file\n");
   printf("file in use?          example:  ram3_find1e_c\n"); 
   printf("Any key to exit\n");
   getchar(c);
   exit(1);
   }
fd2 = fopen("ram3_find1f_log","w+");   
if (fd2 == NULL)
   {
   printf("Did not open ram3_find1f_log file\n"); 
   printf("Any key to exit\n");
   getchar(c);
   exit(1);
   }
else
   printf("file :  ram3_find1f_log is open for w+ \n");   
str_len = strlen(search);
fputs(search,fd2);
strncpy(lookfor,search,str_len);
l = lookfor[0];
printf("Searching file: %s\n",file);
fprintf(fd2,"\nSearching file: %s\n",file);
printf("        looking for %s \n",lookfor);
fprintf(fd2,"looking for %s \n",lookfor);
while(( c = getch()) != EOF)
   {
   ++file_count;
   if((lexorder(c,l)) == 0)
      {
      strset(initok,'\0');
      initok[0] = c;
      for(i=1; i<str_len; i++)
         {
         initok[i]=getch();
         }
      ungets(initok);
      getch();   
      if(lexcmp(lookfor,initok) == 0)
         {
 pat_find++;
         printf("%s <==> %s  @ %u  # %u\n",lookfor,initok,file_count,pat_find);
 fprintf(fd2,"%s <==> %s @ %u # %u\n",lookfor,initok,file_count,pat_find);
 }
      }
   }
   printf("\nFound %d patterns that match %s\n",pat_find,search);
   fprintf(fd2,"\nFound %d patterns that match %s\n",pat_find,search);
   fclose(fd2);
   fclose(fd);
   printf("A log of this session is in ram3_find1f_log\nAny key to exit\n");
   getchar();
}  

/* - - - FUNCTION CALLS - -  getch, ungetch, ungets, strset - - - */

getch()  /*  * get a pushed back character   *  */
{
if(bufp > 0)
{
b = buf[--bufp];
return b;
}
else
{
b = getc(fd);
return b;
}
}

ungetch(b) /*  * push character back on input *  */
int b;
{
if(bufp >= BUFSIZE)
   {
   printf("ungetch: too many characters\n");
   }
else
   {
   buf[bufp++] = b;
   }
}
            
ungets(s)
char *s;
{
len = strlen(s);
while( (len > 0) )
   ungetch(s[--len]);
}   

strset(string,chr)   /*  * fill string with a character  *  */
char *chr, *string;
{
int i, length;
length = strlen(string);
for(i=0; i<length; i++)
   {
   *string++ = *chr;
   }
return *string;
}

/*  *    test string abAbCaababcaa
  XYz   vivid vivid       aabababcabc
aaaaaa ababcaaabcaaaabcabbcabcabbabcABC
*      11 am Feb 23, 1991     */
