Module Name:    src
Committed By:   dsl
Date:           Sat Aug 15 18:40:02 UTC 2009

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

Log Message:
Remove reference to db.h by using separate ptr+len fields for the only
structure that used it.
Pass end of keybuf area, not size to enterkey() - largely to remove a
variable who'se use isn't obvious from the name!
The structute of this code sucks.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/sort/fields.c
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/sort/files.c
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/sort/sort.h

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/fields.c
diff -u src/usr.bin/sort/fields.c:1.21 src/usr.bin/sort/fields.c:1.22
--- src/usr.bin/sort/fields.c:1.21	Sat Aug 15 09:48:46 2009
+++ src/usr.bin/sort/fields.c	Sat Aug 15 18:40:01 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fields.c,v 1.21 2009/08/15 09:48:46 dsl Exp $	*/
+/*	$NetBSD: fields.c,v 1.22 2009/08/15 18:40:01 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 #include "sort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: fields.c,v 1.21 2009/08/15 09:48:46 dsl Exp $");
+__RCSID("$NetBSD: fields.c,v 1.22 2009/08/15 18:40:01 dsl Exp $");
 __SCCSID("@(#)fields.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -81,8 +81,8 @@
 	while ((*(pos+1) != '\0') && !((FLD_D | REC_D_F) & l_d_mask[*++pos]));\
 }
 		
-static u_char *enterfield(u_char *, u_char *, struct field *, int);
-static u_char *number(u_char *, u_char *, u_char *, u_char *, int);
+static u_char *enterfield(u_char *, const u_char *, struct field *, int);
+static u_char *number(u_char *, const u_char *, u_char *, u_char *, int);
 
 #define DECIMAL '.'
 #define OFFSET 128
@@ -97,19 +97,21 @@
  * followed by the original line.
  */
 length_t
