Module Name:    src
Committed By:   dsl
Date:           Sat Aug 15 16:50:29 UTC 2009

Modified Files:
        src/usr.bin/sort: files.c fsort.c fsort.h msort.c

Log Message:
linebuf and linebuf_size are only used inside seq() - which also not
only has its own static variable, but will also extend the buffer.
Remove linebuf/size and change seq() to use a private, locally managed
buffer.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/usr.bin/sort/files.c
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/sort/fsort.c
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/sort/fsort.h
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/sort/msort.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/sort/files.c
diff -u src/usr.bin/sort/files.c:1.30 src/usr.bin/sort/files.c:1.31
--- src/usr.bin/sort/files.c:1.30	Sat Aug 15 16:10:40 2009
+++ src/usr.bin/sort/files.c	Sat Aug 15 16:50:29 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: files.c,v 1.30 2009/08/15 16:10:40 dsl Exp $	*/
+/*	$NetBSD: files.c,v 1.31 2009/08/15 16:50:29 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
 #include "fsort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: files.c,v 1.30 2009/08/15 16:10:40 dsl Exp $");
+__RCSID("$NetBSD: files.c,v 1.31 2009/08/15 16:50:29 dsl Exp $");
 __SCCSID("@(#)files.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -278,8 +278,7 @@
 			if (flno >= 0)
 				fstack[flno].fp = 0;
 		} else {
-			((char *) line->data)[60] = '\000';
-			warnx("makekey: line too long: ignoring %.100s...",
+			warnx("makekey: line too long: ignoring %.60s...",
 			    (char *)line->data);
 		}
 	}
@@ -291,18 +290,22 @@
 static int
 seq(FILE *fp, DBT *line)
 {
-	static u_char *buf, flag = 1;
+	static u_char *buf;
+	static size_t buf_size = DEFLLEN;
 	u_char *end, *pos;
 	int c;
-	u_char *nlinebuf;
+	u_char *new_buf;
 
-	if (flag) {
+	if (!buf) {
 		/* one-time initialization */
-		flag = 0;
-		buf = linebuf;
+		buf = malloc(buf_size);
+		if (!buf)
+		    err(2, "malloc of linebuf for %zu bytes failed",
+			    buf_size);
 		line->data = buf;
 	}
-	end = buf + linebuf_size;
+
+	end = buf + buf_size;
 	pos = buf;
 	while ((c = getc(fp)) != EOF) {
 		if ((*pos++ = c) == REC_D) {
@@ -310,25 +313,28 @@
 			return (0);
 		}
 		if (pos == end) {
-			nlinebuf = realloc(linebuf, linebuf_size * 2);
-			if (!nlinebuf)
-				err(2, "realloc of linebuf to %lu bytes failed",
-					(unsigned long)linebuf_size * 2);
-			linebuf = nlinebuf;
-			linebuf_size *= 2;
+			/* Long line - double size of buffer */
+			buf_size *= 2;
+			new_buf = realloc(buf, buf_size);
+			if (!new_buf)
+				err(2, "realloc of linebuf to %zu bytes failed",
+					buf_size);
 		
-			end = linebuf + linebuf_size;
-			pos = linebuf + (pos - buf);
-			line->data = buf = linebuf;
-			continue;
+			end = new_buf + buf_size;
+			pos = new_buf + (pos - buf);
+			buf = new_buf;
+			line->data = buf;
 		}
 	}
