Module Name:    src
Committed By:   dsl
Date:           Thu Aug 20 06:36:26 UTC 2009

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

Log Message:
Delete more unwanted/unused cruft.
Simplify logic for reading input records.
Do a merge sort whenever we have 16 partial sorted blocks.
The patient is breathing, but still carrying a lot of extra weight.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/sort/append.c
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/sort/fields.c src/usr.bin/sort/sort.h
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/sort/fsort.c
cvs rdiff -u -r1.14 -r1.15 src/usr.bin/sort/fsort.h
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/sort/msort.c
cvs rdiff -u -r1.50 -r1.51 src/usr.bin/sort/sort.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/append.c
diff -u src/usr.bin/sort/append.c:1.18 src/usr.bin/sort/append.c:1.19
--- src/usr.bin/sort/append.c:1.18	Tue Aug 18 18:00:28 2009
+++ src/usr.bin/sort/append.c	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: append.c,v 1.18 2009/08/18 18:00:28 dsl Exp $	*/
+/*	$NetBSD: append.c,v 1.19 2009/08/20 06:36:25 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
 #include "sort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: append.c,v 1.18 2009/08/18 18:00:28 dsl Exp $");
+__RCSID("$NetBSD: append.c,v 1.19 2009/08/20 06:36:25 dsl Exp $");
 __SCCSID("@(#)append.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -73,13 +73,8 @@
 
 #define OUTPUT {							\
 	if ((n = cpos - ppos) > 1) {					\
-		for (; ppos < cpos; ++ppos)				\
-			*ppos -= depth;				\
 		ppos -= n;						\
-		if (stable_sort)					\
-			sradixsort(ppos, n, wts1, REC_D);		\
-		else							\
-			radixsort(ppos, n, wts1, REC_D);		\
+		radix_sort(ppos, n, wts1, REC_D);			\
 		for (; ppos < cpos; ppos++) {				\
 			prec = (const RECHEADER *) (*ppos - REC_DATA_OFFSET);\
 			put(prec, fp);					\
@@ -91,35 +86,36 @@
  * copy sorted lines to output; check for uniqueness
  */
 void
