xref: /minix3/usr.bin/make/lst.lib/lstConcat.c (revision 2e2caf591948f38f31c4e679c6277d2cb999cee7)
1*2e2caf59SThomas Veerman /*	$NetBSD: lstConcat.c,v 1.16 2008/12/13 15:19:29 dsl Exp $	*/
2*2e2caf59SThomas Veerman 
3*2e2caf59SThomas Veerman /*
4*2e2caf59SThomas Veerman  * Copyright (c) 1988, 1989, 1990, 1993
5*2e2caf59SThomas Veerman  *	The Regents of the University of California.  All rights reserved.
6*2e2caf59SThomas Veerman  *
7*2e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
8*2e2caf59SThomas Veerman  * Adam de Boor.
9*2e2caf59SThomas Veerman  *
10*2e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
11*2e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
12*2e2caf59SThomas Veerman  * are met:
13*2e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
14*2e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
15*2e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
16*2e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
17*2e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
18*2e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
19*2e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
20*2e2caf59SThomas Veerman  *    without specific prior written permission.
21*2e2caf59SThomas Veerman  *
22*2e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*2e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*2e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*2e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*2e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*2e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*2e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*2e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*2e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*2e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*2e2caf59SThomas Veerman  * SUCH DAMAGE.
33*2e2caf59SThomas Veerman  */
34*2e2caf59SThomas Veerman 
35*2e2caf59SThomas Veerman #ifndef MAKE_NATIVE
36*2e2caf59SThomas Veerman static char rcsid[] = "$NetBSD: lstConcat.c,v 1.16 2008/12/13 15:19:29 dsl Exp $";
37*2e2caf59SThomas Veerman #else
38*2e2caf59SThomas Veerman #include <sys/cdefs.h>
39*2e2caf59SThomas Veerman #ifndef lint
40*2e2caf59SThomas Veerman #if 0
41*2e2caf59SThomas Veerman static char sccsid[] = "@(#)lstConcat.c	8.1 (Berkeley) 6/6/93";
42*2e2caf59SThomas Veerman #else
43*2e2caf59SThomas Veerman __RCSID("$NetBSD: lstConcat.c,v 1.16 2008/12/13 15:19:29 dsl Exp $");
44*2e2caf59SThomas Veerman #endif
45*2e2caf59SThomas Veerman #endif /* not lint */
46*2e2caf59SThomas Veerman #endif
47*2e2caf59SThomas Veerman 
48*2e2caf59SThomas Veerman /*-
49*2e2caf59SThomas Veerman  * listConcat.c --
50*2e2caf59SThomas Veerman  *	Function to concatentate two lists.
51*2e2caf59SThomas Veerman  */
52*2e2caf59SThomas Veerman 
53*2e2caf59SThomas Veerman #include    "lstInt.h"
54*2e2caf59SThomas Veerman 
55*2e2caf59SThomas Veerman /*-
56*2e2caf59SThomas Veerman  *-----------------------------------------------------------------------
57*2e2caf59SThomas Veerman  * Lst_Concat --
58*2e2caf59SThomas Veerman  *	Concatenate two lists. New elements are created to hold the data
59*2e2caf59SThomas Veerman  *	elements, if specified, but the elements themselves are not copied.
60*2e2caf59SThomas Veerman  *	If the elements should be duplicated to avoid confusion with another
61*2e2caf59SThomas Veerman  *	list, the Lst_Duplicate function should be called first.
62*2e2caf59SThomas Veerman  *	If LST_CONCLINK is specified, the second list is destroyed since
63*2e2caf59SThomas Veerman  *	its pointers have been corrupted and the list is no longer useable.
64*2e2caf59SThomas Veerman  *
65*2e2caf59SThomas Veerman  * Input:
66*2e2caf59SThomas Veerman  *	l1		The list to which l2 is to be appended
67*2e2caf59SThomas Veerman  *	l2		The list to append to l1
68*2e2caf59SThomas Veerman  *	flags		LST_CONCNEW if LstNode's should be duplicated
69*2e2caf59SThomas Veerman  *			LST_CONCLINK if should just be relinked
70*2e2caf59SThomas Veerman  *
71*2e2caf59SThomas Veerman  * Results:
72*2e2caf59SThomas Veerman  *	SUCCESS if all went well. FAILURE otherwise.
73*2e2caf59SThomas Veerman  *
74*2e2caf59SThomas Veerman  * Side Effects:
75*2e2caf59SThomas Veerman  *	New elements are created and appended the first list.
76*2e2caf59SThomas Veerman  *-----------------------------------------------------------------------
77*2e2caf59SThomas Veerman  */
78*2e2caf59SThomas Veerman ReturnStatus
Lst_Concat(Lst l1,Lst l2,int flags)79*2e2caf59SThomas Veerman Lst_Concat(Lst l1, Lst l2, int flags)
80*2e2caf59SThomas Veerman {
81*2e2caf59SThomas Veerman     ListNode  	ln;     /* original LstNode */
82*2e2caf59SThomas Veerman     ListNode  	nln;    /* new LstNode */
83*2e2caf59SThomas Veerman     ListNode  	last;   /* the last element in the list. Keeps
84*2e2caf59SThomas Veerman 				 * bookkeeping until the end */
85*2e2caf59SThomas Veerman     List 	list1 = l1;
86*2e2caf59SThomas Veerman     List 	list2 = l2;
87*2e2caf59SThomas Veerman 
88*2e2caf59SThomas Veerman     if (!LstValid (l1) || !LstValid (l2)) {
89*2e2caf59SThomas Veerman 	return (FAILURE);
90*2e2caf59SThomas Veerman     }
91*2e2caf59SThomas Veerman 
92*2e2caf59SThomas Veerman     if (flags == LST_CONCLINK) {
93*2e2caf59SThomas Veerman 	if (list2->firstPtr != NULL) {
94*2e2caf59SThomas Veerman 	    /*
95*2e2caf59SThomas Veerman 	     * We set the nextPtr of the
96*2e2caf59SThomas Veerman 	     * last element of list two to be NIL to make the loop easier and
97*2e2caf59SThomas Veerman 	     * so we don't need an extra case should the first list turn
98*2e2caf59SThomas Veerman 	     * out to be non-circular -- the final element will already point
99*2e2caf59SThomas Veerman 	     * to NIL space and the first element will be untouched if it
100*2e2caf59SThomas Veerman 	     * existed before and will also point to NIL space if it didn't.
101*2e2caf59SThomas Veerman 	     */
102*2e2caf59SThomas Veerman 	    list2->lastPtr->nextPtr = NULL;
103*2e2caf59SThomas Veerman 	    /*
104*2e2caf59SThomas Veerman 	     * So long as the second list isn't empty, we just link the
105*2e2caf59SThomas Veerman 	     * first element of the second list to the last element of the
106*2e2caf59SThomas Veerman 	     * first list. If the first list isn't empty, we then link the
107*2e2caf59SThomas Veerman 	     * last element of the list to the first element of the second list
108*2e2caf59SThomas Veerman 	     * The last element of the second list, if it exists, then becomes
109*2e2caf59SThomas Veerman 	     * the last element of the first list.
110*2e2caf59SThomas Veerman 	     */
111*2e2caf59SThomas Veerman 	    list2->firstPtr->prevPtr = list1->lastPtr;
112*2e2caf59SThomas Veerman 	    if (list1->lastPtr != NULL) {
113*2e2caf59SThomas Veerman  		list1->lastPtr->nextPtr = list2->firstPtr;
114*2e2caf59SThomas Veerman 	    } else {
115*2e2caf59SThomas Veerman 		list1->firstPtr = list2->firstPtr;
116*2e2caf59SThomas Veerman 	    }
117*2e2caf59SThomas Veerman 	    list1->lastPtr = list2->lastPtr;
118*2e2caf59SThomas Veerman 	}
119*2e2caf59SThomas Veerman 	if (list1->isCirc && list1->firstPtr != NULL) {
120*2e2caf59SThomas Veerman 	    /*
121*2e2caf59SThomas Veerman 	     * If the first list is supposed to be circular and it is (now)
122*2e2caf59SThomas Veerman 	     * non-empty, we must make sure it's circular by linking the
123*2e2caf59SThomas Veerman 	     * first element to the last and vice versa
124*2e2caf59SThomas Veerman 	     */
125*2e2caf59SThomas Veerman 	    list1->firstPtr->prevPtr = list1->lastPtr;
126*2e2caf59SThomas Veerman 	    list1->lastPtr->nextPtr = list1->firstPtr;
127*2e2caf59SThomas Veerman 	}
128*2e2caf59SThomas Veerman 	free(l2);
129*2e2caf59SThomas Veerman     } else if (list2->firstPtr != NULL) {
130*2e2caf59SThomas Veerman 	/*
131*2e2caf59SThomas Veerman 	 * We set the nextPtr of the last element of list 2 to be nil to make
132*2e2caf59SThomas Veerman 	 * the loop less difficult. The loop simply goes through the entire
133*2e2caf59SThomas Veerman 	 * second list creating new LstNodes and filling in the nextPtr, and
134*2e2caf59SThomas Veerman 	 * prevPtr to fit into l1 and its datum field from the
135*2e2caf59SThomas Veerman 	 * datum field of the corresponding element in l2. The 'last' node
136*2e2caf59SThomas Veerman 	 * follows the last of the new nodes along until the entire l2 has
137*2e2caf59SThomas Veerman 	 * been appended. Only then does the bookkeeping catch up with the
138*2e2caf59SThomas Veerman 	 * changes. During the first iteration of the loop, if 'last' is nil,
139*2e2caf59SThomas Veerman 	 * the first list must have been empty so the newly-created node is
140*2e2caf59SThomas Veerman 	 * made the first node of the list.
141*2e2caf59SThomas Veerman 	 */
142*2e2caf59SThomas Veerman 	list2->lastPtr->nextPtr = NULL;
143*2e2caf59SThomas Veerman 	for (last = list1->lastPtr, ln = list2->firstPtr;
144*2e2caf59SThomas Veerman 	     ln != NULL;
145*2e2caf59SThomas Veerman 	     ln = ln->nextPtr)
146*2e2caf59SThomas Veerman 	{
147*2e2caf59SThomas Veerman 	    PAlloc (nln, ListNode);
148*2e2caf59SThomas Veerman 	    nln->datum = ln->datum;
149*2e2caf59SThomas Veerman 	    if (last != NULL) {
150*2e2caf59SThomas Veerman 		last->nextPtr = nln;
151*2e2caf59SThomas Veerman 	    } else {
152*2e2caf59SThomas Veerman 		list1->firstPtr = nln;
153*2e2caf59SThomas Veerman 	    }
154*2e2caf59SThomas Veerman 	    nln->prevPtr = last;
155*2e2caf59SThomas Veerman 	    nln->flags = nln->useCount = 0;
156*2e2caf59SThomas Veerman 	    last = nln;
157*2e2caf59SThomas Veerman 	}
158*2e2caf59SThomas Veerman 
159*2e2caf59SThomas Veerman 	/*
160*2e2caf59SThomas Veerman 	 * Finish bookkeeping. The last new element becomes the last element
161*2e2caf59SThomas Veerman 	 * of list one.
162*2e2caf59SThomas Veerman 	 */
163*2e2caf59SThomas Veerman 	list1->lastPtr = last;
164*2e2caf59SThomas Veerman 
165*2e2caf59SThomas Veerman 	/*
166*2e2caf59SThomas Veerman 	 * The circularity of both list one and list two must be corrected
167*2e2caf59SThomas Veerman 	 * for -- list one because of the new nodes added to it; list two
168*2e2caf59SThomas Veerman 	 * because of the alteration of list2->lastPtr's nextPtr to ease the
169*2e2caf59SThomas Veerman 	 * above for loop.
170*2e2caf59SThomas Veerman 	 */
171*2e2caf59SThomas Veerman 	if (list1->isCirc) {
172*2e2caf59SThomas Veerman 	    list1->lastPtr->nextPtr = list1->firstPtr;
173*2e2caf59SThomas Veerman 	    list1->firstPtr->prevPtr = list1->lastPtr;
174*2e2caf59SThomas Veerman 	} else {
175*2e2caf59SThomas Veerman 	    last->nextPtr = NULL;
176*2e2caf59SThomas Veerman 	}
177*2e2caf59SThomas Veerman 
178*2e2caf59SThomas Veerman 	if (list2->isCirc) {
179*2e2caf59SThomas Veerman 	    list2->lastPtr->nextPtr = list2->firstPtr;
180*2e2caf59SThomas Veerman 	}
181*2e2caf59SThomas Veerman     }
182*2e2caf59SThomas Veerman 
183*2e2caf59SThomas Veerman     return (SUCCESS);
184*2e2caf59SThomas Veerman }
185*2e2caf59SThomas Veerman 
186