+
 	if (pos != buf) {
+		/* EOF part way through line - add line terminator */
 		*pos++ = REC_D;
 		line->size = pos - buf;
 		return (0);
-	} else
-		return (EOF);
+	}
+
+	return (EOF);
 }
 
 /*

Index: src/usr.bin/sort/fsort.c
diff -u src/usr.bin/sort/fsort.c:1.33 src/usr.bin/sort/fsort.c:1.34
--- src/usr.bin/sort/fsort.c:1.33	Sat Aug 15 09:48:46 2009
+++ src/usr.bin/sort/fsort.c	Sat Aug 15 16:50:29 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fsort.c,v 1.33 2009/08/15 09:48:46 dsl Exp $	*/
+/*	$NetBSD: fsort.c,v 1.34 2009/08/15 16:50:29 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -72,7 +72,7 @@
 #include "fsort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: fsort.c,v 1.33 2009/08/15 09:48:46 dsl Exp $");
+__RCSID("$NetBSD: fsort.c,v 1.34 2009/08/15 16:50:29 dsl Exp $");
 __SCCSID("@(#)fsort.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -80,9 +80,8 @@
 #include <string.h>
 
 static const u_char **keylist = 0;
-u_char *buffer = 0, *linebuf = 0;
+u_char *buffer = 0;
 size_t bufsize = DEFBUFSIZE;
-size_t linebuf_size;
 #define FSORTMAX 4
 int PANIC = FSORTMAX;
 
@@ -123,11 +122,6 @@
 		buffer = malloc(bufsize);
 		keylist = malloc(MAXNUM * sizeof(u_char *));
 		memset(keylist, 0, MAXNUM * sizeof(u_char *));
-		if (!SINGL_FLD) {
-			linebuf_size = DEFLLEN;
-			if ((linebuf = malloc(linebuf_size)) == NULL)
-				errx(2, "cannot allocate memory");
-		}
 	}
 	bufend = buffer + bufsize;
 	if (binno >= 0) {

Index: src/usr.bin/sort/fsort.h
diff -u src/usr.bin/sort/fsort.h:1.13 src/usr.bin/sort/fsort.h:1.14
--- src/usr.bin/sort/fsort.h:1.13	Mon Apr 28 20:24:15 2008
+++ src/usr.bin/sort/fsort.h	Sat Aug 15 16:50:29 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fsort.h,v 1.13 2008/04/28 20:24:15 martin Exp $	*/
+/*	$NetBSD: fsort.h,v 1.14 2009/08/15 16:50:29 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -84,8 +84,8 @@
  */
 #define MERGE_FNUM	16
 
-extern u_char *buffer, *linebuf;
-extern size_t bufsize, linebuf_size;
+extern u_char *buffer;
+extern size_t bufsize;
 
 /* temp files in the stack have a file descriptor, a largest bin (maxb)
  * which becomes the last non-empty bin (lastb) when the actual largest

Index: src/usr.bin/sort/msort.c
diff -u src/usr.bin/sort/msort.c:1.19 src/usr.bin/sort/msort.c:1.20
--- src/usr.bin/sort/msort.c:1.19	Sat Aug 15 09:48:46 2009
+++ src/usr.bin/sort/msort.c	Sat Aug 15 16:50:29 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: msort.c,v 1.19 2009/08/15 09:48:46 dsl Exp $	*/
+/*	$NetBSD: msort.c,v 1.20 2009/08/15 16:50:29 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
 #include "fsort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: msort.c,v 1.19 2009/08/15 09:48:46 dsl Exp $");
+__RCSID("$NetBSD: msort.c,v 1.20 2009/08/15 16:50:29 dsl Exp $");
 __SCCSID("@(#)msort.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -106,12 +106,6 @@
 		if (!buffer)
 			err(2, "fmerge(): malloc");
 		memset(buffer, 0, bufsize);
-
-		if (!linebuf && !SINGL_FLD) {
-			linebuf_size = DEFLLEN;
-			linebuf = malloc(linebuf_size);
-			memset(linebuf, 0, linebuf_size);
-		}
 	}
 
 	if (binno >= 0)
@@ -343,8 +337,6 @@
 	int c;
 	RECHEADER *crec, *prec, *trec;
 
-	if (!SINGL_FLD)
-		linebuf = malloc(DEFLLEN);
 	buffer = malloc(2 * (DEFLLEN + sizeof(TRECHEADER)));
 	crec = (RECHEADER *) buffer;
 	crec_end = buffer + DEFLLEN + sizeof(TRECHEADER);

Reply via email to