#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "awake.h"
#define MEM_STRING_LENGTH 3000000
int replace_string( char *oldstring, char *xbuf, char *buf2, char *buf3 )
{
int i;
char *p;
char *q;
int sl2, sl3;
int ret;
sl2 = strlen(buf2);
sl3 = strlen(buf3);
q = oldstring;
ret = 0;
while ( ( p = strstr( q, buf2 ) ) )
{
ret++;
i = p - oldstring;
strncpy( xbuf, oldstring, i+1 );
xbuf[i]=0;
strcat(xbuf,buf3);
strcat(xbuf,&oldstring[i+strlen(buf2)]);
strcpy(oldstring, xbuf );
q = oldstring + i + 1;
}
return ret;
}
#if 0
char *replace_string( char *buf, char *xbuf )
{
int i;
int depth;
char *arg;
if ( ( arg = strstr( buf, "->pcdata->class" ) ) == NULL )
return NULL;
while ( *arg == '-'
|| *arg == '>' )
arg--;
while ( isalpha( *arg ) )
arg--;
arg++;
strcpy( xbuf, buf );
i = strlen( buf ) - strlen( arg );
xbuf[i] = 0;
strcat( xbuf, "GET_LEVEL( " );
i = strlen( xbuf );
while ( isalpha( *arg ) )
{
xbuf[i] = *arg;
i++;
arg++;
}
xbuf[i] = 0;
strcat( xbuf, ", " );
i = strlen( xbuf );
while( *arg != '[' )
arg++;
arg++;
depth = 0;
while( *arg )
{
if ( *arg == '[' )
depth++;
if ( *arg == ']' )
{
if ( depth == 0 )
break;
depth--;
}
xbuf[i] = *arg;
i++;
arg++;
}
arg++;
xbuf[i] = 0;
strcat( xbuf, " )" );
strcat( xbuf, arg );
strcpy( buf, xbuf );
return buf;
}
#endif
int main( int argc, char **argv )
{
FILE *in;
char buf[MEM_STRING_LENGTH];
char xbuf[MEM_STRING_LENGTH+50000];
int i;
int match;
int len;
char ostr[5000];
char nstr[5000];
char cstr[5000];
char chancestring[30];
srand48(time(NULL));
if ( argc <= 1 )
{
fprintf( stderr, "Syntax: %s file1 file2 ...\n", argv[0] );
exit ( 1 );
}
printf("String to replace:\n");
strcpy(ostr,"");
while( fgets(buf, 1000, stdin) != NULL )
{
if (!strcmp(buf,"-EOF-\n"))
break;
strcat(ostr, buf);
}
if (!*ostr)
{
fprintf( stderr, "No string to replace.\n" );
exit ( 1 );
}
if ( ostr[strlen(ostr)-1] == '\n' )
ostr[strlen(ostr)-1] = 0;
printf("String to replace with:\n");
strcpy(nstr,"");
while( fgets(buf, 1000, stdin) != NULL )
{
if (!strcmp(buf,"-EOF-\n"))
break;
strcat(nstr, buf);
}
if (!*nstr)
{
fprintf( stderr, "No replacement string.\n" );
exit ( 1 );
}
if ( nstr[strlen(nstr)-1] == '\n' )
nstr[strlen(nstr)-1] = 0;
strcpy( cstr, nstr );
for ( i = 1; i < argc; i++ )
{
strcpy( nstr, cstr );
sprintf( chancestring, "%ld", lrand48()%10000);
replace_string( nstr, xbuf, "RANDNUM", chancestring );
printf( "Checking %-10s", argv[i] );
fflush( stdout );
if ( ( in = fopen( argv[i], "r" ) ) == NULL )
{
fprintf( stderr, "Could not open file: %s\n", argv[i] );
continue;
}
len = fread( buf, sizeof( char ), MEM_STRING_LENGTH, in );
if ( len <= 0 )
{
fprintf( stderr, "Length of %d for %s.\n", len, argv[i] );
len = 0;
}
if ( len == MEM_STRING_LENGTH )
{
fprintf( stderr, "Length of %d for %s.\n", len, argv[i] );
fprintf( stderr, "SKIPPING.\n" );
continue;
}
buf[len] = 0;
fclose( in );
match = 0;
match = replace_string( buf, xbuf, ostr, nstr );
if ( match <= 0 )
{
printf( " - No matches. Skipping.\n" );
continue;
}
printf( " - %d matches.\n", match );
in = fopen( argv[i], "w" );
fprintf( in, "%s", buf );
fclose( in );
}
return 0;
}