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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)lstAppend.c	8.2 (Berkeley) 04/28/95";
13 #endif /* not lint */
14 
15 /*-
16  * LstAppend.c --
17  *	Add a new node with a new datum after an existing node
18  */
19 
20 #include	"lstInt.h"
21 
22 /*-
23  *-----------------------------------------------------------------------
24  * Lst_Append --
25  *	Create a new node and add it to the given list after the given node.
26  *
27  * Results:
28  *	SUCCESS if all went well.
29  *
30  * Side Effects:
31  *	A new ListNode is created and linked in to the List. The lastPtr
32  *	field of the List will be altered if ln is the last node in the
33  *	list. lastPtr and firstPtr will alter if the list was empty and
34  *	ln was NILLNODE.
35  *
36  *-----------------------------------------------------------------------
37  */
38 ReturnStatus
39 Lst_Append (l, ln, d)
40     Lst	  	l;	/* affected list */
41     LstNode	ln;	/* node after which to append the datum */
42     ClientData	d;	/* said datum */
43 {
44     register List 	list;
45     register ListNode	lNode;
46     register ListNode	nLNode;
47 
48     if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) {
49 	goto ok;
50     }
51 
52     if (!LstValid (l) || LstIsEmpty (l)  || ! LstNodeValid (ln, l)) {
53 	return (FAILURE);
54     }
55     ok:
56 
57     list = (List)l;
58     lNode = (ListNode)ln;
59 
60     PAlloc (nLNode, ListNode);
61     nLNode->datum = d;
62     nLNode->useCount = nLNode->flags = 0;
63 
64     if (lNode == NilListNode) {
65 	if (list->isCirc) {
66 	    nLNode->nextPtr = nLNode->prevPtr = nLNode;
67 	} else {
68 	    nLNode->nextPtr = nLNode->prevPtr = NilListNode;
69 	}
70 	list->firstPtr = list->lastPtr = nLNode;
71     } else {
72 	nLNode->prevPtr = lNode;
73 	nLNode->nextPtr = lNode->nextPtr;
74 
75 	lNode->nextPtr = nLNode;
76 	if (nLNode->nextPtr != NilListNode) {
77 	    nLNode->nextPtr->prevPtr = nLNode;
78 	}
79 
80 	if (lNode == list->lastPtr) {
81 	    list->lastPtr = nLNode;
82 	}
83     }
84 
85     return (SUCCESS);
86 }
87 
88