/* pgreplib.c Jim Garlick This code was stripped out of Ray's cryptoscan program. It's used by the master and slave programs and I thought it would be useful to strip out these functions, compile them, and then link them into each of the other programs. */ #include #include #include #include "pgrep.h" int cryptoscan( char *buf , char *s , int begin , int *offset , int *length ) { /* Scans "buf" for the characters of "s", starting at "begin", and returns the "offset" of the first character, and the "length" of the string containing the the first through last characters. The return-value for the function is the offset if found, or -1 if not found. Note that the characters in buf are possibly separated by non-alpha characters. These are ignored for the purposes of searching. Both "buf" and "s" are assumed to be null-terminated strings. "s" is assumed to contain only alpha characters. */ int i ; /* index in buf */ int j ; /* index in s */ int start ; /* temporary variable that holds the tentative start for a string in buf */ i = begin ; start = i ; j = 0 ; while ( 1 ) { /* if we're at the end of the search string, stop */ if ( s[j] == '\0' ) break ; /* ignore non-alpha chars */ while ( ! ( isalpha( buf[i] ) || buf[i] == '\0' ) ) i ++ ; /* if we're at the end of the buffer, stop */ if ( buf[i] == '\0' ) break ; #ifdef debug printf( "buf[%d] %c s[%d] %c\n" , i , buf[i] , j , s[j] ) ; #endif /* compare the current (alphabetic characters in each string) */ if ( tolower( buf[i] ) == tolower( s[j] ) ) { /* save our starting point if this is the first character */ if ( j == 0 ) start = i ; /* increment both counters and continue searching */ i ++ ; j ++ ; } else { /* restart searching at the character following our previous start */ i = start + 1 ; j = 0 ; start = i ; } } /* if j incremented to the length of the search string, then the whole string was found and we return offset and length otherwise we return not found (-1). */ if ( j == strlen( s ) ) { *offset = start ; *length = i - start ; return( start ) ; } else return( -1 ) ; } void cryptocount( char *buf , char *search_string , int *found_count ) /* repeatedly calls cryptoscan to search a buffer (buf) for a search_string, and increments found_count for each occurrence. Note that found_count is not initialized by this routine, only incremented; before calling this routine for the first time, you should initialize found_count. */ { int p ; /* current search position */ int found ; /* where found, relative to the beginning of the buffer */ int length ; /* length of found string, including non-alpha characters */ p = 0 ; while( 1 ) { p = cryptoscan( buf , search_string , p , &found , &length ) ; if( p > 0 ) { #ifdef debug printf( "found at offset %d length %d \"" , found , length ) ; for( i = found ; i < found + length ; i ++ ) putchar( buf[i] ) ; putchar( '\"' ) ; putchar( '\n' ) ; #endif (*found_count) ++ ; p = found + length ; } else break ; } } int last_n_alphas( char *buf , int len , int *start ) /* this routine scans backwards from the end of a buffer "buf" looking for "len" alpha characters, and returns as "start" the position of the first character in the sequence. If the desired number of letters isn't found, -1 is returned by the function. */ { int i ; int count ; int siz ; count = 0 ; siz = strlen( buf ) ; for( i = siz - 1 ; i >= 0 ; i -- ) { if( count >= len ) break ; if( isalpha( buf[i] ) ) count ++ ; } if( i >= 0 ) { *start = i ; return( i ) ; } else return( -1 ) ; }