-enterkey(RECHEADER *keybuf, DBT *line, int size, struct field fieldtable[])
+enterkey(RECHEADER *keybuf, const u_char *keybuf_end, u_char *line_data, size_t line_size, struct field fieldtable[])
 	/* keybuf:	 pointer to start of key */
 {
 	int i;
 	u_char *l_d_mask;
 	u_char *lineend, *pos;
-	u_char *endkey, *keypos;
+	const u_char *endkey;
+	u_char *keypos;
 	struct coldesc *clpos;
 	int col = 1;
 	struct field *ftpos;
+
 	l_d_mask = d_mask;
-	pos = (u_char *) line->data - 1;
-	lineend = (u_char *) line->data + line->size-1;
+	pos = line_data - 1;
+	lineend = line_data + line_size-1;
 				/* don't include rec_delimiter */
 
 	for (i = 0; i < ncols; i++) {
@@ -131,19 +133,19 @@
 	}
 	for (; i <= ncols; i++)
 		clist[i].start = clist[i].end = lineend;
-	if (clist[0].start < (u_char *) line->data)
+	if (clist[0].start < line_data)
 		clist[0].start++;
 
 	keypos = keybuf->data;
-	endkey = (u_char *) keybuf + size - line->size;
+	endkey = keybuf_end - line_size;
 	for (ftpos = fieldtable + 1; ftpos->icol.num; ftpos++)
 		if ((keypos = enterfield(keypos, endkey, ftpos,
 		    fieldtable->flags)) == NULL)
 			return (1);
 
 	keybuf->offset = keypos - keybuf->data;
-	keybuf->length = keybuf->offset + line->size;
-	if (keybuf->length + sizeof(TRECHEADER) > (length_t)size) {
+	keybuf->length = keybuf->offset + line_size;
+	if (keybuf->data + keybuf->length > keybuf_end) {
 		/* line too long for buffer */
 		return (1);
 	}
@@ -154,10 +156,10 @@
 	 * 2. we want stable sort and so the items should be sorted only by
 	 *    the relevant field[s]
 	 */
-	if (UNIQUE || (stable_sort && keybuf->offset < line->size))
+	if (UNIQUE || (stable_sort && keybuf->offset < line_size))
 		keypos[-1] = REC_D;
 
-	memcpy(keybuf->data + keybuf->offset, line->data, line->size);
+	memcpy(keybuf->data + keybuf->offset, line_data, line_size);
 	return (0);
 }
 
@@ -165,7 +167,7 @@
  * constructs a field (as defined by -k) within a key
  */
 static u_char *
-enterfield(u_char *tablepos, u_char *endkey, struct field *cur_fld, int gflags)
+enterfield(u_char *tablepos, const u_char *endkey, struct field *cur_fld, int gflags)
 {
 	u_char *start, *end, *lineend, *mask, *lweight;
 	struct column icol, tcol;
@@ -233,7 +235,7 @@
  */
 
 static u_char *
-number(u_char *pos, u_char *bufend, u_char *line, u_char *lineend, int Rflag)
+number(u_char *pos, const u_char *bufend, u_char *line, u_char *lineend, int Rflag)
 {
 	int or_sign, parity = 0;
 	int expincr = 1, exponent = -1;

Index: src/usr.bin/sort/files.c
diff -u src/usr.bin/sort/files.c:1.31 src/usr.bin/sort/files.c:1.32
--- src/usr.bin/sort/files.c:1.31	Sat Aug 15 16:50:29 2009
+++ src/usr.bin/sort/files.c	Sat Aug 15 18:40:01 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: files.c,v 1.31 2009/08/15 16:50:29 dsl Exp $	*/
+/*	$NetBSD: files.c,v 1.32 2009/08/15 18:40:01 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -65,13 +65,13 @@
 #include "fsort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: files.c,v 1.31 2009/08/15 16:50:29 dsl Exp $");
+__RCSID("$NetBSD: files.c,v 1.32 2009/08/15 18:40:01 dsl Exp $");
 __SCCSID("@(#)files.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
 #include <string.h>
 
-static int	seq(FILE *, DBT *);
+static ssize_t	seq(FILE *, u_char **);
 
 /*
  * this is the subroutine for file management for fsort().
@@ -174,6 +174,7 @@
 		pos += osz;
 		overflow = 0;
 	}
+
 	for (;;) {
 		if (flno >= 0 && (fp = fstack[flno].fp) == NULL)
 			return (EOF);
@@ -235,19 +236,17 @@
 {
 	static int filenum = 0;
 	static FILE *dbdesc = 0;
-	static DBT line[1];
+	static u_char *line_data;
+	static ssize_t line_size;
 	static int overflow = 0;
-	int c;
 
+	/* We get re-entered after returning BUFFEND - save old data */
 	if (overflow) {
-		overflow = enterkey(recbuf, line, bufend - (u_char *)recbuf,
-									ftbl);
-		if (overflow)
-			return (BUFFEND);
-		else
-			return (0);
+		overflow = enterkey(recbuf, bufend, line_data, line_size, ftbl);
+		return overflow ? BUFFEND : 0;
 	}
 
+	/* Loop through files until we find a line of input */
 	for (;;) {
 		if (flno >= 0) {
 			if (!(dbdesc = fstack[flno].fp))
@@ -260,35 +259,31 @@
 				err(2, "%s", filelist->names[filenum]);
 			filenum++;
 		}
-		if (!(c = seq(dbdesc, line))) {
-			if ((signed)line->size > bufend - recbuf->data) {
-				overflow = 1;
-			} else {
-				overflow = enterkey(recbuf, line,
-				    bufend - (u_char *) recbuf, ftbl);
-			}
-			if (overflow)
-				return (BUFFEND);
-			else
-				return (0);
-		}
-		if (c == EOF) {
-			FCLOSE(dbdesc);
-			dbdesc = 0;
-			if (flno >= 0)
-				fstack[flno].fp = 0;
-		} else {
-			warnx("makekey: line too long: ignoring %.60s...",
-			    (char *)line->data);
-		}
+		line_size = seq(dbdesc, &line_data);
+		if (line_size != 0)
+			/* Got a line */
+			break;
+
+		/* End of file ... */
+		FCLOSE(dbdesc);
+		dbdesc = 0;
+		if (flno >= 0)
+			fstack[flno].fp = 0;
+	}
+
+	if (line_size > bufend - recbuf->data) {
+		overflow = 1;
+	} else {
+		overflow = enterkey(recbuf, bufend, line_data, line_size, ftbl);
 	}
+	return overflow ? BUFFEND : 0;
 }
 
 /*
- * get a line pair from fp
+ * get a line of input from fp
  */
-static int
-seq(FILE *fp, DBT *line)
+static ssize_t
+seq(FILE *fp, u_char **line)
 {
 	static u_char *buf;
 	static size_t buf_size = DEFLLEN;
@@ -302,18 +297,19 @@
 		if (!buf)
 		    err(2, "malloc of linebuf for %zu bytes failed",
 			    buf_size);
-		line->data = buf;
 	}
 
 	end = buf + buf_size;
 	pos = buf;
 	while ((c = getc(fp)) != EOF) {
-		if ((*pos++ = c) == REC_D) {
-			line->size = pos - buf;
-			return (0);
+		*pos++ = c;
+		if (c == REC_D) {
+			*line = buf;
+			return pos - buf;
 		}
 		if (pos == end) {
 			/* Long line - double size of buffer */
+			/* XXX: Check here for stupidly long lines */
 			buf_size *= 2;
 			new_buf = realloc(buf, buf_size);
 			if (!new_buf)
@@ -323,18 +319,17 @@
 			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);
+		*line = buf;
+		return pos - buf;
 	}
 
-	return (EOF);
+	return 0;
 }
 
 /*

Index: src/usr.bin/sort/sort.h
diff -u src/usr.bin/sort/sort.h:1.20 src/usr.bin/sort/sort.h:1.21
--- src/usr.bin/sort/sort.h:1.20	Mon Apr 13 11:07:59 2009
+++ src/usr.bin/sort/sort.h	Sat Aug 15 18:40:01 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: sort.h,v 1.20 2009/04/13 11:07:59 lukem Exp $	*/
+/*	$NetBSD: sort.h,v 1.21 2009/08/15 18:40:01 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -65,7 +65,6 @@
 
 #include <sys/param.h>
 
-#include <db.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -174,7 +173,7 @@
 void	 append(const u_char **, int, int, FILE *,
 	    void (*)(const RECHEADER *, FILE *), struct field *);
 void	 concat(FILE *, FILE *);
-length_t enterkey(RECHEADER *, DBT *, int, struct field *);
+length_t enterkey(RECHEADER *, const u_char *, u_char *, size_t, struct field *);
 void	 fixit(int *, char **);
 void	 fldreset(struct field *);
 FILE	*ftmp(void);

Reply via email to