#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MB 400000
#define MP 256
#define NCOPY 3
char *txtbuf;
int bmhInit(char *txt, int m, char *pat);
int bmhFind(int n, char *txt, int m, char *pat);
main(int argc, char **argv)
{
char patbuf[MP];
FILE *in;
int i, m, n, t0, t1;
int nmatch = 0;
char *txtp, *patp = patbuf+1;
if (argc < 3) {
fprintf(stderr,"usage: %s pattern file\n",argv[0]);
exit(0);
}
if ((in = fopen(argv[2], "r")) == NULL) {
fprintf(stderr,"cannot open input file: %s\n",argv[2]);
exit(0);
}
strcpy(patp, argv[1]);
m = strlen(patp);
txtbuf = malloc(NCOPY*MB);
txtp = txtbuf + 1;
n = 0;
for (i=0; i<NCOPY; i++) {
n += fread(txtp+n, sizeof(char), MB, in);
rewind (in);
}
close(in);
bmhInit(txtbuf, m, patbuf);
t0 = clock();
nmatch = bmhFind(n, txtbuf, m, patbuf);
t1 = clock() - t0;
printf("%d matches took %.3f\n", nmatch, (float)t1/CLOCKS_PER_SEC);
}
/* End of File */