1*40412Sbostic /*
2*40412Sbostic  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3*40412Sbostic  * All rights reserved.
440411Sbostic  *
5*40412Sbostic  * This code is derived from software contributed to Berkeley by
6*40412Sbostic  * Adam de Boor.
740411Sbostic  *
8*40412Sbostic  * Redistribution and use in source and binary forms are permitted
9*40412Sbostic  * provided that the above copyright notice and this paragraph are
10*40412Sbostic  * duplicated in all such forms and that any documentation,
11*40412Sbostic  * advertising materials, and other materials related to such
12*40412Sbostic  * distribution and use acknowledge that the software was developed
13*40412Sbostic  * by the University of California, Berkeley.  The name of the
14*40412Sbostic  * University may not be used to endorse or promote products derived
15*40412Sbostic  * from this software without specific prior written permission.
16*40412Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*40412Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*40412Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1940411Sbostic  */
20*40412Sbostic 
2140411Sbostic #ifndef lint
22*40412Sbostic static char sccsid[] = "@(#)lstAppend.c	5.2 (Berkeley) 03/11/90";
23*40412Sbostic #endif /* not lint */
2440411Sbostic 
25*40412Sbostic /*-
26*40412Sbostic  * LstAppend.c --
27*40412Sbostic  *	Add a new node with a new datum after an existing node
28*40412Sbostic  */
29*40412Sbostic 
3040411Sbostic #include	"lstInt.h"
3140411Sbostic 
3240411Sbostic /*-
3340411Sbostic  *-----------------------------------------------------------------------
3440411Sbostic  * Lst_Append --
3540411Sbostic  *	Create a new node and add it to the given list after the given node.
3640411Sbostic  *
3740411Sbostic  * Results:
3840411Sbostic  *	SUCCESS if all went well.
3940411Sbostic  *
4040411Sbostic  * Side Effects:
4140411Sbostic  *	A new ListNode is created and linked in to the List. The lastPtr
4240411Sbostic  *	field of the List will be altered if ln is the last node in the
4340411Sbostic  *	list. lastPtr and firstPtr will alter if the list was empty and
4440411Sbostic  *	ln was NILLNODE.
4540411Sbostic  *
4640411Sbostic  *-----------------------------------------------------------------------
4740411Sbostic  */
4840411Sbostic ReturnStatus
4940411Sbostic Lst_Append (l, ln, d)
5040411Sbostic     Lst	  	l;	/* affected list */
5140411Sbostic     LstNode	ln;	/* node after which to append the datum */
5240411Sbostic     ClientData	d;	/* said datum */
5340411Sbostic {
5440411Sbostic     register List 	list;
5540411Sbostic     register ListNode	lNode;
5640411Sbostic     register ListNode	nLNode;
5740411Sbostic 
5840411Sbostic     if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) {
5940411Sbostic 	goto ok;
6040411Sbostic     }
6140411Sbostic 
6240411Sbostic     if (!LstValid (l) || LstIsEmpty (l)  || ! LstNodeValid (ln, l)) {
6340411Sbostic 	return (FAILURE);
6440411Sbostic     }
6540411Sbostic     ok:
6640411Sbostic 
6740411Sbostic     list = (List)l;
6840411Sbostic     lNode = (ListNode)ln;
6940411Sbostic 
7040411Sbostic     PAlloc (nLNode, ListNode);
7140411Sbostic     nLNode->datum = d;
7240411Sbostic     nLNode->useCount = nLNode->flags = 0;
7340411Sbostic 
7440411Sbostic     if (lNode == NilListNode) {
7540411Sbostic 	if (list->isCirc) {
7640411Sbostic 	    nLNode->nextPtr = nLNode->prevPtr = nLNode;
7740411Sbostic 	} else {
7840411Sbostic 	    nLNode->nextPtr = nLNode->prevPtr = NilListNode;
7940411Sbostic 	}
8040411Sbostic 	list->firstPtr = list->lastPtr = nLNode;
8140411Sbostic     } else {
8240411Sbostic 	nLNode->prevPtr = lNode;
8340411Sbostic 	nLNode->nextPtr = lNode->nextPtr;
8440411Sbostic 
8540411Sbostic 	lNode->nextPtr = nLNode;
8640411Sbostic 	if (nLNode->nextPtr != NilListNode) {
8740411Sbostic 	    nLNode->nextPtr->prevPtr = nLNode;
8840411Sbostic 	}
8940411Sbostic 
9040411Sbostic 	if (lNode == list->lastPtr) {
9140411Sbostic 	    list->lastPtr = nLNode;
9240411Sbostic 	}
9340411Sbostic     }
9440411Sbostic 
9540411Sbostic     return (SUCCESS);
9640411Sbostic }
9740411Sbostic 
98