[BusyBox-cvs] CVS busybox/editors

CVS User landley landley at codepoet.org
Thu Nov 25 07:21:48 UTC 2004


Update of /var/cvs/busybox/editors
In directory nail:/tmp/cvs-serv31258/busybox/editors

Modified Files:
	sed.c 
Log Message:
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


--- /var/cvs/busybox/editors/sed.c	2004/10/30 06:54:19	1.171
+++ /var/cvs/busybox/editors/sed.c	2004/11/25 07:21:47	1.172
@@ -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 @@
 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 @@
 	}
 
 	if(hold_space) free(hold_space);
+
+    while(current_input_file<input_file_count)
+		fclose(input_file_list[current_input_file++]);
 }
 #endif
 
@@ -565,6 +574,8 @@
 	}
 }
 
+/* Append to a string, reallocating memory as necessary. */
+
 struct pipeline {
 	char *buf;	/* Space to hold string */
 	int idx;	/* Space used */
@@ -718,20 +729,29 @@
 	append_head=append_tail=NULL;
 }
 
-/* Get next line of input, flushing append buffer and noting if we hit EOF
- * without a newline on the last line.
+void add_input_file(FILE *file)
+{
+	input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *));
+	input_file_list[input_file_count++]=file;
+}
+
+/* Get next line of input from input_file_list, flushing append buffer and
+ * noting if we ran out of files without a newline on the last line we read.
  */
-static char *get_next_line(FILE * file, int *no_newline)
+static char *get_next_line(int *no_newline)
 {
-	char *temp;
+	char *temp=NULL;
 	int len;
 
 	flush_append();
-	temp=bb_get_line_from_file(file);
-	if(temp) {
-		len=strlen(temp);
-		if(len && temp[len-1]=='\n') temp[len-1]=0;
-		else *no_newline=1;
+	while(current_input_file<input_file_count) {
+		temp=bb_get_line_from_file(input_file_list[current_input_file]);
+		if(temp) {
+			len=strlen(temp);
+			*no_newline=!(len && temp[len-1]=='\n');
+			if(!*no_newline) temp[len-1]=0;
+			break;
+		} else fclose(input_file_list[current_input_file++]);
 	}
 
 	return temp;
@@ -757,15 +777,15 @@
 
 #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
 
-static void process_file(FILE *file)
+static void process_files(void)
 {
 	char *pattern_space, *next_line;
-	static int linenum = 0, missing_newline=0;
+	int linenum = 0, missing_newline=0;
 	int no_newline,next_no_newline=0;
 
-	next_line = get_next_line(file,&next_no_newline);
+	next_line = get_next_line(&next_no_newline);
 
-	/* go through every line in the file */
+	/* go through every line in each file */
 	for(;;) {
 		sed_cmd_t *sed_cmd;
 		int substituted=0;
@@ -775,7 +795,7 @@
 		no_newline=next_no_newline;
 
 		/* Read one line in advance so we can act on the last line, the '$' address */
-		next_line = get_next_line(file,&next_no_newline);
+		next_line = get_next_line(&next_no_newline);
 		linenum++;
 restart:
 		/* for every line, go through all the commands */
@@ -944,7 +964,7 @@
 							free(pattern_space);
 							pattern_space = next_line;
 							no_newline=next_no_newline;
-							next_line = get_next_line(file,&next_no_newline);
+							next_line = get_next_line(&next_no_newline);
 							linenum++;
 							break;
 						}
@@ -974,7 +994,7 @@
 							pattern_space[len]='\n';
 							strcpy(pattern_space+len+1, next_line);
 							no_newline=next_no_newline;
-							next_line = get_next_line(file,&next_no_newline);
+							next_line = get_next_line(&next_no_newline);
 							linenum++;
 						}
 						break;
@@ -1150,8 +1170,7 @@
 		}
 	}
 
-	/* if we didn't get a pattern from a -e and no command file was specified,
-	 * argv[optind] should be the pattern. no pattern, no worky */
+	/* if we didn't get a pattern from -e or -f, use argv[optind] */
 	if(getpat) {
 		if (argv[optind] == NULL)
 			bb_show_usage();
@@ -1172,14 +1191,16 @@
 			fprintf(stderr,"sed: Filename required for -i\n");
 			exit(1);
 		}
-		process_file(stdin);
+		add_input_file(stdin);
+		process_files();
 	} else {
 		int i;
 		FILE *file;
 
 		for (i = optind; i < argc; i++) {
 			if(!strcmp(argv[i], "-") && !in_place) {
-				process_file(stdin);
+				add_input_file(stdin);
+				process_files();
 			} else {
 				file = bb_wfopen(argv[i], "r");
 				if (file) {
@@ -1187,30 +1208,27 @@
 						struct stat statbuf;
 						outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
 						strcat(outname,"XXXXXX");
-						/* Set permissions of output file */
-						fstat(fileno(file),&statbuf);
 						mkstemp(outname);
 						nonstdout=bb_wfopen(outname,"w");
 						/* Set permissions of output file */
 						fstat(fileno(file),&statbuf);
 						fchmod(fileno(nonstdout),statbuf.st_mode);
 						atexit(cleanup_outname);
-					}
-					process_file(file);
-					fclose(file);
-					if(in_place) {
+						add_input_file(file);
+						process_files();
 						fclose(nonstdout);
 						nonstdout=stdout;
 						unlink(argv[i]);
 						rename(outname,argv[i]);
 						free(outname);
 						outname=0;
-					}
+					} else add_input_file(file);
 				} else {
 					status = EXIT_FAILURE;
 				}
 			}
 		}
+		if(input_file_count>current_input_file) process_files();
 	}
 
 	return status;



More information about the busybox-cvs mailing list