1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * 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 are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 static char sccsid[] = "@(#)lstAppend.c	5.2 (Berkeley) 03/11/90";
23 #endif /* not lint */
24 
25 /*-
26  * LstAppend.c --
27  *	Add a new node with a new datum after an existing node
28  */
29 
30 #include	"lstInt.h"
31 
32 /*-
33  *-----------------------------------------------------------------------
34  * Lst_Append --
35  *	Create a new node and add it to the given list after the given node.
36  *
37  * Results:
38  *	SUCCESS if all went well.
39  *
40  * Side Effects:
41  *	A new ListNode is created and linked in to the List. The lastPtr
42  *	field of the List will be altered if ln is the last node in the
43  *	list. lastPtr and firstPtr will alter if the list was empty and
44  *	ln was NILLNODE.
45  *
46  *-----------------------------------------------------------------------
47  */
48 ReturnStatus
49 Lst_Append (l, ln, d)
50     Lst	  	l;	/* affected list */
51     LstNode	ln;	/* node after which to append the datum */
52     ClientData	d;	/* said datum */
53 {
54     register List 	list;
55     register ListNode	lNode;
56     register ListNode	nLNode;
57 
58     if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) {
59 	goto ok;
60     }
61 
62     if (!LstValid (l) || LstIsEmpty (l)  || ! LstNodeValid (ln, l)) {
63 	return (FAILURE);
64     }
65     ok:
66 
67     list = (List)l;
68     lNode = (ListNode)ln;
69 
70     PAlloc (nLNode, ListNode);
71     nLNode->datum = d;
72     nLNode->useCount = nLNode->flags = 0;
73 
74     if (lNode == NilListNode) {
75 	if (list->isCirc) {
76 	    nLNode->nextPtr = nLNode->prevPtr = nLNode;
77 	} else {
78 	    nLNode->nextPtr = nLNode->prevPtr = NilListNode;
79 	}
80 	list->firstPtr = list->lastPtr = nLNode;
81     } else {
82 	nLNode->prevPtr = lNode;
83 	nLNode->nextPtr = lNode->nextPtr;
84 
85 	lNode->nextPtr = nLNode;
86 	if (nLNode->nextPtr != NilListNode) {
87 	    nLNode->nextPtr->prevPtr = nLNode;
88 	}
89 
90 	if (lNode == list->lastPtr) {
91 	    list->lastPtr = nLNode;
92 	}
93     }
94 
95     return (SUCCESS);
96 }
97 
98