summaryrefslogtreecommitdiff
path: root/sed.c
diff options
context:
space:
mode:
authorEric Andersen1999-11-09 01:47:36 +0000
committerEric Andersen1999-11-09 01:47:36 +0000
commit50d6360771be509737bb55b2cc5bc5e25f2a4fea (patch)
tree81d4cfe9ec9b5281924f678c28f61542616a3db7 /sed.c
parentfbb39c83b69d6c4de943c0b7374000339635d13d (diff)
downloadbusybox-50d6360771be509737bb55b2cc5bc5e25f2a4fea.zip
busybox-50d6360771be509737bb55b2cc5bc5e25f2a4fea.tar.gz
Stuff
Diffstat (limited to 'sed.c')
-rw-r--r--sed.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/sed.c b/sed.c
index f0a6a3b..34756e0 100644
--- a/sed.c
+++ b/sed.c
@@ -45,9 +45,23 @@ static const char sed_usage[] =
#else
"This version of sed matches strings (not full regexps).\n";
#endif
+
+static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag)
+{
+ int foundOne=FALSE;
+ char haystack[1024];
-
+ while (fgets (haystack, 1023, fp)) {
+ foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
+ if (foundOne==TRUE && printFlag==TRUE) {
+ fprintf(stdout, haystack);
+ }
+ if (quietFlag==FALSE) {
+ fprintf(stdout, haystack);
+ }
+ }
+}
extern int sed_main (int argc, char **argv)
{
@@ -56,11 +70,9 @@ extern int sed_main (int argc, char **argv)
char *name;
char *cp;
int ignoreCase=FALSE;
- int foundOne=FALSE;
int printFlag=FALSE;
int quietFlag=FALSE;
int stopNow;
- char *haystack;
argc--;
argv++;
@@ -73,24 +85,23 @@ extern int sed_main (int argc, char **argv)
cp = *argv++;
stopNow=FALSE;
- while (*++cp && stopNow==FALSE)
+ while (*++cp && stopNow==FALSE) {
switch (*cp) {
case 'n':
quietFlag = TRUE;
break;
case 'e':
if (*(cp+1)==0 && --argc < 0) {
- fprintf(stderr, "A\n");
usage( sed_usage);
}
- cp = *argv++;
+ if ( *++cp != 's')
+ cp = *argv++;
while( *cp ) {
if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
char* pos=needle=cp+2;
for(;;) {
pos = strchr(pos, '/');
if (pos==NULL) {
- fprintf(stderr, "B\n");
usage( sed_usage);
}
if (*(pos-1) == '\\') {
@@ -104,7 +115,6 @@ extern int sed_main (int argc, char **argv)
for(;;) {
pos = strchr(pos, '/');
if (pos==NULL) {
- fprintf(stderr, "C\n");
usage( sed_usage);
}
if (*(pos-1) == '\\') {
@@ -116,7 +126,6 @@ extern int sed_main (int argc, char **argv)
*pos=0;
if (pos+2 != 0) {
while (*++pos) {
- fprintf(stderr, "pos='%s'\n", pos);
switch (*pos) {
case 'i':
ignoreCase=TRUE;
@@ -134,42 +143,35 @@ extern int sed_main (int argc, char **argv)
}
cp++;
}
- fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
+ //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
stopNow=TRUE;
break;
default:
- fprintf(stderr, "D\n");
usage(sed_usage);
}
+ }
}
- while (argc-- > 0) {
- name = *argv++;
+ if (argc==0) {
+ do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag);
+ } else {
+ while (argc-- > 0) {
+ name = *argv++;
- fp = fopen (name, "r");
- if (fp == NULL) {
- perror (name);
- continue;
- }
+ fp = fopen (name, "r");
+ if (fp == NULL) {
+ perror (name);
+ continue;
+ }
- haystack = (char*)malloc( 1024);
- while (fgets (haystack, 1023, fp)) {
-
- foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
- if (foundOne==TRUE && printFlag==TRUE)
- fputs (haystack, stdout);
- if (quietFlag==FALSE)
- fputs (haystack, stdout);
- /* Avoid any mem leaks */
- free(haystack);
- haystack = (char*)malloc( BUF_SIZE);
- }
+ do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag);
- if (ferror (fp))
- perror (name);
+ if (ferror (fp))
+ perror (name);
- fclose (fp);
+ fclose (fp);
+ }
}
exit( TRUE);
}