Some patches from Gennady Feldman. Fixed a glob problem such that
[oweals/busybox.git] / sed.c
diff --git a/sed.c b/sed.c
index a18f6e5222033abe76c542d23b9b3db7e28f4db7..1342a66433554a7bf9dce8fb02cfaee22ac2427f 100644 (file)
--- a/sed.c
+++ b/sed.c
@@ -317,7 +317,7 @@ static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
 
        /* now we need to go through * and: s/\\[\r\n]$/\n/g on the edit line */
        while (ptr[idx]) {
-               while (ptr[idx] != '\\' && (ptr[idx+1] != '\n' || ptr[idx+1] != '\r')) {
+               while (ptr[idx] != '\\' || (ptr[idx+1] != '\n' && ptr[idx+1] != '\r')) {
                        idx++;
                        if (!ptr[idx]) {
                                goto out;
@@ -333,18 +333,20 @@ static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
        }
 
 out:
-       ptr[idx] = '\n';
-       ptr[idx+1] = 0;
-
        /* this accounts for discrepancies between the modified string and the
         * original string passed in to this function */
        idx += slashes_eaten;
 
-       /* this accounts for the fact that A) we started at index 3, not at index
-        * 0  and B) that we added an extra '\n' at the end (if you think the next
-        * line should read 'idx += 4' remember, arrays are zero-based) */
+       /* figure out if we need to add a newline */
+       if (ptr[idx-1] != '\n') {
+               ptr[idx] = '\n';
+               idx++;
+       }
 
-       idx += 3;
+       /* terminate string */
+       ptr[idx]= 0;
+       /* adjust for opening 2 chars [aic]\ */
+       idx += 2;
 
        return idx;
 }
@@ -622,9 +624,10 @@ static void process_file(FILE *file)
                        if (sed_cmds[i].beg_match && sed_cmds[i].end_match) {
                                if (still_in_range || regexec(sed_cmds[i].beg_match, line, 0, NULL, 0) == 0) {
                                        line_altered += do_sed_command(&sed_cmds[i], line);
-                                       still_in_range = 1; 
-                                       if (regexec(sed_cmds[i].end_match, line, 0, NULL, 0) == 0)
+                                       if (still_in_range && regexec(sed_cmds[i].end_match, line, 0, NULL, 0) == 0)
                                                still_in_range = 0;
+                                       else
+                                               still_in_range = 1;
                                }
                        }