Home | History | Annotate | Line # | Download | only in sort
msort.c revision 1.16
      1  1.16    itojun /*	$NetBSD: msort.c,v 1.16 2003/10/18 03:03:20 itojun Exp $	*/
      2  1.14  jdolecek 
      3  1.14  jdolecek /*-
      4  1.14  jdolecek  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
      5  1.14  jdolecek  * All rights reserved.
      6  1.14  jdolecek  *
      7  1.14  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8  1.14  jdolecek  * by Ben Harris and Jaromir Dolecek.
      9  1.14  jdolecek  *
     10  1.14  jdolecek  * Redistribution and use in source and binary forms, with or without
     11  1.14  jdolecek  * modification, are permitted provided that the following conditions
     12  1.14  jdolecek  * are met:
     13  1.14  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14  1.14  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15  1.14  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.14  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17  1.14  jdolecek  *    documentation and/or other materials provided with the distribution.
     18  1.14  jdolecek  * 3. All advertising materials mentioning features or use of this software
     19  1.14  jdolecek  *    must display the following acknowledgement:
     20  1.14  jdolecek  *        This product includes software developed by the NetBSD
     21  1.14  jdolecek  *        Foundation, Inc. and its contributors.
     22  1.14  jdolecek  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.14  jdolecek  *    contributors may be used to endorse or promote products derived
     24  1.14  jdolecek  *    from this software without specific prior written permission.
     25  1.14  jdolecek  *
     26  1.14  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.14  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.14  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.14  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.14  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.14  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.14  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.14  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.14  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.14  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.14  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     37  1.14  jdolecek  */
     38   1.2     bjh21 
     39   1.1     bjh21 /*-
     40   1.1     bjh21  * Copyright (c) 1993
     41   1.1     bjh21  *	The Regents of the University of California.  All rights reserved.
     42   1.1     bjh21  *
     43   1.1     bjh21  * This code is derived from software contributed to Berkeley by
     44   1.1     bjh21  * Peter McIlroy.
     45   1.1     bjh21  *
     46   1.1     bjh21  * Redistribution and use in source and binary forms, with or without
     47   1.1     bjh21  * modification, are permitted provided that the following conditions
     48   1.1     bjh21  * are met:
     49   1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     50   1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     51   1.1     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     52   1.1     bjh21  *    notice, this list of conditions and the following disclaimer in the
     53   1.1     bjh21  *    documentation and/or other materials provided with the distribution.
     54  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     55   1.1     bjh21  *    may be used to endorse or promote products derived from this software
     56   1.1     bjh21  *    without specific prior written permission.
     57   1.1     bjh21  *
     58   1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1     bjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1     bjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1     bjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1     bjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1     bjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1     bjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1     bjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1     bjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1     bjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1     bjh21  * SUCH DAMAGE.
     69   1.1     bjh21  */
     70   1.1     bjh21 
     71   1.2     bjh21 #include "sort.h"
     72   1.2     bjh21 #include "fsort.h"
     73   1.2     bjh21 
     74   1.1     bjh21 #ifndef lint
     75  1.16    itojun __RCSID("$NetBSD: msort.c,v 1.16 2003/10/18 03:03:20 itojun Exp $");
     76   1.2     bjh21 __SCCSID("@(#)msort.c	8.1 (Berkeley) 6/6/93");
     77   1.1     bjh21 #endif /* not lint */
     78   1.1     bjh21 
     79   1.1     bjh21 #include <stdlib.h>
     80   1.1     bjh21 #include <string.h>
     81   1.1     bjh21 #include <unistd.h>
     82   1.1     bjh21 
     83   1.1     bjh21 /* Subroutines using comparisons: merge sort and check order */
     84   1.1     bjh21 #define DELETE (1)
     85   1.8  jdolecek 
     86   1.1     bjh21 typedef struct mfile {
     87   1.1     bjh21 	u_char *end;
     88   1.1     bjh21 	short flno;
     89   1.1     bjh21 	struct recheader rec[1];
     90   1.1     bjh21 } MFILE;
     91   1.8  jdolecek 
     92   1.9  jdolecek static u_char *wts, *wts1 = NULL;
     93   1.1     bjh21 
     94  1.10  jdolecek static int cmp __P((RECHEADER *, RECHEADER *));
     95   1.1     bjh21 static int insert __P((struct mfile **, struct mfile **, int, int));
     96  1.11  jdolecek static void merge(int, int, get_func_t, FILE *, put_func_t, struct field *);
     97   1.1     bjh21 
     98   1.1     bjh21 void
     99   1.7  jdolecek fmerge(binno, top, filelist, nfiles, get, outfp, fput, ftbl)
    100   1.7  jdolecek 	int binno, top;
    101   1.7  jdolecek 	struct filelist *filelist;
    102   1.7  jdolecek 	int nfiles;
    103   1.7  jdolecek 	get_func_t get;
    104   1.3     bjh21 	FILE *outfp;
    105   1.7  jdolecek 	put_func_t fput;
    106   1.1     bjh21 	struct field *ftbl;
    107   1.1     bjh21 {
    108   1.1     bjh21 	FILE *tout;
    109   1.1     bjh21 	int i, j, last;
    110   1.7  jdolecek 	put_func_t put;
    111   1.1     bjh21 	struct tempfile *l_fstack;
    112   1.1     bjh21 
    113   1.1     bjh21 	wts = ftbl->weights;
    114   1.1     bjh21 	if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
    115   1.1     bjh21 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
    116   1.1     bjh21 
    117   1.9  jdolecek 	if (!buffer) {
    118   1.9  jdolecek 		buffer = malloc(bufsize);
    119   1.1     bjh21 		if (!buffer)
    120  1.12  jdolecek 			err(2, "fmerge(): malloc");
    121   1.9  jdolecek 
    122   1.5  jdolecek 		if (!linebuf && !SINGL_FLD) {
    123   1.5  jdolecek 			linebuf_size = DEFLLEN;
    124   1.5  jdolecek 			linebuf = malloc(linebuf_size);
    125   1.5  jdolecek 		}
    126   1.1     bjh21 	}
    127   1.1     bjh21 
    128   1.1     bjh21 	if (binno >= 0)
    129   1.7  jdolecek 		l_fstack = fstack + top;
    130   1.1     bjh21 	else
    131   1.1     bjh21 		l_fstack = fstack;
    132  1.10  jdolecek 
    133   1.1     bjh21 	while (nfiles) {
    134   1.1     bjh21 		put = putrec;
    135   1.8  jdolecek 		for (j = 0; j < nfiles; j += MERGE_FNUM) {
    136   1.8  jdolecek 			if (nfiles <= MERGE_FNUM) {
    137   1.3     bjh21 				tout = outfp;
    138   1.1     bjh21 				put = fput;
    139   1.1     bjh21 			}
    140   1.1     bjh21 			else
    141   1.1     bjh21 				tout = ftmp();
    142   1.8  jdolecek 			last = min(MERGE_FNUM, nfiles - j);
    143   1.1     bjh21 			if (binno < 0) {
    144  1.10  jdolecek 				for (i = 0; i < last; i++)
    145  1.10  jdolecek 					if (!(l_fstack[i+MAXFCT-1-MERGE_FNUM].fp =
    146  1.10  jdolecek 					    fopen(filelist->names[j+i], "r")))
    147   1.7  jdolecek 						err(2, "%s",
    148   1.7  jdolecek 							filelist->names[j+i]);
    149   1.8  jdolecek 				merge(MAXFCT-1-MERGE_FNUM, last, get, tout, put, ftbl);
    150  1.10  jdolecek 			} else {
    151   1.1     bjh21 				for (i = 0; i< last; i++)
    152   1.3     bjh21 					rewind(l_fstack[i+j].fp);
    153   1.7  jdolecek 				merge(top+j, last, get, tout, put, ftbl);
    154   1.1     bjh21 			}
    155   1.8  jdolecek 			if (nfiles > MERGE_FNUM)
    156   1.8  jdolecek 				l_fstack[j/MERGE_FNUM].fp = tout;
    157   1.1     bjh21 		}
    158   1.8  jdolecek 		nfiles = (nfiles + (MERGE_FNUM - 1)) / MERGE_FNUM;
    159   1.1     bjh21 		if (nfiles == 1)
    160   1.1     bjh21 			nfiles = 0;
    161   1.1     bjh21 		if (binno < 0) {
    162   1.1     bjh21 			binno = 0;
    163   1.1     bjh21 			get = geteasy;
    164   1.7  jdolecek 			top = 0;
    165   1.1     bjh21 		}
    166   1.1     bjh21 	}
    167   1.1     bjh21 }
    168   1.1     bjh21 
    169  1.11  jdolecek static void
    170   1.3     bjh21 merge(infl0, nfiles, get, outfp, put, ftbl)
    171   1.1     bjh21 	int infl0, nfiles;
    172   1.7  jdolecek 	get_func_t get;
    173   1.7  jdolecek 	put_func_t put;
    174   1.3     bjh21 	FILE *outfp;
    175   1.1     bjh21 	struct field *ftbl;
    176   1.1     bjh21 {
    177   1.9  jdolecek 	int c, i, j, nf = nfiles;
    178  1.12  jdolecek 	struct mfile *flistb[MERGE_FNUM], **flist = flistb, *cfile;
    179   1.9  jdolecek 	size_t availsz = bufsize;
    180  1.16    itojun 	static void *bufs[MERGE_FNUM + 1];
    181  1.16    itojun 	static size_t bufs_sz[MERGE_FNUM + 1];
    182   1.9  jdolecek 
    183   1.9  jdolecek 	/*
    184   1.9  jdolecek 	 * We need nfiles + 1 buffers. One is 'buffer', the
    185   1.9  jdolecek 	 * rest needs to be allocated.
    186   1.9  jdolecek 	 */
    187   1.9  jdolecek 	bufs[0] = buffer;
    188   1.9  jdolecek 	bufs_sz[0] = bufsize;
    189  1.16    itojun 	for (i = 1; i < nfiles + 1; i++) {
    190   1.9  jdolecek 		if (bufs[i])
    191   1.9  jdolecek 			continue;
    192   1.9  jdolecek 
    193   1.9  jdolecek 		bufs[i] = malloc(DEFLLEN);
    194   1.9  jdolecek 		if (!bufs[i])
    195  1.12  jdolecek 			err(2, "merge: malloc");
    196   1.9  jdolecek 		bufs_sz[i] = DEFLLEN;
    197   1.9  jdolecek 	}
    198   1.8  jdolecek 
    199  1.11  jdolecek 	for (i = j = 0; i < nfiles; i++, j++) {
    200   1.9  jdolecek 		cfile = (struct mfile *) bufs[j];
    201   1.9  jdolecek 		cfile->flno = infl0 + j;
    202   1.9  jdolecek 		cfile->end = (u_char *) bufs[j] + bufs_sz[j];
    203   1.1     bjh21 		for (c = 1; c == 1;) {
    204   1.9  jdolecek 			if (EOF == (c = get(cfile->flno, 0, NULL, nfiles,
    205   1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    206   1.1     bjh21 				--i;
    207   1.1     bjh21 				--nfiles;
    208   1.1     bjh21 				break;
    209   1.1     bjh21 			}
    210   1.9  jdolecek 
    211   1.9  jdolecek 			if (c == BUFFEND) {
    212  1.15    itojun 				cfile = realloc(bufs[j], bufs_sz[j]);
    213   1.9  jdolecek 				if (!cfile)
    214  1.12  jdolecek 					err(2, "merge: realloc");
    215   1.9  jdolecek 
    216  1.12  jdolecek 				bufs[j] = (void *) cfile;
    217  1.15    itojun 				bufs_sz[j] *= 2;
    218   1.9  jdolecek 				cfile->end = (u_char *)cfile + bufs_sz[j];
    219   1.9  jdolecek 
    220   1.9  jdolecek 				c = 1;
    221   1.9  jdolecek 				continue;
    222   1.9  jdolecek 			}
    223   1.9  jdolecek 
    224   1.1     bjh21 			if (i)
    225   1.1     bjh21 				c = insert(flist, &cfile, i, !DELETE);
    226   1.1     bjh21 			else
    227   1.1     bjh21 				flist[0] = cfile;
    228   1.1     bjh21 		}
    229   1.1     bjh21 	}
    230   1.9  jdolecek 
    231   1.9  jdolecek 	cfile = (struct mfile *) bufs[nf];
    232   1.1     bjh21 	cfile->flno = flist[0]->flno;
    233   1.9  jdolecek 	cfile->end = (u_char *) cfile + bufs_sz[nf];
    234   1.1     bjh21 	while (nfiles) {
    235   1.1     bjh21 		for (c = 1; c == 1;) {
    236   1.9  jdolecek 			if (EOF == (c = get(cfile->flno, 0, NULL, nfiles,
    237   1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    238   1.3     bjh21 				put(flist[0]->rec, outfp);
    239  1.12  jdolecek 				if (--nfiles > 0) {
    240  1.12  jdolecek 					flist++;
    241  1.12  jdolecek 					cfile->flno = flist[0]->flno;
    242  1.12  jdolecek 				}
    243   1.1     bjh21 				break;
    244   1.1     bjh21 			}
    245   1.9  jdolecek 			if (c == BUFFEND) {
    246   1.9  jdolecek 				char *oldbuf = (char *) cfile;
    247   1.9  jdolecek 				availsz = (char *) cfile->end - oldbuf;
    248   1.9  jdolecek 				availsz *= 2;
    249   1.9  jdolecek 				cfile = realloc(oldbuf, availsz);
    250  1.12  jdolecek 				if (!cfile)
    251  1.12  jdolecek 					err(2, "merge: realloc");
    252  1.12  jdolecek 
    253  1.16    itojun 				for (i = 0; i < nf + 1; i++) {
    254   1.9  jdolecek 					if (bufs[i] == oldbuf) {
    255   1.9  jdolecek 						bufs[i] = (char *)cfile;
    256   1.9  jdolecek 						bufs_sz[i] = availsz;
    257   1.9  jdolecek 						break;
    258   1.9  jdolecek 					}
    259   1.9  jdolecek 				}
    260   1.9  jdolecek 
    261   1.9  jdolecek 				cfile->end = (u_char *)cfile + availsz;
    262   1.9  jdolecek 				c = 1;
    263   1.9  jdolecek 				continue;
    264   1.9  jdolecek 			}
    265   1.9  jdolecek 
    266   1.1     bjh21 			if (!(c = insert(flist, &cfile, nfiles, DELETE)))
    267   1.3     bjh21 				put(cfile->rec, outfp);
    268   1.1     bjh21 		}
    269   1.1     bjh21 	}
    270   1.9  jdolecek 
    271   1.9  jdolecek 	if (bufs_sz[0] > bufsize) {
    272   1.9  jdolecek 		buffer = bufs[0];
    273   1.9  jdolecek 		bufsize = bufs_sz[0];
    274   1.9  jdolecek 	}
    275   1.1     bjh21 }
    276   1.1     bjh21 
    277   1.1     bjh21 /*
    278   1.1     bjh21  * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
    279   1.1     bjh21  * otherwise just inserts *rec in flist.
    280  1.10  jdolecek  */
    281   1.1     bjh21 static int
    282   1.1     bjh21 insert(flist, rec, ttop, delete)
    283   1.1     bjh21 	struct mfile **flist, **rec;
    284   1.1     bjh21 	int delete, ttop;			/* delete = 0 or 1 */
    285   1.1     bjh21 {
    286   1.8  jdolecek 	struct mfile *tmprec = *rec;
    287   1.8  jdolecek 	int mid, top = ttop, bot = 0, cmpv = 1;
    288   1.8  jdolecek 
    289  1.16    itojun 	for (mid = top / 2; bot + 1 != top; mid = (bot + top) / 2) {
    290   1.1     bjh21 		cmpv = cmp(tmprec->rec, flist[mid]->rec);
    291   1.1     bjh21 		if (cmpv < 0)
    292   1.1     bjh21 			top = mid;
    293   1.1     bjh21 		else if (cmpv > 0)
    294   1.1     bjh21 			bot = mid;
    295   1.1     bjh21 		else {
    296   1.8  jdolecek 			if (UNIQUE)
    297   1.8  jdolecek 				break;
    298   1.8  jdolecek 
    299   1.8  jdolecek 			if (stable_sort) {
    300   1.8  jdolecek 				/*
    301   1.8  jdolecek 				 * Apply sort by fileno, to give priority
    302   1.8  jdolecek 				 * to earlier specified files, hence providing
    303   1.8  jdolecek 				 * more stable sort.
    304   1.8  jdolecek 				 * If fileno is same, the new record should
    305   1.8  jdolecek 				 * be put _after_ the previous entry.
    306   1.8  jdolecek 				 */
    307   1.8  jdolecek 				cmpv = tmprec->flno - flist[mid]->flno;
    308   1.8  jdolecek 				if (cmpv >= 0)
    309   1.8  jdolecek 					bot = mid;
    310   1.8  jdolecek 				else /* cmpv == 0 */
    311   1.8  jdolecek 					bot = mid - 1;
    312   1.8  jdolecek 			} else {
    313   1.8  jdolecek 				/* non-stable sort */
    314   1.1     bjh21 				bot = mid - 1;
    315   1.8  jdolecek 			}
    316   1.8  jdolecek 
    317   1.1     bjh21 			break;
    318   1.1     bjh21 		}
    319   1.1     bjh21 	}
    320   1.8  jdolecek 
    321   1.1     bjh21 	if (delete) {
    322   1.1     bjh21 		if (UNIQUE) {
    323   1.1     bjh21 			if (!bot && cmpv)
    324   1.1     bjh21 				cmpv = cmp(tmprec->rec, flist[0]->rec);
    325   1.1     bjh21 			if (!cmpv)
    326  1.10  jdolecek 				return (1);
    327   1.1     bjh21 		}
    328   1.1     bjh21 		tmprec = flist[0];
    329   1.1     bjh21 		if (bot)
    330  1.16    itojun 			memmove(flist, flist + 1, bot * sizeof(MFILE **));
    331   1.1     bjh21 		flist[bot] = *rec;
    332   1.1     bjh21 		*rec = tmprec;
    333   1.8  jdolecek 		(*rec)->flno = flist[0]->flno;
    334   1.1     bjh21 		return (0);
    335   1.8  jdolecek 	} else {
    336   1.1     bjh21 		if (!bot && !(UNIQUE && !cmpv)) {
    337   1.1     bjh21 			cmpv = cmp(tmprec->rec, flist[0]->rec);
    338   1.1     bjh21 			if (cmpv < 0)
    339   1.1     bjh21 				bot = -1;
    340   1.1     bjh21 		}
    341   1.1     bjh21 		if (UNIQUE && !cmpv)
    342   1.1     bjh21 			return (1);
    343   1.1     bjh21 		bot++;
    344  1.16    itojun 		memmove(flist + bot + 1, flist + bot,
    345   1.1     bjh21 		    (ttop - bot) * sizeof(MFILE **));
    346   1.1     bjh21 		flist[bot] = *rec;
    347   1.1     bjh21 		return (0);
    348   1.1     bjh21 	}
    349   1.1     bjh21 }
    350   1.1     bjh21 
    351   1.1     bjh21 /*
    352   1.1     bjh21  * check order on one file
    353   1.1     bjh21  */
    354   1.1     bjh21 void
    355   1.7  jdolecek order(filelist, get, ftbl)
    356   1.7  jdolecek 	struct filelist *filelist;
    357   1.7  jdolecek 	get_func_t get;
    358   1.1     bjh21 	struct field *ftbl;
    359   1.1     bjh21 {
    360   1.6  jdolecek 	u_char *crec_end, *prec_end, *trec_end;
    361   1.1     bjh21 	int c;
    362  1.10  jdolecek 	RECHEADER *crec, *prec, *trec;
    363   1.1     bjh21 
    364   1.1     bjh21 	if (!SINGL_FLD)
    365   1.5  jdolecek 		linebuf = malloc(DEFLLEN);
    366   1.5  jdolecek 	buffer = malloc(2 * (DEFLLEN + sizeof(TRECHEADER)));
    367   1.1     bjh21 	crec = (RECHEADER *) buffer;
    368   1.6  jdolecek 	crec_end = buffer + DEFLLEN + sizeof(TRECHEADER);
    369   1.5  jdolecek 	prec = (RECHEADER *) (buffer + DEFLLEN + sizeof(TRECHEADER));
    370   1.6  jdolecek 	prec_end = buffer + 2*(DEFLLEN + sizeof(TRECHEADER));
    371   1.1     bjh21 	wts = ftbl->weights;
    372   1.6  jdolecek 	if (SINGL_FLD && (ftbl->flags & F))
    373   1.8  jdolecek 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
    374   1.1     bjh21 	else
    375   1.9  jdolecek 		wts1 = NULL;
    376   1.7  jdolecek 	if (0 == get(-1, 0, filelist, 1, prec, prec_end, ftbl))
    377   1.7  jdolecek 	while (0 == get(-1, 0, filelist, 1, crec, crec_end, ftbl)) {
    378   1.1     bjh21 		if (0 < (c = cmp(prec, crec))) {
    379   1.1     bjh21 			crec->data[crec->length-1] = 0;
    380   1.1     bjh21 			errx(1, "found disorder: %s", crec->data+crec->offset);
    381   1.1     bjh21 		}
    382   1.1     bjh21 		if (UNIQUE && !c) {
    383   1.1     bjh21 			crec->data[crec->length-1] = 0;
    384   1.1     bjh21 			errx(1, "found non-uniqueness: %s",
    385   1.1     bjh21 			    crec->data+crec->offset);
    386   1.1     bjh21 		}
    387   1.6  jdolecek 		/*
    388   1.6  jdolecek 		 * Swap pointers so that this record is on place pointed
    389   1.6  jdolecek 		 * to by prec and new record is read to place pointed to by
    390   1.6  jdolecek 		 * crec.
    391   1.6  jdolecek 		 */
    392   1.1     bjh21 		trec = prec;
    393   1.1     bjh21 		prec = crec;
    394   1.1     bjh21 		crec = trec;
    395   1.6  jdolecek 		trec_end = prec_end;
    396   1.6  jdolecek 		prec_end = crec_end;
    397   1.6  jdolecek 		crec_end = trec_end;
    398   1.1     bjh21 	}
    399   1.1     bjh21 	exit(0);
    400   1.1     bjh21 }
    401   1.1     bjh21 
    402   1.1     bjh21 static int
    403   1.1     bjh21 cmp(rec1, rec2)
    404  1.10  jdolecek 	RECHEADER *rec1, *rec2;
    405   1.1     bjh21 {
    406   1.4  jdolecek 	int r;
    407   1.4  jdolecek 	u_char *pos1, *pos2, *end;
    408   1.4  jdolecek 	u_char *cwts;
    409   1.9  jdolecek 	for (cwts = wts; cwts; cwts = (cwts == wts1 ? NULL : wts1)) {
    410   1.1     bjh21 		pos1 = rec1->data;
    411   1.1     bjh21 		pos2 = rec2->data;
    412   1.8  jdolecek 		if (!SINGL_FLD && (UNIQUE || stable_sort))
    413   1.1     bjh21 			end = pos1 + min(rec1->offset, rec2->offset);
    414   1.1     bjh21 		else
    415   1.1     bjh21 			end = pos1 + min(rec1->length, rec2->length);
    416   1.8  jdolecek 
    417   1.1     bjh21 		for (; pos1 < end; ) {
    418   1.2     bjh21 			if ((r = cwts[*pos1++] - cwts[*pos2++]))
    419   1.1     bjh21 				return (r);
    420   1.1     bjh21 		}
    421   1.1     bjh21 	}
    422   1.1     bjh21 	return (0);
    423   1.1     bjh21 }
    424