svn commit: trunk/busybox: coreutils editors libbb

landley at busybox.net landley at busybox.net
Fri Feb 24 02:30:42 UTC 2006


Author: landley
Date: 2006-02-23 18:30:39 -0800 (Thu, 23 Feb 2006)
New Revision: 14254

Log:
A few changes falling out from the effort to make sed handle embedded NUL bytes.
Checking in to reduce the diff between my tree and svn...


Modified:
   trunk/busybox/coreutils/sort.c
   trunk/busybox/editors/sed.c
   trunk/busybox/libbb/get_line_from_file.c


Changeset:
Modified: trunk/busybox/coreutils/sort.c
===================================================================
--- trunk/busybox/coreutils/sort.c	2006-02-24 01:45:58 UTC (rev 14253)
+++ trunk/busybox/coreutils/sort.c	2006-02-24 02:30:39 UTC (rev 14254)
@@ -4,20 +4,10 @@
  *
  * Copyright (C) 2004 by Rob Landley <rob at landley.net>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * MAINTAINER: Rob Landley <rob at landley.net>
+ * 
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
  * See SuS3 sort standard at:
  * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
  */
@@ -142,7 +132,7 @@
 	return *pkey=xcalloc(1,sizeof(struct sort_key));
 }
 
-#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp) \
+#define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \
 										   : bb_get_chomped_line_from_file(fp)
 #else
 #define GET_LINE(fp)		bb_get_chomped_line_from_file(fp)

Modified: trunk/busybox/editors/sed.c
===================================================================
--- trunk/busybox/editors/sed.c	2006-02-24 01:45:58 UTC (rev 14253)
+++ trunk/busybox/editors/sed.c	2006-02-24 02:30:39 UTC (rev 14254)
@@ -8,6 +8,8 @@
  * Copyright (C) 2003 by Glenn McGrath <bug1 at iinet.net.au>
  * Copyright (C) 2003,2004 by Rob Landley <rob at landley.net>
  *
+ * MAINTAINER: Rob Landley <rob at landley.net>
+ * 
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
@@ -74,8 +76,8 @@
     regex_t *sub_match;	/* For 's/sub_match/string/' */
     int beg_line;		/* 'sed 1p'   0 == apply commands to all lines */
     int end_line;		/* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
-
-    FILE *file;			/* File (sr) command writes to, -1 for none. */
+	
+    FILE *file;			/* File (sw) command writes to, -1 for none. */
     char *string;		/* Data string for (saicytb) commands. */
 
     unsigned short which_match;		/* (s) Which match to replace (0 for all) */
@@ -83,7 +85,7 @@
     /* Bitfields (gcc won't group them if we don't) */
     unsigned int invert:1;			/* the '!' after the address */
     unsigned int in_match:1;		/* Next line also included in match? */
-    unsigned int no_newline:1;		/* Last line written by (sr) had no '\n' */
+    unsigned int no_newline:1;		/* Last line written by (sw) had no '\n' */
     unsigned int sub_p:1;			/* (s) print option */
 
 
@@ -120,6 +122,7 @@
 };
 static struct append_list *append_head=NULL, *append_tail=NULL;
 
