aboutsummaryrefslogtreecommitdiff
path: root/sed.c
diff options
context:
space:
mode:
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 f0a6a3b48..34756e09a 100644
--- a/sed.c
+++ b/sed.c
@@ -45,9 +45,23 @@ static const char sed_usage[] =
45#else 45#else
46"This version of sed matches strings (not full regexps).\n"; 46"This version of sed matches strings (not full regexps).\n";
47#endif 47#endif
48
48 49
50static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag)
51{
52 int foundOne=FALSE;
53 char haystack[1024];
49 54
50 55 while (fgets (haystack, 1023, fp)) {
56 foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
57 if (foundOne==TRUE && printFlag==TRUE) {
58 fprintf(stdout, haystack);
59 }
60 if (quietFlag==FALSE) {
61 fprintf(stdout, haystack);
62 }
63 }
64}
51 65
52extern int sed_main (int argc, char **argv) 66extern int sed_main (int argc, char **argv)
53{ 67{
@@ -56,11 +70,9 @@ extern int sed_main (int argc, char **argv)
56 char *name; 70 char *name;
57 char *cp; 71 char *cp;
58 int ignoreCase=FALSE; 72 int ignoreCase=FALSE;
59 int foundOne=FALSE;
60 int printFlag=FALSE; 73 int printFlag=FALSE;
61 int quietFlag=FALSE; 74 int quietFlag=FALSE;
62 int stopNow; 75 int stopNow;
63 char *haystack;
64 76
65 argc--; 77 argc--;
66 argv++; 78 argv++;
@@ -73,24 +85,23 @@ extern int sed_main (int argc, char **argv)
73 cp = *argv++; 85 cp = *argv++;
74 stopNow=FALSE; 86 stopNow=FALSE;
75 87
76 while (*++cp && stopNow==FALSE) 88 while (*++cp && stopNow==FALSE) {
77 switch (*cp) { 89 switch (*cp) {
78 case 'n': 90 case 'n':
79 quietFlag = TRUE; 91 quietFlag = TRUE;
80 break; 92 break;
81 case 'e': 93 case 'e':
82 if (*(cp+1)==0 && --argc < 0) { 94 if (*(cp+1)==0 && --argc < 0) {
83 fprintf(stderr, "A\n");
84 usage( sed_usage); 95 usage( sed_usage);
85 } 96 }
86 cp = *argv++; 97 if ( *++cp != 's')
98 cp = *argv++;
87 while( *cp ) { 99 while( *cp ) {
88 if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') { 100 if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
89 char* pos=needle=cp+2; 101 char* pos=needle=cp+2;
90 for(;;) { 102 for(;;) {
91 pos = strchr(pos, '/'); 103 pos = strchr(pos, '/');
92 if (pos==NULL) { 104 if (pos==NULL) {
93 fprintf(stderr, "B\n");
94 usage( sed_usage); 105 usage( sed_usage);
95 } 106 }
96 if (*(pos-1) == '\\') { 107 if (*(pos-1) == '\\') {
@@ -104,7 +115,6 @@ extern int sed_main (int argc, char **argv)
104 for(;;) { 115 for(;;) {
105 pos = strchr(pos, '/'); 116 pos = strchr(pos, '/');
106 if (pos==NULL) { 117 if (pos==NULL) {
107 fprintf(stderr, "C\n");
108 usage( sed_usage); 118 usage( sed_usage);
109 } 119 }
110 if (*(pos-1) == '\\') { 120 if (*(pos-1) == '\\') {
@@ -116,7 +126,6 @@ extern int sed_main (int argc, char **argv)
116 *pos=0; 126 *pos=0;
117 if (pos+2 != 0) { 127 if (pos+2 != 0) {
118 while (*++pos) { 128 while (*++pos) {
119 fprintf(stderr, "pos='%s'\n", pos);
120 switch (*pos) { 129 switch (*pos) {
121 case 'i': 130 case 'i':
122 ignoreCase=TRUE; 131 ignoreCase=TRUE;
@@ -134,42 +143,35 @@ extern int sed_main (int argc, char **argv)
134 } 143 }
135 cp++; 144 cp++;
136 } 145 }
137 fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); 146 //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
138 stopNow=TRUE; 147 stopNow=TRUE;
139 break; 148 break;
140 149
141 default: 150 default:
142 fprintf(stderr, "D\n");
143 usage(sed_usage); 151 usage(sed_usage);
144 } 152 }
153 }
145 } 154 }
146 155
147 while (argc-- > 0) { 156 if (argc==0) {
148 name = *argv++; 157 do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag);
158 } else {
159 while (argc-- > 0) {
160 name = *argv++;
149 161
150 fp = fopen (name, "r"); 162 fp = fopen (name, "r");
151 if (fp == NULL) { 163 if (fp == NULL) {
152 perror (name); 164 perror (name);
153 continue; 165 continue;
154 } 166 }
155 167
156 haystack = (char*)malloc( 1024); 168 do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag);
157 while (fgets (haystack, 1023, fp)) {
158
159 foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
160 if (foundOne==TRUE && printFlag==TRUE)
161 fputs (haystack, stdout);
162 if (quietFlag==FALSE)
163 fputs (haystack, stdout);
164 /* Avoid any mem leaks */
165 free(haystack);
166 haystack = (char*)malloc( BUF_SIZE);
167 }
168 169
169 if (ferror (fp)) 170 if (ferror (fp))
170 perror (name); 171 perror (name);
171 172
172 fclose (fp); 173 fclose (fp);
174 }
173 } 175 }
174 exit( TRUE); 176 exit( TRUE);
175} 177}