Here is a slapped together version of it. It has no error checking and probably has overflow bugs. But I think it finds the desired string and accounts for the backtracking necessary for partial substring matches. I doubt there are more than 15 bugs left.
Edit: There was at least one in the first answer. I woke up in the middle of the night and realized the backtracking check was wrong. It didn't find '12123' in '1212123'. It might still be wrong, but at least it finds that one now.
int main( int argc, char* argv[] )
{
FILE *fp;
char *find, *hist;
int len, pos=0, hl=0, i;
char c;
fp = fopen( argv[1], "r" );
find = argv[2];
len = (int)strlen( find );
hist = malloc( len );
memset( hist, 0, len );
while ( !feof( fp )) {
c = fgetc( fp );
if ( find[pos++] == c ) {
if ( pos == len ) {
printf( "Found it\n" );
return 1;
}
}
else {
// check history buffer (kludge for backtracking)
if ( pos > 0 ) {
pos = 0;
for ( i = 0; i < len - 1; i++ )
if ( 0 == memcmp( hist+len-i-1, find, i + 1 )) {
// we had a mismatch, but the history matches up to len i
pos = i;
}
}
}
// update history buffer - this is innefficient - better as circular buffer
memmove( hist, hist + 1, len - 1 );
hist[len-1] = c;
}
printf( "Not found\n" );
}