hush: replace flag bytes in struct o_string with bit flags
[oweals/busybox.git] / editors / sed.c
index 7fe2809b313b11f9a7cde63b73d0bb1f07f55db4..8d9f7b25be0fbbe6609a77bb5f0d90049b5a4347 100644 (file)
@@ -10,7 +10,7 @@
  *
  * MAINTAINER: Rob Landley <rob@landley.net>
  *
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* Code overview.
 #include "libbb.h"
 #include "xregex.h"
 
+enum {
+       OPT_in_place = 1 << 0,
+};
+
 /* Each sed command turns into one of these structures. */
 typedef struct sed_cmd_s {
        /* Ordered by alignment requirements: currently 36 bytes on x86 */
@@ -917,7 +921,7 @@ static void process_files(void)
                        /* Or did we match the start of a numerical range? */
                        || (sed_cmd->beg_line > 0
                            && (sed_cmd->end_line || sed_cmd->end_match
-                                 /* note: even if end numeric and is < linenum too,
+                                 /* note: even if end is numeric and is < linenum too,
                                   * GNU sed matches! We match too */
                                ? (sed_cmd->beg_line <= linenum)    /* N,end */
                                : (sed_cmd->beg_line == linenum)    /* N */
@@ -938,8 +942,11 @@ static void process_files(void)
 
                if (matched) {
                        /* once matched, "n,xxx" range is dead, disabling it */
-                       if (sed_cmd->beg_line > 0)
+                       if (sed_cmd->beg_line > 0
+                        && !(option_mask32 & OPT_in_place) /* but not for -i */
+                       ) {
                                sed_cmd->beg_line = -2;
+                       }
                        sed_cmd->in_match = !(
                                /* has the ending line come, or is this a single address command? */
                                (sed_cmd->end_line ?
@@ -985,6 +992,8 @@ static void process_files(void)
                }
 
                /* actual sedding */
+               //bb_error_msg("pattern_space:'%s' next_line:'%s' cmd:%c",
+               //pattern_space, next_line, sed_cmd->cmd);
                switch (sed_cmd->cmd) {
 
                /* Print line number */
@@ -1111,10 +1120,16 @@ static void process_files(void)
                {
                        int len;
                        /* If no next line, jump to end of script and exit. */
+                       /* http://www.gnu.org/software/sed/manual/sed.html:
+                        * "Most versions of sed exit without printing anything
+                        * when the N command is issued on the last line of
+                        * a file. GNU sed prints pattern space before exiting
+                        * unless of course the -n command switch has been
+                        * specified. This choice is by design."
+                        */
                        if (next_line == NULL) {
-                               free(next_line);
-                               next_line = NULL;
-                               goto discard_line;
+                               //goto discard_line;
+                               goto discard_commands; /* GNU behavior */
                        }
                        /* Append next_line, read new next_line. */
                        len = strlen(pattern_space);
@@ -1270,9 +1285,6 @@ static void add_cmd_block(char *cmdstr)
 int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int sed_main(int argc UNUSED_PARAM, char **argv)
 {
-       enum {
-               OPT_in_place = 1 << 0,
-       };
        unsigned opt;
        llist_t *opt_e, *opt_f;
        int status = EXIT_SUCCESS;
@@ -1292,6 +1304,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
        opt_e = opt_f = NULL;
        opt_complementary = "e::f::" /* can occur multiple times */
                            "nn"; /* count -n */
+       /* -i must be first, to match OPT_in_place definition */
        opt = getopt32(argv, "irne:f:", &opt_e, &opt_f,
                            &G.be_quiet); /* counter for -n */
        //argc -= optind;
@@ -1333,7 +1346,6 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
                if (opt & OPT_in_place)
                        bb_error_msg_and_die(bb_msg_requires_arg, "-i");
                add_input_file(stdin);
-               process_files();
        } else {
                int i;
                FILE *file;
@@ -1365,6 +1377,8 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 
                        /* Set permissions/owner of output file */
                        fstat(fileno(file), &statbuf);
+                       /* chmod'ing AFTER chown would preserve suid/sgid bits,
+                        * but GNU sed 4.2.1 does not preserve them either */
                        fchmod(nonstdoutfd, statbuf.st_mode);
                        fchown(nonstdoutfd, statbuf.st_uid, statbuf.st_gid);
                        add_input_file(file);
@@ -1377,9 +1391,13 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
                        free(G.outname);
                        G.outname = NULL;
                }
-               if (G.input_file_count > G.current_input_file)
-                       process_files();
+               /* Here, to handle "sed 'cmds' nonexistent_file" case we did:
+                * if (G.current_input_file >= G.input_file_count)
+                *      return status;
+                * but it's not needed since process_files() works correctly
+                * in this case too. */
        }
+       process_files();
 
        return status;
 }