Clean up include header mess. Split nonints into separate header files.
[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.11 2005/01/06 10:53:00 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 <stdio.h>
48
49 #include "make.h"
50 #include "util.h"
51 #include "lst.h"
52
53 /*-
54  *-----------------------------------------------------------------------
55  * Lst_Concat --
56  *      Concatenate two lists. New elements are created to hold the data
57  *      elements, if specified, but the elements themselves are not copied.
58  *      If the elements should be duplicated to avoid confusion with another
59  *      list, the Lst_Duplicate function should be called first.
60  *
61  * Results:
62  *      SUCCESS if all went well. FAILURE otherwise.
63  *
64  * Arguments:
65  *      list1   The list to which list2 is to be appended
66  *      list2   The list to append to list1
67  *      flags   LST_CONCNEW if LstNode's should be duplicated
68  *              LST_CONCLINK if should just be relinked
69  *
70  * Side Effects:
71  *      New elements are created and appended the the first list.
72  *-----------------------------------------------------------------------
73  */
74 void
75 Lst_Concat(Lst *list1, Lst *list2, int flags)
76 {
77     LstNode *ln;        /* original LstNode */
78     LstNode *nln;       /* new LstNode */
79     LstNode *last;      /* the last element in the list. Keeps
80                          * bookkeeping until the end */
81
82     if (list2->firstPtr == NULL)
83         return;
84
85     if (flags == LST_CONCLINK) {
86         /*
87          * Link the first element of the second list to the last element of the
88          * first list. If the first list isn't empty, we then link the
89          * last element of the list to the first element of the second list
90          * The last element of the second list, if it exists, then becomes
91          * the last element of the first list.
92          */
93         list2->firstPtr->prevPtr = list1->lastPtr;
94         if (list1->lastPtr != NULL)
95             list1->lastPtr->nextPtr = list2->firstPtr;
96         else
97             list1->firstPtr = list2->firstPtr;
98         list1->lastPtr = list2->lastPtr;
99
100         Lst_Init(list2);
101     } else {
102         /*
103          * The loop simply goes through the entire
104          * second list creating new LstNodes and filling in the nextPtr, and
105          * prevPtr to fit into list1 and its datum field from the
106          * datum field of the corresponding element in list2. The 'last' node
107          * follows the last of the new nodes along until the entire list2 has
108          * been appended. Only then does the bookkeeping catch up with the
109          * changes. During the first iteration of the loop, if 'last' is NULL,
110          * the first list must have been empty so the newly-created node is
111          * made the first node of the list.
112          */
113         for (last = list1->lastPtr, ln = list2->firstPtr;
114              ln != NULL;
115              ln = ln->nextPtr)
116         {
117             nln = emalloc(sizeof(*nln));
118             nln->datum = ln->datum;
119             if (last != NULL) {
120                 last->nextPtr = nln;
121             } else {
122                 list1->firstPtr = nln;
123             }
124             nln->prevPtr = last;
125             nln->flags = nln->useCount = 0;
126             last = nln;
127         }
128
129         /*
130          * Finish bookkeeping. The last new element becomes the last element
131          * of list one.
132          */
133         list1->lastPtr = last;
134         last->nextPtr = NULL;
135     }
136 }