140412Sbostic /*
262085Sbostic  * Copyright (c) 1988, 1989, 1990, 1993
362085Sbostic  *	The Regents of the University of California.  All rights reserved.
440411Sbostic  *
540412Sbostic  * This code is derived from software contributed to Berkeley by
640412Sbostic  * Adam de Boor.
740411Sbostic  *
842741Sbostic  * %sccs.include.redist.c%
940411Sbostic  */
1040412Sbostic 
1140411Sbostic #ifndef lint
12*69094Schristos static char sccsid[] = "@(#)lstAppend.c	8.2 (Berkeley) 04/28/95";
1340412Sbostic #endif /* not lint */
1440411Sbostic 
1540412Sbostic /*-
1640412Sbostic  * LstAppend.c --
1740412Sbostic  *	Add a new node with a new datum after an existing node
1840412Sbostic  */
1940412Sbostic 
2040411Sbostic #include	"lstInt.h"
2140411Sbostic 
2240411Sbostic /*-
2340411Sbostic  *-----------------------------------------------------------------------
2440411Sbostic  * Lst_Append --
2540411Sbostic  *	Create a new node and add it to the given list after the given node.
2640411Sbostic  *
2740411Sbostic  * Results:
2840411Sbostic  *	SUCCESS if all went well.
2940411Sbostic  *
3040411Sbostic  * Side Effects:
3140411Sbostic  *	A new ListNode is created and linked in to the List. The lastPtr
3240411Sbostic  *	field of the List will be altered if ln is the last node in the
3340411Sbostic  *	list. lastPtr and firstPtr will alter if the list was empty and
3440411Sbostic  *	ln was NILLNODE.
3540411Sbostic  *
3640411Sbostic  *-----------------------------------------------------------------------
3740411Sbostic  */
3840411Sbostic ReturnStatus
Lst_Append(l,ln,d)3940411Sbostic Lst_Append (l, ln, d)
4040411Sbostic     Lst	  	l;	/* affected list */
4140411Sbostic     LstNode	ln;	/* node after which to append the datum */
4240411Sbostic     ClientData	d;	/* said datum */
4340411Sbostic {
4440411Sbostic     register List 	list;
4540411Sbostic     register ListNode	lNode;
4640411Sbostic     register ListNode	nLNode;
4740411Sbostic 
4840411Sbostic     if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) {
4940411Sbostic 	goto ok;
5040411Sbostic     }
5140411Sbostic 
5240411Sbostic     if (!LstValid (l) || LstIsEmpty (l)  || ! LstNodeValid (ln, l)) {
5340411Sbostic 	return (FAILURE);
5440411Sbostic     }
5540411Sbostic     ok:
5640411Sbostic 
5740411Sbostic     list = (List)l;
5840411Sbostic     lNode = (ListNode)ln;
5940411Sbostic 
6040411Sbostic     PAlloc (nLNode, ListNode);
6140411Sbostic     nLNode->datum = d;
6240411Sbostic     nLNode->useCount = nLNode->flags = 0;
6340411Sbostic 
6440411Sbostic     if (lNode == NilListNode) {
6540411Sbostic 	if (list->isCirc) {
6640411Sbostic 	    nLNode->nextPtr = nLNode->prevPtr = nLNode;
6740411Sbostic 	} else {
6840411Sbostic 	    nLNode->nextPtr = nLNode->prevPtr = NilListNode;
6940411Sbostic 	}
7040411Sbostic 	list->firstPtr = list->lastPtr = nLNode;
7140411Sbostic     } else {
7240411Sbostic 	nLNode->prevPtr = lNode;
7340411Sbostic 	nLNode->nextPtr = lNode->nextPtr;
7440411Sbostic 
7540411Sbostic 	lNode->nextPtr = nLNode;
7640411Sbostic 	if (nLNode->nextPtr != NilListNode) {
7740411Sbostic 	    nLNode->nextPtr->prevPtr = nLNode;
7840411Sbostic 	}
7940411Sbostic 
8040411Sbostic 	if (lNode == list->lastPtr) {
8140411Sbostic 	    list->lastPtr = nLNode;
8240411Sbostic 	}
8340411Sbostic     }
8440411Sbostic 
8540411Sbostic     return (SUCCESS);
8640411Sbostic }
8740411Sbostic 
88