Annotation of src/usr.bin/sort/msort.c, Revision 1.13
1.13 ! deraadt 1: /* $OpenBSD: msort.c,v 1.12 2003/06/03 02:56:16 millert Exp $ */
1.1 millert 2:
3: /*-
4: * Copyright (c) 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Peter McIlroy.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.12 millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 millert 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #ifndef lint
36: #if 0
37: static char sccsid[] = "@(#)msort.c 8.1 (Berkeley) 6/6/93";
38: #else
1.13 ! deraadt 39: static char rcsid[] = "$OpenBSD: msort.c,v 1.12 2003/06/03 02:56:16 millert Exp $";
1.1 millert 40: #endif
41: #endif /* not lint */
42:
43: #include "sort.h"
44: #include "fsort.h"
45:
46: #include <stdlib.h>
47: #include <string.h>
48: #include <unistd.h>
49:
50: /* Subroutines using comparisons: merge sort and check order */
51: #define DELETE (1)
1.5 millert 52: #define LALIGN(n) ((n+(sizeof(long)-1)) & ~(sizeof(long)-1))
1.1 millert 53:
54: typedef struct mfile {
55: u_char *end;
56: short flno;
1.6 millert 57: RECHEADER rec[1];
1.1 millert 58: } MFILE;
59: typedef struct tmfile {
60: u_char *end;
61: short flno;
1.6 millert 62: TRECHEADER rec[1];
1.1 millert 63: } TMFILE;
64: u_char *wts, *wts1 = 0;
65: struct mfile *cfilebuf;
66:
1.11 millert 67: static int cmp(RECHEADER *, RECHEADER *);
68: static int insert(struct mfile **, struct mfile **, int, int);
1.1 millert 69:
70: void
71: fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
72: union f_handle files;
73: int binno, nfiles;
1.13 ! deraadt 74: int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
1.1 millert 75: FILE *outfp;
1.13 ! deraadt 76: void (*fput)(RECHEADER *, FILE *);
1.1 millert 77: struct field *ftbl;
78: {
79: FILE *tout;
80: int i, j, last;
1.6 millert 81: void (*put)(RECHEADER *, FILE *);
1.1 millert 82: struct tempfile *l_fstack;
83:
84: wts = ftbl->weights;
1.9 ericj 85: if (!UNIQUE && SINGL_FLD && (ftbl->flags & F))
1.1 millert 86: wts1 = (ftbl->flags & R) ? Rascii : ascii;
1.7 millert 87: if (!cfilebuf) {
1.1 millert 88: cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
1.7 millert 89: if (cfilebuf == NULL)
90: errx(2, "cannot allocate memory");
91: }
1.1 millert 92:
93: i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
94: if (!buffer || i > BUFSIZE) {
95: buffer = buffer ? realloc(buffer, i) : malloc(i);
96: if (!buffer)
1.7 millert 97: errx(2, "cannot allocate memory");
98: if (!SINGL_FLD) {
99: if ((linebuf = malloc(MAXLLEN)) == NULL)
100: errx(2, "cannot allocate memory");
101: }
1.1 millert 102: }
103:
104: if (binno >= 0)
105: l_fstack = fstack + files.top;
106: else
107: l_fstack = fstack;
1.4 millert 108:
1.1 millert 109: while (nfiles) {
110: put = putrec;
111: for (j = 0; j < nfiles; j += 16) {
112: if (nfiles <= 16) {
113: tout = outfp;
114: put = fput;
115: }
116: else
117: tout = ftmp();
118: last = min(16, nfiles - j);
119: if (binno < 0) {
120: for (i = 0; i < last; i++)
121: if (!(l_fstack[i+MAXFCT-1-16].fp =
122: fopen(files.names[j + i], "r")))
1.8 millert 123: err(2, "%s", files.names[j+i]);
1.1 millert 124: merge(MAXFCT-1-16, last, get, tout, put, ftbl);
1.4 millert 125: } else {
1.1 millert 126: for (i = 0; i< last; i++)
127: rewind(l_fstack[i+j].fp);
128: merge(files.top+j, last, get, tout, put, ftbl);
129: }
1.4 millert 130: if (nfiles > 16)
131: l_fstack[j/16].fp = tout;
1.1 millert 132: }
133: nfiles = (nfiles + 15) / 16;
134: if (nfiles == 1)
135: nfiles = 0;
136: if (binno < 0) {
137: binno = 0;
138: get = geteasy;
139: files.top = 0;
140: }
141: }
142: }
143:
144: void
145: merge(infl0, nfiles, get, outfp, put, ftbl)
146: int infl0, nfiles;
1.13 ! deraadt 147: int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
1.6 millert 148: void (*put)(RECHEADER *, FILE *);
1.1 millert 149: FILE *outfp;
150: struct field *ftbl;
151: {
152: int c, i, j;
153: union f_handle dummy = {0};
154: struct mfile *flist[16], *cfile;
1.2 millert 155:
1.1 millert 156: for (i = j = 0; i < nfiles; i++) {
157: cfile = (MFILE *) (buffer +
158: i * LALIGN(MAXLLEN + sizeof(TMFILE)));
159: cfile->flno = j + infl0;
160: cfile->end = cfile->rec->data + MAXLLEN;
161: for (c = 1; c == 1;) {
162: if (EOF == (c = get(j+infl0, dummy, nfiles,
163: cfile->rec, cfile->end, ftbl))) {
1.4 millert 164: i--;
165: nfiles--;
1.1 millert 166: break;
167: }
168: if (i)
169: c = insert(flist, &cfile, i, !DELETE);
170: else
171: flist[0] = cfile;
172: }
173: j++;
174: }
1.2 millert 175: if (nfiles > 0) {
176: cfile = cfilebuf;
177: cfile->flno = flist[0]->flno;
178: cfile->end = cfile->rec->data + MAXLLEN;
179: while (nfiles) {
180: for (c = 1; c == 1;) {
181: if (EOF == (c = get(cfile->flno, dummy, nfiles,
182: cfile->rec, cfile->end, ftbl))) {
183: put(flist[0]->rec, outfp);
184: memmove(flist, flist + 1,
185: sizeof(MFILE *) * (--nfiles));
186: cfile->flno = flist[0]->flno;
187: break;
188: }
189: if (!(c = insert(flist, &cfile, nfiles, DELETE)))
190: put(cfile->rec, outfp);
1.1 millert 191: }
1.2 millert 192: }
1.1 millert 193: }
194: }
195:
196: /*
197: * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
198: * otherwise just inserts *rec in flist.
1.4 millert 199: */
1.1 millert 200: static int
201: insert(flist, rec, ttop, delete)
202: struct mfile **flist, **rec;
203: int delete, ttop; /* delete = 0 or 1 */
204: {
1.9 ericj 205: struct mfile *tmprec;
206: int top, mid, bot = 0, cmpv = 1;
1.1 millert 207: tmprec = *rec;
208: top = ttop;
209: for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
210: cmpv = cmp(tmprec->rec, flist[mid]->rec);
211: if (cmpv < 0)
212: top = mid;
213: else if (cmpv > 0)
214: bot = mid;
215: else {
216: if (!UNIQUE)
217: bot = mid - 1;
218: break;
219: }
220: }
221: if (delete) {
222: if (UNIQUE) {
223: if (!bot && cmpv)
224: cmpv = cmp(tmprec->rec, flist[0]->rec);
225: if (!cmpv)
1.4 millert 226: return (1);
1.1 millert 227: }
228: tmprec = flist[0];
229: if (bot)
230: memmove(flist, flist+1, bot * sizeof(MFILE **));
231: flist[bot] = *rec;
232: *rec = tmprec;
233: (*rec)->flno = (*flist)->flno;
234: return (0);
235: }
236: else {
237: if (!bot && !(UNIQUE && !cmpv)) {
238: cmpv = cmp(tmprec->rec, flist[0]->rec);
239: if (cmpv < 0)
240: bot = -1;
241: }
242: if (UNIQUE && !cmpv)
243: return (1);
244: bot++;
245: memmove(flist + bot+1, flist + bot,
246: (ttop - bot) * sizeof(MFILE **));
247: flist[bot] = *rec;
248: return (0);
249: }
250: }
251:
252: /*
253: * check order on one file
254: */
255: void
256: order(infile, get, ftbl)
257: union f_handle infile;
1.13 ! deraadt 258: int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
1.1 millert 259: struct field *ftbl;
260: {
1.9 ericj 261: u_char *crec_end, *prec_end, *trec_end;
1.1 millert 262: int c;
1.6 millert 263: RECHEADER *crec, *prec, *trec;
1.1 millert 264:
1.7 millert 265: if (!SINGL_FLD) {
266: if ((linebuf = malloc(MAXLLEN)) == NULL)
267: errx(2, "cannot allocate memory");
268: }
1.10 art 269: buffer = malloc(2 * ALIGN((MAXLLEN + sizeof(TRECHEADER))));
1.7 millert 270: if (buffer == NULL)
271: errx(2, "cannot allocate memory");
1.10 art 272:
1.1 millert 273: crec = (RECHEADER *) buffer;
1.10 art 274: crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(TRECHEADER));
275: prec = (RECHEADER *) crec_end;
276: prec_end = ((char *)prec) + ALIGN(MAXLLEN + sizeof(TRECHEADER));
1.1 millert 277: wts = ftbl->weights;
1.9 ericj 278: if (SINGL_FLD && (ftbl->flags & F))
1.1 millert 279: wts1 = ftbl->flags & R ? Rascii : ascii;
280: else
281: wts1 = 0;
1.9 ericj 282: if (get(-1, infile, 1, prec, prec_end, ftbl) == 0)
283: while (get(-1, infile, 1, crec, crec_end, ftbl) == 0) {
1.4 millert 284: if (0 < (c = cmp(prec, crec))) {
285: crec->data[crec->length-1] = 0;
286: errx(1, "found disorder: %s",
287: crec->data+crec->offset);
288: }
289: if (UNIQUE && !c) {
290: crec->data[crec->length-1] = 0;
291: errx(1, "found non-uniqueness: %s",
292: crec->data+crec->offset);
293: }
1.9 ericj 294: /* Swap pointers so that this record is on place
295: * pointed to by prec and new record is read to place
296: * pointed to by crec.
297: */
1.4 millert 298: trec = prec;
299: prec = crec;
300: crec = trec;
1.9 ericj 301: trec_end = prec_end;
302: prec_end = crec_end;
303: crec_end = trec_end;
1.1 millert 304: }
305: exit(0);
306: }
307:
308: static int
309: cmp(rec1, rec2)
1.6 millert 310: RECHEADER *rec1, *rec2;
1.1 millert 311: {
1.9 ericj 312: int r;
313: u_char *pos1, *pos2, *end;
314: u_char *cwts;
1.1 millert 315: for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
316: pos1 = rec1->data;
317: pos2 = rec2->data;
318: if (!SINGL_FLD && UNIQUE)
319: end = pos1 + min(rec1->offset, rec2->offset);
320: else
321: end = pos1 + min(rec1->length, rec2->length);
322: for (; pos1 < end; ) {
323: if ((r = cwts[*pos1++] - cwts[*pos2++]))
324: return (r);
325: }
326: }
327: return (0);
328: }