xref: /netbsd-src/usr.bin/sort/append.c (revision 2b8a0536177e8e4dedd09b11fdaeb6e7a990ce1a)
1*2b8a0536Sjoerg /*	$NetBSD: append.c,v 1.23 2009/11/06 18:34:22 joerg Exp $	*/
2f84513a7Sjdolecek 
3f84513a7Sjdolecek /*-
4f84513a7Sjdolecek  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5f84513a7Sjdolecek  * All rights reserved.
6f84513a7Sjdolecek  *
7f84513a7Sjdolecek  * This code is derived from software contributed to The NetBSD Foundation
8f84513a7Sjdolecek  * by Ben Harris and Jaromir Dolecek.
9f84513a7Sjdolecek  *
10f84513a7Sjdolecek  * Redistribution and use in source and binary forms, with or without
11f84513a7Sjdolecek  * modification, are permitted provided that the following conditions
12f84513a7Sjdolecek  * are met:
13f84513a7Sjdolecek  * 1. Redistributions of source code must retain the above copyright
14f84513a7Sjdolecek  *    notice, this list of conditions and the following disclaimer.
15f84513a7Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
16f84513a7Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
17f84513a7Sjdolecek  *    documentation and/or other materials provided with the distribution.
18f84513a7Sjdolecek  *
19f84513a7Sjdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f84513a7Sjdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f84513a7Sjdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f84513a7Sjdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f84513a7Sjdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f84513a7Sjdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f84513a7Sjdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f84513a7Sjdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f84513a7Sjdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f84513a7Sjdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f84513a7Sjdolecek  * POSSIBILITY OF SUCH DAMAGE.
30f84513a7Sjdolecek  */
316029888aSbjh21 
321d5d9b5bSbjh21 /*-
331d5d9b5bSbjh21  * Copyright (c) 1993
341d5d9b5bSbjh21  *	The Regents of the University of California.  All rights reserved.
351d5d9b5bSbjh21  *
361d5d9b5bSbjh21  * This code is derived from software contributed to Berkeley by
371d5d9b5bSbjh21  * Peter McIlroy.
381d5d9b5bSbjh21  *
391d5d9b5bSbjh21  * Redistribution and use in source and binary forms, with or without
401d5d9b5bSbjh21  * modification, are permitted provided that the following conditions
411d5d9b5bSbjh21  * are met:
421d5d9b5bSbjh21  * 1. Redistributions of source code must retain the above copyright
431d5d9b5bSbjh21  *    notice, this list of conditions and the following disclaimer.
441d5d9b5bSbjh21  * 2. Redistributions in binary form must reproduce the above copyright
451d5d9b5bSbjh21  *    notice, this list of conditions and the following disclaimer in the
461d5d9b5bSbjh21  *    documentation and/or other materials provided with the distribution.
4789aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
481d5d9b5bSbjh21  *    may be used to endorse or promote products derived from this software
491d5d9b5bSbjh21  *    without specific prior written permission.
501d5d9b5bSbjh21  *
511d5d9b5bSbjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
521d5d9b5bSbjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
531d5d9b5bSbjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
541d5d9b5bSbjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
551d5d9b5bSbjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
561d5d9b5bSbjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
571d5d9b5bSbjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
581d5d9b5bSbjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
591d5d9b5bSbjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
601d5d9b5bSbjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
611d5d9b5bSbjh21  * SUCH DAMAGE.
621d5d9b5bSbjh21  */
631d5d9b5bSbjh21 
641d5d9b5bSbjh21 #include "sort.h"
651d5d9b5bSbjh21 
66*2b8a0536Sjoerg __RCSID("$NetBSD: append.c,v 1.23 2009/11/06 18:34:22 joerg Exp $");
676029888aSbjh21 
681d5d9b5bSbjh21 #include <stdlib.h>
691d5d9b5bSbjh21 
701d5d9b5bSbjh21 /*
711310aa04Sdsl  * copy sorted lines to output
721310aa04Sdsl  * Ignore duplicates (marked with -ve keylen)
731d5d9b5bSbjh21  */
741d5d9b5bSbjh21 void
append(RECHEADER ** keylist,int nelem,FILE * fp,put_func_t put)751310aa04Sdsl append(RECHEADER **keylist, int nelem, FILE *fp, put_func_t put)
761d5d9b5bSbjh21 {
771310aa04Sdsl 	RECHEADER **cpos, **lastkey;
781310aa04Sdsl 	RECHEADER *crec;
79bf80c848Sdsl 
801d5d9b5bSbjh21 	lastkey = keylist + nelem;
811310aa04Sdsl 	if (REVERSE) {
821310aa04Sdsl 		for (cpos = lastkey; cpos-- > keylist;) {
832abdfb39Sdsl 			crec = *cpos;
841310aa04Sdsl 			if (crec->keylen >= 0)
851310aa04Sdsl 				put(crec, fp);
861d5d9b5bSbjh21 		}
871310aa04Sdsl 	} else {
881310aa04Sdsl 		for (cpos = keylist; cpos < lastkey; cpos++) {
892abdfb39Sdsl 			crec = *cpos;
901310aa04Sdsl 			if (crec->keylen >= 0)
911310aa04Sdsl 				put(crec, fp);
921d5d9b5bSbjh21 		}
937b4a02beSdsl 	}
941d5d9b5bSbjh21 }
95