[BusyBox] Re: Bug Report:BB 1.00 sed(address "$" matches end of all input files)

Rob Landley rob at landley.net
Fri Nov 5 23:11:39 UTC 2004


On Tuesday 02 November 2004 03:48, Hiroshi Ito wrote:
> Hello, Rob.
>
> I found another bug.

Yes you did.

> ---BB's sed---
> /tmp # (echo 1; echo 2; echo 3) > a
> /tmp # sed -n -e '$ p' a a
> 3		    <= $ matches end of first file.
> 3

Okay, this is nasty.  The correct behavior is actually non-obvious.  Try this 
one in gnu sed:

sed -i -e '$ p' b c

I think this patch should fix it.  (Works for me, anyway, lemme know if it 
works for you and I'll check it in if so.)

Note that there's still a difference between gnu and busybox behavior when you 
do something like this (where a exists but g does not):

landley at cathair busybox-1.00]$ sed -n -e '$ p' a g
sed: can't read g: No such file or directory
3
[landley at cathair busybox-1.00]$ ./busybox sed -n -e '$ p' a g
sed: g: No such file or directory
[landley at cathair busybox-1.00]$

Once again, GNU does processing/checking on input that we do on output, and 
vice versa.  Considering the spec doesn't say what to do when an input file 
isn't found, I think we're ok...

Rob

--- busybox/editors/sed.c	2004-11-05 16:36:08.329190568 -0600
+++ busybox-1.00/editors/sed.c	2004-11-05 16:41:26.663796352 -0600
@@ -757,7 +757,7 @@
 
 #define sed_puts(s,n) 
missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
 
-static void process_file(FILE *file)
+static void process_file(FILE *file, int is_last_file)
 {
 	char *pattern_space, *next_line;
 	static int linenum = 0, missing_newline=0;
@@ -802,7 +802,7 @@
 				    !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
 
 			/* Or did we match last line of input? */
-				|| (sed_cmd->beg_line == -1 && next_line == NULL);
+				|| (sed_cmd->beg_line == -1 && !next_line && is_last_file);
 
 			/* Snapshot the value */
 
@@ -1172,14 +1172,14 @@
 			fprintf(stderr,"sed: Filename required for -i\n");
 			exit(1);
 		}
-		process_file(stdin);
+		process_file(stdin,1);
 	} else {
 		int i;
 		FILE *file;
 
 		for (i = optind; i < argc; i++) {
 			if(!strcmp(argv[i], "-") && !in_place) {
-				process_file(stdin);
+				process_file(stdin,i==argc-1);
 			} else {
 				file = bb_wfopen(argv[i], "r");
 				if (file) {
@@ -1196,7 +1196,7 @@
 						fchmod(fileno(nonstdout),statbuf.st_mode);
 						atexit(cleanup_outname);
 					}
-					process_file(file);
+					process_file(file,in_place || (i==argc-1));
 					fclose(file);
 					if(in_place) {
 						fclose(nonstdout);




More information about the busybox mailing list