From: Rob Landley Date: Thu, 25 Nov 2004 07:21:47 +0000 (-0000) Subject: Hiroshi found another bug. Currently sed's $ triggers at end of every file, X-Git-Tag: 1_1_0~1124 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=dcc286607c83723f2b05f91da0a6942999576106;p=oweals%2Fbusybox.git Hiroshi found another bug. Currently sed's $ triggers at end of every file, and with multiple files SuSv3 says it should only trigger at the end of the LAST file. The trivial fix I tried first broke if the last file is empty. Fixing this properly required restructuring things to create a file list (actually a FILE * list), and then processing it all in one go. (There's probably a smaller way to do this, merging with append_list perhaps. But let's get the behavior correct first.) Note that editing files in place (-i) needs the _old_ behavior, with $ triggering at the end of each file. Here's a test of all the things this patch fixed. gnu and busybox seds produce the same results with this patch, and different without it. echo -n -e "1one\n1two\n1three" > ../test1 echo -n > ../test2 echo -e "3one\n3two\n3three" > ../test3 sed -n "$ p" ../test1 ../test2 ../test3 sed -n "$ p" ../test1 ../test2 sed -i -n "$ p" ../test1 ../test2 ../test3 --- diff --git a/editors/sed.c b/editors/sed.c index ae8d9832e..db364e3ee 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -34,7 +34,10 @@ resulting sed_cmd_t structures are appended to a linked list (sed_cmd_head/sed_cmd_tail). - process_file() does actual sedding, reading data lines from an input FILE * + add_input_file() adds a FILE * to the list of input files. We need to + know them all ahead of time to find the last line for the $ match. + + process_files() does actual sedding, reading data lines from each input FILE * (which could be stdin) and applying the sed command list (sed_cmd_head) to each of the resulting lines. @@ -116,6 +119,9 @@ static int be_quiet, in_place, regex_type; FILE *nonstdout; char *outname,*hold_space; +/* List of input files */ +int input_file_count,current_input_file; +FILE **input_file_list; static const char bad_format_in_subst[] = "bad format in substitution expression"; @@ -171,6 +177,9 @@ static void free_and_close_stuff(void) } if(hold_space) free(hold_space); + + while(current_input_filecurrent_input_file) process_files(); } return status;