1 /* $NetBSD: append.c,v 1.10 2001/02/19 20:50:17 jdolecek Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 __RCSID("$NetBSD: append.c,v 1.10 2001/02/19 20:50:17 jdolecek Exp $");
43 __SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
50 if ((n = cpos - ppos) > 1) { \
51 for (; ppos < cpos; ++ppos) \
55 sradixsort(ppos, n, wts1, REC_D); \
57 radixsort(ppos, n, wts1, REC_D); \
58 for (; ppos < cpos; ppos++) { \
59 prec = (const RECHEADER *) (*ppos - sizeof(TRECHEADER));\
62 } else put(prec, fp); \
66 * copy sorted lines to output; check for uniqueness
69 append(keylist, nelem, depth, fp, put, ftbl)
70 const u_char **keylist;
79 const u_char **cpos, **ppos, **lastkey;
80 const u_char *cend, *pend, *start;
81 const struct recheader *crec, *prec;
83 if (*keylist == '\0' && UNIQUE)
85 wts1 = wts = ftbl[0].weights;
86 if ((!UNIQUE) && SINGL_FLD) {
87 if ((ftbl[0].flags & F) && (ftbl[0].flags & R))
89 else if (ftbl[0].flags & F)
93 lastkey = keylist + nelem;
94 depth += sizeof(TRECHEADER);
95 if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
97 prec = (const RECHEADER *) (*ppos - depth);
100 for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
101 crec = (const RECHEADER *) (*cpos - depth);
102 if (crec->length == prec->length) {
104 * Set pend and cend so that trailing NUL and
105 * record separator is ignored.
107 pend = (const u_char *) &prec->data + prec->length - 2;
108 cend = (const u_char *) &crec->data + crec->length - 2;
109 for (start = *cpos; cend >= start; cend--) {
110 if (wts[*cend] != wts[*pend])
114 if (pend + 1 != *ppos) {
131 if (!UNIQUE) { OUTPUT; }
134 prec = (const RECHEADER *) (*ppos - depth);
136 for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
137 crec = (const RECHEADER *) (*cpos - depth);
138 if (crec->offset == prec->offset) {
140 * Set pend and cend so that trailing NUL and
141 * record separator is ignored.
143 pend = (const u_char *) &prec->data + prec->offset - 2;
144 cend = (const u_char *) &crec->data + crec->offset - 2;
145 for (start = *cpos; cend >= start; cend--) {
146 if (wts[*cend] != wts[*pend])
150 if (pend + 1 != *ppos) {
161 } else for (cpos = keylist; cpos < lastkey; cpos++) {
162 crec = (const RECHEADER *) (*cpos - depth);
168 * output the already sorted eol bin.
171 rd_append(binno, infl0, nfiles, outfp, buffer, bufend)
180 rec = (RECHEADER *) buffer;
181 if (!getnext(binno, infl0, NULL, nfiles,
182 (RECHEADER *) buffer, bufend, 0)) {
184 while (getnext(binno, infl0, NULL, nfiles, (RECHEADER *) buffer,
193 * append plain text--used after sorting the biggest bin.
203 while ((nread = fread(buffer, 1, 4096, b)) > 0)
204 EWRITE(buffer, 1, nread, a);