xref: /openbsd/usr.bin/make/lst.lib/lstAppend.c (revision 78b63d65)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: lstAppend.c,v 1.14 2001/05/29 12:53:44 espie Exp $	*/
3 /*	$NetBSD: lstAppend.c,v 1.5 1996/11/06 17:59:31 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1988, 1989, 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Adam de Boor.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 /*-
42  * LstAppend.c --
43  *	Add a new node with a new datum after an existing node
44  */
45 
46 #include "lstInt.h"
47 #include <sys/types.h>
48 #include <stddef.h>
49 #include "memory.h"
50 
51 /*-
52  *-----------------------------------------------------------------------
53  * Lst_Append --
54  *	Create a new node and add it to the given list after the given node.
55  *
56  * Side Effects:
57  *	A new ListNode is created and linked in to the List. The lastPtr
58  *	field of the List will be altered if ln is the last node in the
59  *	list. lastPtr and firstPtr will alter if the list was empty and
60  *	ln was NULL.
61  *
62  *-----------------------------------------------------------------------
63  */
64 void
65 Lst_Append(l, ln, d)
66     Lst 	l;	/* affected list */
67     LstNode	ln;	/* node after which to append the datum */
68     void	*d;	/* said datum */
69 {
70     LstNode	nLNode;
71 
72     if (ln == NULL && !Lst_IsEmpty(l))
73 	return;
74 
75     if (ln != NULL && Lst_IsEmpty(l))
76 	return;
77 
78     PAlloc(nLNode, LstNode);
79     nLNode->datum = d;
80 
81     if (ln == NULL) {
82 	nLNode->nextPtr = nLNode->prevPtr = NULL;
83 	l->firstPtr = l->lastPtr = nLNode;
84     } else {
85 	nLNode->prevPtr = ln;
86 	nLNode->nextPtr = ln->nextPtr;
87 
88 	ln->nextPtr = nLNode;
89 	if (nLNode->nextPtr != NULL)
90 	    nLNode->nextPtr->prevPtr = nLNode;
91 
92 	if (ln == l->lastPtr)
93 	    l->lastPtr = nLNode;
94     }
95 }
96 
97 void
98 Lst_AtEnd(l, d)
99     Lst 	l;
100     void	*d;
101 {
102     LstNode	ln;
103 
104     PAlloc(ln, LstNode);
105     ln->datum = d;
106 
107     ln->prevPtr = l->lastPtr;
108     ln->nextPtr = NULL;
109     if (l->lastPtr == NULL)
110 	l->firstPtr = ln;
111     else
112 	l->lastPtr->nextPtr = ln;
113     l->lastPtr = ln;
114 }
115