summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/sort/fsort.c63
-rw-r--r--usr.bin/sort/fsort.h4
-rw-r--r--usr.bin/sort/msort.c30
-rw-r--r--usr.bin/sort/sort.c12
4 files changed, 42 insertions, 67 deletions
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c
index 6502a44a70b..5a0e98c4c40 100644
--- a/usr.bin/sort/fsort.c
+++ b/usr.bin/sort/fsort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.c,v 1.14 2006/10/28 21:14:29 naddy Exp $ */
+/* $OpenBSD: fsort.c,v 1.15 2006/10/29 18:40:34 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -36,7 +36,7 @@
#if 0
static char sccsid[] = "@(#)fsort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: fsort.c,v 1.14 2006/10/28 21:14:29 naddy Exp $";
+static char rcsid[] = "$OpenBSD: fsort.c,v 1.15 2006/10/29 18:40:34 millert Exp $";
#endif
#endif /* not lint */
@@ -53,8 +53,8 @@ static char rcsid[] = "$OpenBSD: fsort.c,v 1.14 2006/10/28 21:14:29 naddy Exp $"
#include <stdlib.h>
#include <string.h>
-u_char **keylist = 0, *buffer = 0, *linebuf = 0;
-size_t bufsize, linebuf_size;
+u_char *buffer = NULL, *bufend = NULL, *linebuf = NULL;
+size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN;
struct tempfile fstack[MAXFCT];
extern char toutpath[];
#define FSORTMAX 4
@@ -64,8 +64,8 @@ void
fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
struct field *ftbl)
{
- u_char *bufend, **keypos, *tmpbuf;
- u_char *weights;
+ u_char *weights, **keypos, *tmpbuf;
+ static u_char **keylist;
int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
int c, nelem;
long sizes[NBINS+1];
@@ -85,18 +85,10 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
tfield[0].weights = ascii;
tfield[0].icol.num = 1;
weights = ftbl[0].weights;
- if (!buffer) {
- bufsize = BUFSIZE;
- if ((buffer = malloc(bufsize + 1)) == NULL ||
- (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
- errx(2, "cannot allocate memory");
- if (!SINGL_FLD) {
- linebuf_size = MAXLLEN;
- if ((linebuf = malloc(linebuf_size)) == NULL)
- errx(2, "cannot allocate memory");
- }
+ if (keylist == NULL) {
+ if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
+ err(2, NULL);
}
- bufend = buffer + bufsize;
if (binno >= 0) {
tfiles.top = infiles.top + nfiles;
get = getnext;
@@ -140,10 +132,13 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
*/
if (c == BUFFEND && nelem == 0) {
bufsize *= 2;
- buffer = realloc(buffer, bufsize);
- if (!buffer)
+ tmpbuf = realloc(buffer, bufsize);
+ if (!tmpbuf)
err(2, "failed to realloc buffer");
- bufend = buffer + bufsize;
+ crec = (RECHEADER *)
+ (tmpbuf + ((u_char *)crec - buffer));
+ buffer = tmpbuf;
+ bufend = buffer + bufsize - 1;
continue;
}
if (c == BUFFEND || ntfiles || mfct) { /* push */
@@ -157,27 +152,6 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
mfct++;
/* reduce number of open files */
if (mfct == 16 ||(c == EOF && ntfiles)) {
- /*
- * Only copy extra incomplete
- * crec data if there is any.
- */
- int nodata = (bufend
- >= (u_char *)crec
- && bufend <= crec->data);
- size_t sz = 0;
-
- if (!nodata) {
- sz = bufend
- - crec->data;
- tmpbuf = malloc(sz);
- if (tmpbuf == NULL)
- errx(2, "cannot"
- " allocate"
- " memory");
- memmove(tmpbuf,
- crec->data, sz);
- }
-
fstack[tfiles.top + ntfiles].fp
= ftmp();
fmerge(0, mstart, mfct, geteasy,
@@ -185,12 +159,6 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
putrec, ftbl);
ntfiles++;
mfct = 0;
-
- if (!nodata) {
- memmove(crec->data,
- tmpbuf, sz);
- free(tmpbuf);
- }
}
} else {
fstack[tfiles.top + ntfiles].fp= ftmp();
@@ -216,7 +184,6 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
fmerge(0, tfiles, ntfiles, geteasy,
outfp, putline, ftbl);
break;
-
}
total = maxb = lastb = 0; /* find if one bin dominates */
for (i = 0; i < NBINS; i++)
diff --git a/usr.bin/sort/fsort.h b/usr.bin/sort/fsort.h
index d07e4010ae6..a2691563780 100644
--- a/usr.bin/sort/fsort.h
+++ b/usr.bin/sort/fsort.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.h,v 1.6 2006/10/28 21:14:29 naddy Exp $ */
+/* $OpenBSD: fsort.h,v 1.7 2006/10/29 18:40:34 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -42,7 +42,7 @@
#define MAXFCT 1000
#define MAXLLEN ((1 << min(POW-4, 16)) - 14)
-extern u_char **keylist, *buffer, *linebuf;
+extern u_char *buffer, *bufend, *linebuf;
extern size_t bufsize, linebuf_size;
/* temp files in the stack have a file descriptor, a largest bin (maxb)
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index e33298c0a21..311b1e4e894 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: msort.c,v 1.16 2006/10/28 21:14:29 naddy Exp $ */
+/* $OpenBSD: msort.c,v 1.17 2006/10/29 18:40:34 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -36,7 +36,7 @@
#if 0
static char sccsid[] = "@(#)msort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: msort.c,v 1.16 2006/10/28 21:14:29 naddy Exp $";
+static char rcsid[] = "$OpenBSD: msort.c,v 1.17 2006/10/29 18:40:34 millert Exp $";
#endif
#endif /* not lint */
@@ -87,14 +87,14 @@ fmerge(int binno, union f_handle files, int nfiles,
}
i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
- if (!buffer || i > BUFSIZE) {
- buffer = buffer ? realloc(buffer, i) : malloc(i);
+ if (i > bufsize) {
+ do {
+ bufsize *= 2;
+ } while (i > bufsize);
+ buffer = realloc(buffer, bufsize);
if (!buffer)
errx(2, "cannot allocate memory");
- if (!SINGL_FLD) {
- if ((linebuf = malloc(MAXLLEN)) == NULL)
- errx(2, "cannot allocate memory");
- }
+ bufend = buffer + bufsize - 1;
}
if (binno >= 0)
@@ -253,14 +253,14 @@ order(union f_handle infile,
int c;
RECHEADER *crec, *prec, *trec;
- if (!SINGL_FLD) {
- if ((linebuf = malloc(MAXLLEN)) == NULL)
- errx(2, "cannot allocate memory");
+ if (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER))) {
+ do {
+ bufsize *= 2;
+ } while (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER)));
+ if ((buffer = realloc(buffer, bufsize)) == NULL)
+ err(2, NULL);
+ bufend = buffer + bufsize - 1;
}
- buffer = malloc(2 * ALIGN((MAXLLEN + sizeof(TRECHEADER))));
- if (buffer == NULL)
- errx(2, "cannot allocate memory");
-
crec = (RECHEADER *) buffer;
crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(TRECHEADER));
prec = (RECHEADER *) crec_end;
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index f5da9ad174e..90ba230554a 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.c,v 1.30 2006/10/28 21:14:29 naddy Exp $ */
+/* $OpenBSD: sort.c,v 1.31 2006/10/29 18:40:34 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -42,7 +42,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: sort.c,v 1.30 2006/10/28 21:14:29 naddy Exp $";
+static char rcsid[] = "$OpenBSD: sort.c,v 1.31 2006/10/29 18:40:34 millert Exp $";
#endif
#endif /* not lint */
@@ -254,6 +254,14 @@ main(int argc, char *argv[])
else
get = makekey;
+ if ((buffer = malloc(bufsize)) == NULL)
+ err(2, NULL);
+ bufend = buffer + bufsize - 1;
+ if (!SINGL_FLD) {
+ if ((linebuf = malloc(linebuf_size)) == NULL)
+ err(2, NULL);
+ }
+
if (cflag) {
order(filelist, get, fldtab);
/* NOT REACHED */