
/* sedb.c */
/* Andrew Davison (ad@ratree.psu.ac.th) */
/* February 11th, 1997 */

#include <string.h>
#include "match.h"

search_matches * search_db_1_svc(search_info *si, struct svc_req *rqstp)
/* Search the database file specified in si->fnm. 
   Return the total number of lines which contain the string in
   si->match_str and upto MAXMATCH of those lines.
   rgstp is not used in this function.
*/
{
  static search_matches sm;          /* static output argument */
  char lines[MAXMATCH][MAXLEN];
  char buf[MAXLEN];
  int i, matchnum;
  FILE *fp;

  sm.count = 0;
  sm.lines.lines_len = 0;
  sm.lines.lines_val = NULL;

  if ((fp = fopen(si->fnm, "r")) != NULL) {   /* No error reporting here */
    while (fgets(buf, MAXLEN, fp) != NULL)
      if (strstr(buf, si->match_str) != NULL) {
        if (sm.count < MAXMATCH)
          strcpy(lines[sm.count], buf);
        sm.count++;
      }
    fclose(fp);
    
    matchnum = (sm.count < MAXMATCH) ? sm.count : MAXMATCH;
    sm.lines.lines_len = matchnum;
    sm.lines.lines_val = (line *)malloc(matchnum * sizeof(line));
    for (i=0; i < matchnum; i++)
      sm.lines.lines_val[i] = (line) strdup(lines[i]);
  }
  return &sm;
}


