This commit is contained in:
Eric Andersen 1999-10-29 06:50:17 +00:00
parent c1525e84dd
commit 24d8e7d787
3 changed files with 53 additions and 45 deletions

View File

@ -126,7 +126,6 @@ extern int sed_main (int argc, char **argv)
} }
} }
fprintf(stderr, "argc=%d\n", argc);
while (argc-- > 0) { while (argc-- > 0) {
name = *argv++; name = *argv++;
@ -135,10 +134,9 @@ extern int sed_main (int argc, char **argv)
perror (name); perror (name);
continue; continue;
} }
fprintf(stderr, "filename is '%s'\n", name);
haystack = (char*)malloc( 80); haystack = (char*)malloc( BUF_SIZE);
while (fgets (haystack, sizeof (haystack), fp)) { while (fgets (haystack, BUF_SIZE-1, fp)) {
foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
if (noprintFlag==TRUE && foundOne==TRUE) if (noprintFlag==TRUE && foundOne==TRUE)

6
sed.c
View File

@ -126,7 +126,6 @@ extern int sed_main (int argc, char **argv)
} }
} }
fprintf(stderr, "argc=%d\n", argc);
while (argc-- > 0) { while (argc-- > 0) {
name = *argv++; name = *argv++;
@ -135,10 +134,9 @@ extern int sed_main (int argc, char **argv)
perror (name); perror (name);
continue; continue;
} }
fprintf(stderr, "filename is '%s'\n", name);
haystack = (char*)malloc( 80); haystack = (char*)malloc( BUF_SIZE);
while (fgets (haystack, sizeof (haystack), fp)) { while (fgets (haystack, BUF_SIZE-1, fp)) {
foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
if (noprintFlag==TRUE && foundOne==TRUE) if (noprintFlag==TRUE && foundOne==TRUE)

View File

@ -778,70 +778,82 @@ int get_console_fd(char* tty_name)
#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND ) #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND )
/* Do a case insensitive strstr() */
char* stristr(char *haystack, const char *needle)
{
int len = strlen( needle );
while( *haystack ) {
if( !strncasecmp( haystack, needle, len ) )
break;
haystack++;
}
if( !(*haystack) )
haystack = NULL;
return haystack;
}
/* This tries to find a needle in a haystack, but does so by /* This tries to find a needle in a haystack, but does so by
* only trying to match literal strings (look 'ma, no regexps!) * only trying to match literal strings (look 'ma, no regexps!)
* This is short, sweet, and carries _very_ little baggage, * This is short, sweet, and carries _very_ little baggage,
* unlike its beefier cousin a few lines down... * unlike its beefier cousin in regexp.c
* -Erik Andersen * -Erik Andersen
*/ */
extern int find_match(char *haystack, char *needle, int ignoreCase) extern int find_match(char *haystack, char *needle, int ignoreCase)
{ {
if (ignoreCase == FALSE) { if (ignoreCase == FALSE)
haystack = strstr (haystack, needle); haystack = strstr (haystack, needle);
if (haystack == NULL) else
return FALSE; haystack = stristr (haystack, needle);
return TRUE; if (haystack == NULL)
} else { return FALSE;
int i; return TRUE;
char needle1[BUF_SIZE];
char haystack1[BUF_SIZE];
strncpy( haystack1, haystack, sizeof(haystack1));
strncpy( needle1, needle, sizeof(needle1));
for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
haystack1[i]=tolower( haystack1[i]);
for( i=0; i<sizeof(needle1) && needle1[i]; i++)
needle1[i]=tolower( needle1[i]);
haystack = strstr (haystack1, needle1);
if (haystack == NULL)
return FALSE;
return TRUE;
}
} }
/* This performs substitutions after a regexp match has been found. */ /* This performs substitutions after a string match has been found. */
extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
{ {
int foundOne; int foundOne;
char *where, *slider; char *where, *slider, *slider1, *oldhayStack;
if (ignoreCase == FALSE) { if (ignoreCase == FALSE)
/*Find needle in haystack */
where = strstr (haystack, needle); where = strstr (haystack, needle);
while(where!=NULL) { else
foundOne++; where = stristr (haystack, needle);
fprintf(stderr, "A match: haystack='%s'\n", haystack);
if (strcmp(needle, newNeedle)==0)
return FALSE;
oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
while(where!=NULL) {
foundOne++;
strcpy(oldhayStack, haystack);
#if 0
if ( strlen(newNeedle) > strlen(needle)) {
haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
strlen(needle) + strlen(newNeedle))); strlen(needle) + strlen(newNeedle)));
for(slider=haystack;slider!=where;slider++);
*slider=0;
haystack=strcat(haystack, newNeedle);
slider+=1+sizeof(newNeedle);
haystack = strcat(haystack, slider);
where = strstr (where+1, needle);
} }
} else { #endif
// FIXME for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
*slider=0;
haystack=strcat(haystack, newNeedle);
slider1+=strlen(needle);
haystack = strcat(haystack, slider1);
where = strstr (slider, needle);
} }
free( oldhayStack);
if (foundOne) if (foundOne)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
} }
#endif #endif
/* END CODE */ /* END CODE */