+void free_and_close_stuff(void);
 #ifdef CONFIG_FEATURE_CLEAN_UP
 static void free_and_close_stuff(void)
 {
@@ -729,13 +732,13 @@
 	int len;
 
 	flush_append();
-	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;
+	while (current_input_file<input_file_count) {
+		temp = bb_get_chunk_from_file(input_file_list[current_input_file],&len);
+		if (temp) {
+			*no_newline = !(len && temp[len-1]=='\n');
+			if (!*no_newline) temp[len-1] = 0;
 			break;
+		// Close this file and advance to next one
 		} else fclose(input_file_list[current_input_file++]);
 	}
 
@@ -762,12 +765,15 @@
 
 #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
 
+/* Process all the lines in all the files */
+
 static void process_files(void)
 {
 	char *pattern_space, *next_line;
 	int linenum = 0, missing_newline=0;
 	int no_newline,next_no_newline=0;
 
+	/* Prime the pump */
 	next_line = get_next_line(&next_no_newline);
 
 	/* go through every line in each file */
@@ -779,7 +785,8 @@
 		if(!(pattern_space=next_line)) break;
 		no_newline=next_no_newline;
 
-		/* Read one line in advance so we can act on the last line, the '$' address */
+		/* Read one line in advance so we can act on the last line,
+		 * the '$' address */
 		next_line = get_next_line(&next_no_newline);
 		linenum++;
 restart:
@@ -921,16 +928,16 @@
 					/* Read file, append contents to output */
 					case 'r':
 					{
-						FILE *outfile;
+						FILE *rfile;
 
-						outfile = fopen(sed_cmd->string, "r");
-						if (outfile) {
+						rfile = fopen(sed_cmd->string, "r");
+						if (rfile) {
 							char *line;
 
-							while ((line = bb_get_chomped_line_from_file(outfile))
+							while ((line = bb_get_chomped_line_from_file(rfile))
 									!= NULL)
 								append(line);
-							bb_xprint_and_close_file(outfile);
+							bb_xprint_and_close_file(rfile);
 						}
 
 						break;
@@ -1107,11 +1114,9 @@
 {
 	int status = EXIT_SUCCESS, opt, getpat = 1;
 
-#ifdef CONFIG_FEATURE_CLEAN_UP
 	/* destroy command strings on exit */
-	if (atexit(free_and_close_stuff) == -1)
+	if (ENABLE_FEATURE_CLEAN_UP && atexit(free_and_close_stuff) == -1)
 		bb_perror_msg_and_die("atexit");
-#endif
 
 	/* Lie to autoconf when it starts asking stupid questions. */
 	if(argc==2 && !strcmp(argv[1],"--version")) {
@@ -1197,12 +1202,15 @@
 						if(-1==(nonstdoutfd=mkstemp(outname)))
 							bb_error_msg_and_die("no temp file");
 						nonstdout=fdopen(nonstdoutfd,"w");
+						
 						/* Set permissions of output file */
+						
 						fstat(fileno(file),&statbuf);
 						fchmod(nonstdoutfd,statbuf.st_mode);
 						add_input_file(file);
 						process_files();
 						fclose(nonstdout);
+						
 						nonstdout=stdout;
 						unlink(argv[i]);
 						rename(outname,argv[i]);

Modified: trunk/busybox/libbb/get_line_from_file.c
===================================================================
--- trunk/busybox/libbb/get_line_from_file.c	2006-02-24 01:45:58 UTC (rev 14253)
+++ trunk/busybox/libbb/get_line_from_file.c	2006-02-24 02:30:39 UTC (rev 14254)
@@ -2,22 +2,11 @@
 /*
  * Utility routines.
  *
- * Copyright (C) many different people.
- * If you wrote this, please acknowledge your work.
+ * Copyright (C) 2005, 2006 Rob Landley <rob at landley.net>
+ * Copyright (C) 2004 Erik Andersen <andersen at codepoet.org>
+ * Copyright (C) 2001 Matt Krai
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include <stdio.h>
@@ -25,14 +14,12 @@
 #include "libbb.h"
 
 /* get_line_from_file() - This function reads an entire line from a text file,
- * up to a newline. It returns a malloc'ed char * which must be stored and
- * free'ed  by the caller.  If 'c' is nonzero, the trailing '\n' (if any)
- * is removed.  In event of a read error or EOF, NULL is returned. */
+ * up to a newline or NUL byte.  It returns a malloc'ed char * which must be
+ * stored and free'ed  by the caller.  If end is null '\n' isn't considered
+ * and of line.  If end isn't null, length of the chunk read is stored in it. */
 
-static char *private_get_line_from_file(FILE *file, int c)
+char *bb_get_chunk_from_file(FILE *file, int *end)
 {
-#define GROWBY (80)		/* how large we will grow strings by */
-
 	int ch;
 	int idx = 0;
 	char *linebuf = NULL;
@@ -41,17 +28,12 @@
 	while ((ch = getc(file)) != EOF) {
 		/* grow the line buffer as necessary */
 		if (idx > linebufsz - 2) {
-			linebuf = xrealloc(linebuf, linebufsz += GROWBY);
+			linebuf = xrealloc(linebuf, linebufsz += 80);
 		}
 		linebuf[idx++] = (char)ch;
-		if (!ch) return linebuf;
-		if (c<2 && ch == '\n') {
-			if (c) {
-				--idx;
-			}
-			break;
-		}
+		if (!ch || (end && ch == '\n')) break;
 	}
+	if (end) *end = idx;
 	if (linebuf) {
 		if (ferror(file)) {
 			free(linebuf);
@@ -62,27 +44,19 @@
 	return linebuf;
 }
 
+/* Get line, including trailing /n if any */
 extern char *bb_get_line_from_file(FILE *file)
 {
-	return private_get_line_from_file(file, 0);
+	int i;
+	return bb_get_chunk_from_file(file, &i);
 }
 
+/* Get line.  Remove trailing /n */
 extern char *bb_get_chomped_line_from_file(FILE *file)
 {
-	return private_get_line_from_file(file, 1);
+	int i;
+	char *c=bb_get_chunk_from_file(file, &i);
+	if(i) c[--i]=0;
+	
+	return c;
 }
-
-extern char *bb_get_chunk_from_file(FILE *file)
-{
-	return private_get_line_from_file(file, 2);
-}
-
-
-/* END CODE */
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/




More information about the busybox-cvs mailing list