summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/sort/fsort.c21
-rw-r--r--usr.bin/sort/fsort.h6
-rw-r--r--usr.bin/sort/msort.c44
-rw-r--r--usr.bin/sort/sort.c9
4 files changed, 35 insertions, 45 deletions
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c
index cd25ab559a4..d706dd44ab5 100644
--- a/usr.bin/sort/fsort.c
+++ b/usr.bin/sort/fsort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.c,v 1.17 2007/03/11 00:38:01 deraadt Exp $ */
+/* $OpenBSD: fsort.c,v 1.18 2007/03/13 17:33:58 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.17 2007/03/11 00:38:01 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: fsort.c,v 1.18 2007/03/13 17:33:58 millert Exp $";
#endif
#endif /* not lint */
@@ -53,10 +53,9 @@ static char rcsid[] = "$OpenBSD: fsort.c,v 1.17 2007/03/11 00:38:01 deraadt Exp
#include <stdlib.h>
#include <string.h>
-u_char *buffer = NULL, *bufend = NULL, *linebuf = NULL;
-size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN;
+u_char *linebuf;
+size_t linebuf_size = MAXLLEN;
struct tempfile fstack[MAXFCT];
-extern char toutpath[];
#define FSORTMAX 4
int PANIC = FSORTMAX;
@@ -64,8 +63,9 @@ void
fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp,
struct field *ftbl)
{
- u_char *weights, **keypos, *tmpbuf;
- static u_char **keylist;
+ u_char *weights, **keypos, *bufend, *tmpbuf;
+ static u_char *buffer, **keylist;
+ static size_t bufsize;
int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
int c, nelem;
long sizes[NBINS+1];
@@ -85,10 +85,13 @@ 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 (keylist == NULL) {
- if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
+ if (buffer == NULL) {
+ bufsize = BUFSIZE;
+ if ((buffer = malloc(bufsize)) == NULL ||
+ (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
err(2, NULL);
}
+ bufend = buffer + bufsize - 1;
if (binno >= 0) {
tfiles.top = infiles.top + nfiles;
get = getnext;
diff --git a/usr.bin/sort/fsort.h b/usr.bin/sort/fsort.h
index 6b85bf05e23..89913c6a199 100644
--- a/usr.bin/sort/fsort.h
+++ b/usr.bin/sort/fsort.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.h,v 1.9 2007/03/11 00:38:01 deraadt Exp $ */
+/* $OpenBSD: fsort.h,v 1.10 2007/03/13 17:33:58 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -42,8 +42,8 @@
#define MAXFCT 1000
#define MAXLLEN ((1 << min(POW-4, 16)) - 14)
-extern u_char *buffer, *bufend, *linebuf;
-extern size_t bufsize, linebuf_size;
+extern u_char *linebuf;
+extern size_t linebuf_size;
/* 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
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index 39407e25109..cfd223c4570 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: msort.c,v 1.19 2007/03/11 00:38:01 deraadt Exp $ */
+/* $OpenBSD: msort.c,v 1.20 2007/03/13 17:33:58 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.19 2007/03/11 00:38:01 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: msort.c,v 1.20 2007/03/13 17:33:58 millert Exp $";
#endif
#endif /* not lint */
@@ -56,13 +56,10 @@ typedef struct mfile {
short flno;
RECHEADER rec[1];
} MFILE;
-typedef struct tmfile {
- u_char *end;
- short flno;
- TRECHEADER rec[1];
-} TMFILE;
-u_char *wts, *wts1 = 0;
-struct mfile *cfilebuf;
+static u_char *wts, *wts1;
+static struct mfile *cfilebuf;
+static void *buffer;
+static size_t bufsize;
static int cmp(RECHEADER *, RECHEADER *);
static int insert(struct mfile **, struct mfile **, int, int);
@@ -80,21 +77,17 @@ fmerge(int binno, union f_handle files, int nfiles,
wts = ftbl->weights;
if (!UNIQUE && SINGL_FLD && (ftbl->flags & F))
wts1 = (ftbl->flags & R) ? Rascii : ascii;
- if (!cfilebuf) {
- cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
+ if (cfilebuf == NULL) {
+ cfilebuf = malloc(MAXLLEN + sizeof(MFILE));
if (cfilebuf == NULL)
errx(2, "cannot allocate memory");
}
- i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
+ i = min(16, nfiles) * LALIGN(MAXLLEN + sizeof(MFILE));
if (i > bufsize) {
- do {
- bufsize *= 2;
- } while (i > bufsize);
- buffer = realloc(buffer, bufsize);
- if (!buffer)
- errx(2, "cannot allocate memory");
- bufend = buffer + bufsize - 1;
+ bufsize = i;
+ if ((buffer = realloc(buffer, bufsize)) == NULL)
+ err(2, NULL);
}
if (binno >= 0)
@@ -148,7 +141,7 @@ merge(int infl0, int nfiles,
for (i = j = 0; i < nfiles; i++) {
cfile = (MFILE *) (buffer +
- i * LALIGN(MAXLLEN + sizeof(TMFILE)));
+ i * LALIGN(MAXLLEN + sizeof(MFILE)));
cfile->flno = j + infl0;
cfile->end = cfile->rec->data + MAXLLEN;
for (c = 1; c == 1;) {
@@ -253,18 +246,15 @@ order(union f_handle infile,
int c;
RECHEADER *crec, *prec, *trec;
- if (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER))) {
- do {
- bufsize *= 2;
- } while (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER)));
+ if (bufsize < 2 * ALIGN(MAXLLEN + sizeof(RECHEADER))) {
+ bufsize = 2 * ALIGN(MAXLLEN + sizeof(RECHEADER));
if ((buffer = realloc(buffer, bufsize)) == NULL)
err(2, NULL);
- bufend = buffer + bufsize - 1;
}
crec = (RECHEADER *) buffer;
- crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(TRECHEADER));
+ crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(RECHEADER));
prec = (RECHEADER *) crec_end;
- prec_end = ((char *)prec) + ALIGN(MAXLLEN + sizeof(TRECHEADER));
+ prec_end = ((char *)prec) + ALIGN(MAXLLEN + sizeof(RECHEADER));
wts = ftbl->weights;
if (SINGL_FLD && (ftbl->flags & F))
wts1 = ftbl->flags & R ? Rascii : ascii;
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 48f869b63e4..5205c2304d7 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.c,v 1.33 2007/03/11 00:38:01 deraadt Exp $ */
+/* $OpenBSD: sort.c,v 1.34 2007/03/13 17:33:58 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.33 2007/03/11 00:38:01 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: sort.c,v 1.34 2007/03/13 17:33:58 millert Exp $";
#endif
#endif /* not lint */
@@ -254,10 +254,7 @@ main(int argc, char *argv[])
else
get = makekey;
- if ((buffer = malloc(bufsize)) == NULL)
- err(2, NULL);
- bufend = buffer + bufsize - 1;
- if (!SINGL_FLD) {
+ if (!SINGL_FLD) {
if ((linebuf = malloc(linebuf_size)) == NULL)
err(2, NULL);
}