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