1*0fbbaa43SLionel Sambuc /* $NetBSD: append.c,v 1.23 2009/11/06 18:34:22 joerg Exp $ */
2*0fbbaa43SLionel Sambuc
3*0fbbaa43SLionel Sambuc /*-
4*0fbbaa43SLionel Sambuc * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5*0fbbaa43SLionel Sambuc * All rights reserved.
6*0fbbaa43SLionel Sambuc *
7*0fbbaa43SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0fbbaa43SLionel Sambuc * by Ben Harris and Jaromir Dolecek.
9*0fbbaa43SLionel Sambuc *
10*0fbbaa43SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0fbbaa43SLionel Sambuc * modification, are permitted provided that the following conditions
12*0fbbaa43SLionel Sambuc * are met:
13*0fbbaa43SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0fbbaa43SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0fbbaa43SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0fbbaa43SLionel Sambuc *
19*0fbbaa43SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0fbbaa43SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0fbbaa43SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0fbbaa43SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0fbbaa43SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0fbbaa43SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0fbbaa43SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0fbbaa43SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0fbbaa43SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0fbbaa43SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0fbbaa43SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*0fbbaa43SLionel Sambuc */
31*0fbbaa43SLionel Sambuc
32*0fbbaa43SLionel Sambuc /*-
33*0fbbaa43SLionel Sambuc * Copyright (c) 1993
34*0fbbaa43SLionel Sambuc * The Regents of the University of California. All rights reserved.
35*0fbbaa43SLionel Sambuc *
36*0fbbaa43SLionel Sambuc * This code is derived from software contributed to Berkeley by
37*0fbbaa43SLionel Sambuc * Peter McIlroy.
38*0fbbaa43SLionel Sambuc *
39*0fbbaa43SLionel Sambuc * Redistribution and use in source and binary forms, with or without
40*0fbbaa43SLionel Sambuc * modification, are permitted provided that the following conditions
41*0fbbaa43SLionel Sambuc * are met:
42*0fbbaa43SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
43*0fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer.
44*0fbbaa43SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
45*0fbbaa43SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
46*0fbbaa43SLionel Sambuc * documentation and/or other materials provided with the distribution.
47*0fbbaa43SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
48*0fbbaa43SLionel Sambuc * may be used to endorse or promote products derived from this software
49*0fbbaa43SLionel Sambuc * without specific prior written permission.
50*0fbbaa43SLionel Sambuc *
51*0fbbaa43SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52*0fbbaa43SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*0fbbaa43SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*0fbbaa43SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55*0fbbaa43SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*0fbbaa43SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*0fbbaa43SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*0fbbaa43SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*0fbbaa43SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*0fbbaa43SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*0fbbaa43SLionel Sambuc * SUCH DAMAGE.
62*0fbbaa43SLionel Sambuc */
63*0fbbaa43SLionel Sambuc
64*0fbbaa43SLionel Sambuc #include "sort.h"
65*0fbbaa43SLionel Sambuc
66*0fbbaa43SLionel Sambuc __RCSID("$NetBSD: append.c,v 1.23 2009/11/06 18:34:22 joerg Exp $");
67*0fbbaa43SLionel Sambuc
68*0fbbaa43SLionel Sambuc #include <stdlib.h>
69*0fbbaa43SLionel Sambuc
70*0fbbaa43SLionel Sambuc /*
71*0fbbaa43SLionel Sambuc * copy sorted lines to output
72*0fbbaa43SLionel Sambuc * Ignore duplicates (marked with -ve keylen)
73*0fbbaa43SLionel Sambuc */
74*0fbbaa43SLionel Sambuc void
append(RECHEADER ** keylist,int nelem,FILE * fp,put_func_t put)75*0fbbaa43SLionel Sambuc append(RECHEADER **keylist, int nelem, FILE *fp, put_func_t put)
76*0fbbaa43SLionel Sambuc {
77*0fbbaa43SLionel Sambuc RECHEADER **cpos, **lastkey;
78*0fbbaa43SLionel Sambuc RECHEADER *crec;
79*0fbbaa43SLionel Sambuc
80*0fbbaa43SLionel Sambuc lastkey = keylist + nelem;
81*0fbbaa43SLionel Sambuc if (REVERSE) {
82*0fbbaa43SLionel Sambuc for (cpos = lastkey; cpos-- > keylist;) {
83*0fbbaa43SLionel Sambuc crec = *cpos;
84*0fbbaa43SLionel Sambuc if (crec->keylen >= 0)
85*0fbbaa43SLionel Sambuc put(crec, fp);
86*0fbbaa43SLionel Sambuc }
87*0fbbaa43SLionel Sambuc } else {
88*0fbbaa43SLionel Sambuc for (cpos = keylist; cpos < lastkey; cpos++) {
89*0fbbaa43SLionel Sambuc crec = *cpos;
90*0fbbaa43SLionel Sambuc if (crec->keylen >= 0)
91*0fbbaa43SLionel Sambuc put(crec, fp);
92*0fbbaa43SLionel Sambuc }
93*0fbbaa43SLionel Sambuc }
94*0fbbaa43SLionel Sambuc }
95