-append(const u_char **keylist, int nelem, int depth, FILE *fp, put_func_t put,
+append(const u_char **keylist, int nelem, FILE *fp, put_func_t put,
     struct field *ftbl)
 {
 	u_char *wts, *wts1;
 	int n;
-	int hdr_off;
 	const u_char **cpos, **ppos, **lastkey;
 	const u_char *cend, *pend, *start;
 	const struct recheader *crec, *prec;
 
 	if (*keylist == '\0' && UNIQUE)
 		return;
+
 	wts1 = wts = ftbl[0].weights;
-	if ((!UNIQUE) && SINGL_FLD) {
-		if ((ftbl[0].flags & F) && (ftbl[0].flags & R))
+	if ((!UNIQUE) && SINGL_FLD && ftbl[0].flags & F) {
+		/* Folding case */
+		if (ftbl[0].flags & R)
 			wts1 = Rascii;
-		else if (ftbl[0].flags & F)
+		else
 			wts1 = ascii;
 	}
+
 	lastkey = keylist + nelem;
-	hdr_off = REC_DATA_OFFSET + depth;
 	if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
 		ppos = keylist;
-		prec = (const RECHEADER *) (*ppos - hdr_off);
+		prec = (const RECHEADER *) (*ppos - REC_DATA_OFFSET);
 		if (UNIQUE)
 			put(prec, fp);
 		for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
-			crec = (const RECHEADER *) (*cpos - hdr_off);
-			if (crec->length  == prec->length) {
+			crec = (const RECHEADER *) (*cpos - REC_DATA_OFFSET);
+			if (crec->length == prec->length) {
 				/*
 				 * Set pend and cend so that trailing NUL and
 				 * record separator is ignored.
@@ -151,10 +147,10 @@
 		if (!UNIQUE)  { OUTPUT; }
 	} else if (UNIQUE) {
 		ppos = keylist;
-		prec = (const RECHEADER *) (*ppos - hdr_off);
+		prec = (const RECHEADER *) (*ppos - REC_DATA_OFFSET);
 		put(prec, fp);
 		for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
-			crec = (const RECHEADER *) (*cpos - hdr_off);
+			crec = (const RECHEADER *) (*cpos - REC_DATA_OFFSET);
 			if (crec->offset == prec->offset) {
 				/*
 				 * Set pend and cend so that trailing NUL and
@@ -179,7 +175,7 @@
 			}
 		}
 	} else for (cpos = keylist; cpos < lastkey; cpos++) {
-		crec = (const RECHEADER *) (*cpos - hdr_off);
+		crec = (const RECHEADER *) (*cpos - REC_DATA_OFFSET);
 		put(crec, fp);
 	}
 }

Index: src/usr.bin/sort/fields.c
diff -u src/usr.bin/sort/fields.c:1.23 src/usr.bin/sort/fields.c:1.24
--- src/usr.bin/sort/fields.c:1.23	Sat Aug 15 21:26:32 2009
+++ src/usr.bin/sort/fields.c	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fields.c,v 1.23 2009/08/15 21:26:32 dsl Exp $	*/
+/*	$NetBSD: fields.c,v 1.24 2009/08/20 06:36:25 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.23 2009/08/15 21:26:32 dsl Exp $");
+__RCSID("$NetBSD: fields.c,v 1.24 2009/08/20 06:36:25 dsl Exp $");
 __SCCSID("@(#)fields.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -97,7 +97,8 @@
  * followed by the original line.
  */
 length_t
-enterkey(RECHEADER *keybuf, const u_char *keybuf_end, u_char *line_data, size_t line_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;
@@ -169,7 +170,8 @@
  * constructs a field (as defined by -k) within a key
  */
 static u_char *
-enterfield(u_char *tablepos, const 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;
Index: src/usr.bin/sort/sort.h
diff -u src/usr.bin/sort/sort.h:1.23 src/usr.bin/sort/sort.h:1.24
--- src/usr.bin/sort/sort.h:1.23	Tue Aug 18 18:00:28 2009
+++ src/usr.bin/sort/sort.h	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: sort.h,v 1.23 2009/08/18 18:00:28 dsl Exp $	*/
+/*	$NetBSD: sort.h,v 1.24 2009/08/20 06:36:25 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -171,17 +171,16 @@
 extern struct coldesc *clist;
 extern int ncols;
 
-void	 append(const u_char **, int, int, FILE *,
+void	 append(const u_char **, int, FILE *,
 	    void (*)(const RECHEADER *, FILE *), struct field *);
 void	 concat(FILE *, FILE *);
 length_t enterkey(RECHEADER *, const u_char *, u_char *, size_t, struct field *);
 void	 fixit(int *, char **);
 void	 fldreset(struct field *);
 FILE	*ftmp(void);
-void	 fmerge(int, int, struct filelist *, int,
+void	 fmerge(int, struct filelist *, int,
 		get_func_t, FILE *, put_func_t, struct field *);
-void	 fsort(int, int, int, struct filelist *, int, FILE *,
-		struct field *);
+void	 fsort(struct filelist *, int, FILE *, struct field *);
 int	 geteasy(int, int, struct filelist *,
 	    int, RECHEADER *, u_char *, struct field *);
 int	 makekey(int, int, struct filelist *,
@@ -189,7 +188,6 @@
 int	 makeline(int, int, struct filelist *,
 	    int, RECHEADER *, u_char *, struct field *);
 void	 num_init(void);
-void	 onepass(const u_char **, int, long, long *, u_char *, FILE *);
 int	 optval(int, int);
 void	 order(struct filelist *, get_func_t, struct field *);
 void	 putline(const RECHEADER *, FILE *);

Index: src/usr.bin/sort/fsort.c
diff -u src/usr.bin/sort/fsort.c:1.37 src/usr.bin/sort/fsort.c:1.38
--- src/usr.bin/sort/fsort.c:1.37	Tue Aug 18 18:00:28 2009
+++ src/usr.bin/sort/fsort.c	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fsort.c,v 1.37 2009/08/18 18:00:28 dsl Exp $	*/
+/*	$NetBSD: fsort.c,v 1.38 2009/08/20 06:36:25 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.37 2009/08/18 18:00:28 dsl Exp $");
+__RCSID("$NetBSD: fsort.c,v 1.38 2009/08/20 06:36:25 dsl Exp $");
 __SCCSID("@(#)fsort.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -82,148 +82,112 @@
 static const u_char **keylist = 0;
 u_char *buffer = 0;
 size_t bufsize = DEFBUFSIZE;
-#define FSORTMAX 4
-int PANIC = FSORTMAX;
 
 struct tempfile fstack[MAXFCT];
-#define MSTART		(MAXFCT - MERGE_FNUM)
-#define	CHECKFSTACK(n)					\
-	if (n >= MAXFCT)				\
-		errx(2, "fstack: too many temporary files; use -H or sort in pieces")
-	
+
 #define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
 
 void
-fsort(int binno, int depth, int top, struct filelist *filelist, int nfiles,
-    FILE *outfp, struct field *ftbl)
+fsort(struct filelist *filelist, int nfiles, FILE *outfp, struct field *ftbl)
 {
-	const u_char **keypos;
+	const u_char **keypos, **keyp;
 	u_char *bufend;
-	u_char *weights;
-	int ntfiles, mfct = 0;
+	int mfct = 0;
 	int c, nelem;
 	get_func_t get;
 	struct recheader *crec;
-	struct field tfield[2];
 	u_char *nbuffer;
 
-	memset(tfield, 0, sizeof(tfield));
-	if (ftbl[0].flags & R)
-		tfield[0].weights = Rascii;
-	else
-		tfield[0].weights = ascii;
-	tfield[0].icol.num = 1;
-	weights = ftbl[0].weights;
 	if (!buffer) {
 		buffer = malloc(bufsize);
 		keylist = malloc(MAXNUM * sizeof(u_char *));
 		memset(keylist, 0, MAXNUM * sizeof(u_char *));
 	}
 	bufend = buffer + bufsize;
+
 	if (SINGL_FLD)
+		/* Key and data are one! */
 		get = makeline;
 	else
+		/* Key (merged key fields) added before data */
 		get = makekey;
 
-	c = nelem = ntfiles = 0; /* XXXGCC -Wuninitialized m68k */
-	keypos = keylist;	     /* XXXGCC -Wuninitialized m68k */
-	crec = (RECHEADER *) buffer; /* XXXGCC -Wuninitialized m68k */
-	while (c != EOF) {
+	/* Loop through reads of chunk of input files that get sorted
+	 * and then merged together. */
+	for (;;) {
 		keypos = keylist;
 		nelem = 0;
 		crec = (RECHEADER *) buffer;
 
-	   do_read:
-		while ((c = get(-1, top, filelist, nfiles, crec,
-		    bufend, ftbl)) == 0) {
-			*keypos++ = crec->data + depth;
-			if (++nelem == MAXNUM) {
-				c = BUFFEND;
-				break;
+		/* Loop reading records */
+		for (;;) {
+			c = get(-1, 0, filelist, nfiles, crec, bufend, ftbl);
+			/* 'c' is 0, EOF or BUFFEND */
+			if (c == 0) {
+				/* Save start of key in input buffer */
+				*keypos++ = crec->data;
+				if (++nelem == MAXNUM) {
+					c = BUFFEND;
+					break;
+				}
+				crec = (RECHEADER *)((char *) crec +
+				    SALIGN(crec->length) + REC_DATA_OFFSET);
+				continue;
 			}
-			crec =(RECHEADER *)((char *) crec +
-			    SALIGN(crec->length) + REC_DATA_OFFSET);
-		}
+			if (c == EOF)
+				break;
+			if (nelem >= MAXNUM || bufsize >= MAXBUFSIZE)
+				/* Need to sort and save this lot of data */
+				break;
 
-		if (c == BUFFEND && nelem < MAXNUM
-		    && bufsize < MAXBUFSIZE) {
-			const u_char **keyp;
-			u_char *oldb = buffer;
-
-			/* buffer was too small for data, allocate
-			 * bigger buffer */
-			nbuffer = realloc(buffer, bufsize * 2);
+			/* c == BUFFEND, and we can process more data */
+			/* Allocate a larger buffer for this lot of data */
+			bufsize *= 2;
+			nbuffer = realloc(buffer, bufsize);
 			if (!nbuffer) {
-				err(2, "failed to realloc buffer to %lu bytes",
-					(unsigned long) bufsize * 2);
+				err(2, "failed to realloc buffer to %zu bytes",
+					bufsize);
 			}
-			buffer = nbuffer;
-			bufsize *= 2;
-			bufend = buffer + bufsize;
 
 			/* patch up keylist[] */
 			for (keyp = &keypos[-1]; keyp >= keylist; keyp--)
-				*keyp = buffer + (*keyp - oldb);
+				*keyp = nbuffer + (*keyp - buffer);
 
-			crec = (RECHEADER *) (buffer + ((u_char *)crec - oldb));
-			goto do_read;
+			crec = (RECHEADER *) (nbuffer + ((u_char *)crec - buffer));
+			buffer = nbuffer;
+			bufend = buffer + bufsize;
 		}
 
-		if (c != BUFFEND && !ntfiles && !mfct) {
-			/* do not push */
-			continue;
+		/* Sort this set of records */
+		if (radix_sort(keylist, nelem, ftbl[0].weights, REC_D))
+			err(2, NULL);
+
+		if (c == EOF && mfct == 0) {
+			/* all the data is (sorted) in the buffer */
+			append(keylist, nelem, outfp, putline, ftbl);
+			break;
 		}
 
-		/* push */
-		fstack[MSTART + mfct].fp = ftmp();
-		if (radix_sort(keylist, nelem, weights, REC_D))
-			err(2, NULL);
-		append(keylist, nelem, depth, fstack[MSTART + mfct].fp, putrec,
-		    ftbl);
+		/* Save current data to a temporary file for a later merge */
+		fstack[mfct].fp = ftmp();
+		append(keylist, nelem, fstack[mfct].fp, putrec, ftbl);
 		mfct++;
-		/* reduce number of open files */
-		if (mfct == MERGE_FNUM ||(c == EOF && ntfiles)) {
-			/*
-			 * Only copy extra incomplete crec
-			 * data if there are any.
-			 */
-			int nodata = (bufend >= (u_char *)crec
-			    && bufend <= crec->data);
-			size_t sz=0;
-			u_char *tmpbuf=NULL;
-
-			if (!nodata) {
-				sz = bufend - crec->data;
-				tmpbuf = malloc(sz);
-				memmove(tmpbuf, crec->data, sz);
-			}
 
-			CHECKFSTACK(ntfiles);
-			fstack[ntfiles].fp = ftmp();
-			fmerge(0, MSTART, filelist, mfct, geteasy,
-			    fstack[ntfiles].fp, putrec, ftbl);
-			ntfiles++;
-			mfct = 0;
-
-			if (!nodata) {
-				memmove(crec->data, tmpbuf, sz);
-				free(tmpbuf);
-			}
+		if (c == EOF) {
+			/* merge to output file */
+			fmerge(0, filelist, mfct, geteasy, outfp, putline,
+			    ftbl);
+			break;
 		}
-	}
 
-	if (!ntfiles && !mfct) {	/* everything in memory--pop */
-		if (nelem > 1 && radix_sort(keylist, nelem, weights, REC_D))
-			err(2, NULL);
-		if (nelem > 0)
-			append(keylist, nelem, depth, outfp, putline, ftbl);
+		if (mfct == MERGE_FNUM) {
+			/* Merge the files we have */
+			FILE *fp = ftmp();
+			fmerge(0, filelist, mfct, geteasy, fp, putrec, ftbl);
+			mfct = 1;
+			fstack[0].fp = fp;
+		}
 	}
-	if (!ntfiles)
-		fmerge(0, MSTART, filelist, mfct, geteasy,
-		    outfp, putline, ftbl);
-	else
-		fmerge(0, 0, filelist, ntfiles, geteasy,
-		    outfp, putline, ftbl);
 
 	free(keylist);
 	keylist = NULL;

Index: src/usr.bin/sort/fsort.h
diff -u src/usr.bin/sort/fsort.h:1.14 src/usr.bin/sort/fsort.h:1.15
--- src/usr.bin/sort/fsort.h:1.14	Sat Aug 15 16:50:29 2009
+++ src/usr.bin/sort/fsort.h	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: fsort.h,v 1.14 2009/08/15 16:50:29 dsl Exp $	*/
+/*	$NetBSD: fsort.h,v 1.15 2009/08/20 06:36:25 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -87,16 +87,8 @@
 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
- * bin is smaller than max(half the total file, BUFSIZE)
- * Max_o is the offset of maxb so it can be sought after the other bins
- * are sorted.
-*/
+/* Temporary files contian data (with record headers) in sorted order */
 struct tempfile {
 	FILE *fp;
-	u_char maxb;
-	u_char lastb;
-	off_t max_o;
 };
 extern struct tempfile fstack[MAXFCT];

Index: src/usr.bin/sort/msort.c
diff -u src/usr.bin/sort/msort.c:1.21 src/usr.bin/sort/msort.c:1.22
--- src/usr.bin/sort/msort.c:1.21	Sun Aug 16 19:53:43 2009
+++ src/usr.bin/sort/msort.c	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: msort.c,v 1.21 2009/08/16 19:53:43 dsl Exp $	*/
+/*	$NetBSD: msort.c,v 1.22 2009/08/20 06:36:25 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.21 2009/08/16 19:53:43 dsl Exp $");
+__RCSID("$NetBSD: msort.c,v 1.22 2009/08/20 06:36:25 dsl Exp $");
 __SCCSID("@(#)msort.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -89,13 +89,12 @@
 static void merge(int, int, get_func_t, FILE *, put_func_t, struct field *);
 
 void
-fmerge(int binno, int top, struct filelist *filelist, int nfiles,
+fmerge(int binno, struct filelist *filelist, int nfiles,
     get_func_t get, FILE *outfp, put_func_t fput, struct field *ftbl)
 {
 	FILE *tout;
 	int i, j, last;
 	put_func_t put;
-	struct tempfile *l_fstack;
 
 	wts = ftbl->weights;
 	if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
@@ -108,11 +107,6 @@
 		memset(buffer, 0, bufsize);
 	}
 
-	if (binno >= 0)
-		l_fstack = fstack + top;
-	else
-		l_fstack = fstack;
-
 	while (nfiles) {
 		put = putrec;
 		for (j = 0; j < nfiles; j += MERGE_FNUM) {
@@ -125,18 +119,18 @@
 			last = min(MERGE_FNUM, nfiles - j);
 			if (binno < 0) {
 				for (i = 0; i < last; i++)
-					if (!(l_fstack[i+MAXFCT-1-MERGE_FNUM].fp =
+					if (!(fstack[i+MAXFCT-1-MERGE_FNUM].fp =
 					    fopen(filelist->names[j+i], "r")))
 						err(2, "%s",
 							filelist->names[j+i]);
 				merge(MAXFCT-1-MERGE_FNUM, last, get, tout, put, ftbl);
 			} else {
 				for (i = 0; i< last; i++)
-					rewind(l_fstack[i+j].fp);
-				merge(top+j, last, get, tout, put, ftbl);
+					rewind(fstack[i+j].fp);
+				merge(j, last, get, tout, put, ftbl);
 			}
 			if (nfiles > MERGE_FNUM)
-				l_fstack[j/MERGE_FNUM].fp = tout;
+				fstack[j/MERGE_FNUM].fp = tout;
 		}
 		nfiles = (nfiles + (MERGE_FNUM - 1)) / MERGE_FNUM;
 		if (nfiles == 1)
@@ -144,7 +138,6 @@
 		if (binno < 0) {
 			binno = 0;
 			get = geteasy;
-			top = 0;
 		}
 	}
 }

Index: src/usr.bin/sort/sort.c
diff -u src/usr.bin/sort/sort.c:1.50 src/usr.bin/sort/sort.c:1.51
--- src/usr.bin/sort/sort.c:1.50	Tue Aug 18 18:00:28 2009
+++ src/usr.bin/sort/sort.c	Thu Aug 20 06:36:25 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: sort.c,v 1.50 2009/08/18 18:00:28 dsl Exp $	*/
+/*	$NetBSD: sort.c,v 1.51 2009/08/20 06:36:25 dsl Exp $	*/
 
 /*-
  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -76,7 +76,7 @@
 #endif /* not lint */
 
 #ifndef lint
-__RCSID("$NetBSD: sort.c,v 1.50 2009/08/18 18:00:28 dsl Exp $");
+__RCSID("$NetBSD: sort.c,v 1.51 2009/08/20 06:36:25 dsl Exp $");
 __SCCSID("@(#)sort.c	8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -127,6 +127,7 @@
 	struct field *fldtab, *p;
 	size_t fldtab_sz = 3, fidx = 0;
 	struct filelist filelist;
+	int num_input_files;
 	FILE *outfp = NULL;
 	struct rlimit rl;
 	struct stat st;
@@ -146,6 +147,7 @@
 	fldtab = malloc(fldtab_sz * sizeof(*fldtab));
 	memset(fldtab, 0, fldtab_sz * sizeof(*fldtab));
 
+	/* Convert "+field" args to -f format */
 	fixit(&argc, argv);
 
 	if (!(tmpdir = getenv("TMPDIR")))
@@ -240,6 +242,7 @@
 			usage(NULL);
 		}
 	}
+
 	if (cflag && argc > optind+1)
 		errx(2, "too many input files for -c option");
 	if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
@@ -248,6 +251,7 @@
 	}
 	if (mflag && argc - optind > (MAXFCT - (16+1))*16)
 		errx(2, "too many input files for -m option");
+
 	for (i = optind; i < argc; i++) {
 		/* allow one occurrence of /dev/stdin */
 		if (!strcmp(argv[i], "-") || !strcmp(argv[i], _PATH_STDIN)) {
@@ -258,12 +262,15 @@
 				stdinflag = 1;
 
 			/* change to /dev/stdin if '-' */
-			if (argv[i][0] == '-')
-				argv[i] = __UNCONST(_PATH_STDIN);
+			if (argv[i][0] == '-') {
+				static char path_stdin[] = _PATH_STDIN;
+				argv[i] = path_stdin;
+			}
 
 		} else if ((ch = access(argv[i], R_OK)))
 			err(2, "%s", argv[i]);
 	}
+
 	if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
 		SINGL_FLD = 1;
 		fldtab[0].icol.num = 1;
@@ -278,13 +285,15 @@
 	settables(fldtab[0].flags);
 	num_init();
 	fldtab->weights = gweights;
+
 	if (optind == argc) {
 		static const char * const names[] = { _PATH_STDIN, NULL };
-
 		filelist.names = names;
-		optind--;
-	} else
+		num_input_files = 1;
+	} else {
 		filelist.names = (const char * const *) &argv[optind];
+		num_input_files = argc - optind;
+	}
 
 	if (SINGL_FLD)
 		get = makeline;
@@ -313,15 +322,15 @@
 		    outpath);
 		if ((outfd = mkstemp(toutpath)) == -1)
 			err(2, "Cannot create temporary file `%s'", toutpath);
-		if ((outfp = fdopen(outfd, "w")) == NULL)
-			err(2, "Cannot open temporary file `%s'", toutpath);
-		outfile = toutpath;
 		(void)atexit(cleanup);
 		act.sa_handler = onsignal;
 		(void) sigemptyset(&act.sa_mask);
 		act.sa_flags = SA_RESTART | SA_RESETHAND;
 		for (i = 0; sigtable[i]; ++i)	/* always unlink toutpath */
 			sigaction(sigtable[i], &act, 0);
+		outfile = toutpath;
+		if ((outfp = fdopen(outfd, "w")) == NULL)
+			err(2, "Cannot open temporary file `%s'", toutpath);
 	} else {
 		outfile = outpath;
 
@@ -330,10 +339,10 @@
 	}
 
 	if (mflag) {
-		fmerge(-1, 0, &filelist, argc-optind, get, outfp, putline,
+		fmerge(-1, &filelist, num_input_files, get, outfp, putline,
 			fldtab);
 	} else
-		fsort(-1, 0, 0, &filelist, argc-optind, outfp, fldtab);
+		fsort(&filelist, num_input_files, outfp, fldtab);
 
 	if (outfile != outpath) {
 		if (access(outfile, F_OK))
@@ -354,6 +363,7 @@
 			err(2, "cannot link %s: output left in %s",
 			    outpath, outfile);
 		(void)unlink(outfile);
+		toutpath[0] = 0;
 	}
 	exit(0);
 }

Reply via email to