=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/sort/Attic/fsort.c,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- src/usr.bin/sort/Attic/fsort.c 2004/09/14 22:57:58 1.12 +++ src/usr.bin/sort/Attic/fsort.c 2006/10/18 23:30:43 1.13 @@ -1,4 +1,4 @@ -/* $OpenBSD: fsort.c,v 1.12 2004/09/14 22:57:58 deraadt Exp $ */ +/* $OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 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.12 2004/09/14 22:57:58 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 millert Exp $"; #endif #endif /* not lint */ @@ -53,8 +53,8 @@ #include #include -u_char **keylist = 0, *buffer = 0, *linebuf = 0; -size_t bufsize, linebuf_size; +u_char *buffer = NULL, *linebuf = NULL; +size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN; struct tempfile fstack[MAXFCT]; extern char toutpath[]; #define FSORTMAX 4 @@ -64,8 +64,8 @@ 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 *bufend, *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,11 @@ 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"); - } - } bufend = buffer + bufsize; + if (keylist == NULL) { + if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) + err(2, NULL); + } if (binno >= 0) { tfiles.top = infiles.top + nfiles; get = getnext; @@ -140,9 +133,12 @@ */ 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"); + crec = (RECHEADER *) + (tmpbuf + ((u_char *)crec - buffer)); + buffer = tmpbuf; bufend = buffer + bufsize; continue; } @@ -157,27 +153,6 @@ 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 +160,6 @@ putrec, ftbl); ntfiles++; mfct = 0; - - if (!nodata) { - memmove(crec->data, - tmpbuf, sz); - free(tmpbuf); - } } } else { fstack[tfiles.top + ntfiles].fp= ftmp();