Small size optimization from Aaron Lehmann
[oweals/busybox.git] / sed.c
diff --git a/sed.c b/sed.c
index 73ed058e237fa1a2486bad4d194e74b87d7257f5..a18cfc7c306e0d444c61ddbc5714c444469a9ee7 100644 (file)
--- a/sed.c
+++ b/sed.c
@@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r
        return idx;
 }
 
-static char *strdup_substr(const char *str, int start, int end)
-{
-       int size = end - start + 1;
-       char *newstr = xmalloc(size);
-       memcpy(newstr, str+start, size-1);
-       newstr[size-1] = '\0';
-       return newstr;
-}
-
 static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
 {
        int oldidx, cflags = REG_NEWLINE;
@@ -232,7 +223,7 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
        idx = index_of_next_unescaped_regexp_delim(sed_cmd, substr, ++idx);
        if (idx == -1)
                error_msg_and_die("bad format in substitution expression");
-       match = strdup_substr(substr, oldidx, idx);
+       match = xstrndup(substr + oldidx, idx - oldidx);
 
        /* determine the number of back references in the match string */
        /* Note: we compute this here rather than in the do_subst_command()
@@ -251,7 +242,7 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
        idx = index_of_next_unescaped_regexp_delim(sed_cmd, substr, ++idx);
        if (idx == -1)
                error_msg_and_die("bad format in substitution expression");
-       sed_cmd->replace = strdup_substr(substr, oldidx, idx);
+       sed_cmd->replace = xstrndup(substr + oldidx, idx - oldidx);
 
        /* process the flags */
        while (substr[++idx]) {
@@ -608,6 +599,10 @@ static void process_file(FILE *file)
                         * entry point into sedding...
                         */
                        if (
+                                       /* no range necessary */
+                                       (sed_cmds[i].beg_line == 0 && sed_cmds[i].end_line == 0 &&
+                                        sed_cmds[i].beg_match == NULL &&
+                                        sed_cmds[i].end_match == NULL) ||
                                        /* this line number is the first address we're looking for */
                                        (sed_cmds[i].beg_line && (sed_cmds[i].beg_line == linenum)) ||
                                        /* this line matches our first address regex */
@@ -655,12 +650,12 @@ static void process_file(FILE *file)
 
                                                /* we print the line once, unless we were told to be quiet */
                                                if (!be_quiet)
-                                                       altered = do_subst_command(&sed_cmds[i], line);
+                                                       altered |= do_subst_command(&sed_cmds[i], line);
 
                                                /* we also print the line if we were given the 'p' flag
                                                 * (this is quite possibly the second printing) */
                                                if (sed_cmds[i].sub_p)
-                                                       altered = do_subst_command(&sed_cmds[i], line);
+                                                       altered |= do_subst_command(&sed_cmds[i], line);
 
                                                break;