140412Sbostic /*
262087Sbostic  * Copyright (c) 1988, 1989, 1990, 1993
362087Sbostic  *	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[] = "@(#)lstInsert.c	8.2 (Berkeley) 04/28/95";
1340412Sbostic #endif /* not lint */
1440411Sbostic 
1540412Sbostic /*-
1640412Sbostic  * LstInsert.c --
1740412Sbostic  *	Insert a new datum before an old one
1840412Sbostic  */
1940412Sbostic 
2040411Sbostic #include	"lstInt.h"
2140411Sbostic 
2240411Sbostic /*-
2340411Sbostic  *-----------------------------------------------------------------------
2440411Sbostic  * Lst_Insert --
2540411Sbostic  *	Insert a new node with the given piece of data before the given
2640411Sbostic  *	node in the given list.
2740411Sbostic  *
2840411Sbostic  * Results:
2940411Sbostic  *	SUCCESS or FAILURE.
3040411Sbostic  *
3140411Sbostic  * Side Effects:
3240411Sbostic  *	the firstPtr field will be changed if ln is the first node in the
3340411Sbostic  *	list.
3440411Sbostic  *
3540411Sbostic  *-----------------------------------------------------------------------
3640411Sbostic  */
3740411Sbostic ReturnStatus
Lst_Insert(l,ln,d)3840411Sbostic Lst_Insert (l, ln, d)
3940411Sbostic     Lst	    	  	l;	/* list to manipulate */
4040411Sbostic     LstNode	  	ln;	/* node before which to insert d */
4140411Sbostic     ClientData	  	d;	/* datum to be inserted */
4240411Sbostic {
4340411Sbostic     register ListNode	nLNode;	/* new lnode for d */
4440411Sbostic     register ListNode	lNode = (ListNode)ln;
4540411Sbostic     register List 	list = (List)l;
4640411Sbostic 
4740411Sbostic 
4840411Sbostic     /*
4940411Sbostic      * check validity of arguments
5040411Sbostic      */
5140411Sbostic     if (LstValid (l) && (LstIsEmpty (l) && ln == NILLNODE))
5240411Sbostic 	goto ok;
5340411Sbostic 
5440411Sbostic     if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) {
5540411Sbostic 	return (FAILURE);
5640411Sbostic     }
5740411Sbostic 
5840411Sbostic     ok:
5940411Sbostic     PAlloc (nLNode, ListNode);
6040411Sbostic 
6140411Sbostic     nLNode->datum = d;
6240411Sbostic     nLNode->useCount = nLNode->flags = 0;
6340411Sbostic 
6440411Sbostic     if (ln == NILLNODE) {
6540411Sbostic 	if (list->isCirc) {
6640411Sbostic 	    nLNode->prevPtr = nLNode->nextPtr = nLNode;
6740411Sbostic 	} else {
6840411Sbostic 	    nLNode->prevPtr = nLNode->nextPtr = NilListNode;
6940411Sbostic 	}
7040411Sbostic 	list->firstPtr = list->lastPtr = nLNode;
7140411Sbostic     } else {
7240411Sbostic 	nLNode->prevPtr = lNode->prevPtr;
7340411Sbostic 	nLNode->nextPtr = lNode;
7440411Sbostic 
7540411Sbostic 	if (nLNode->prevPtr != NilListNode) {
7640411Sbostic 	    nLNode->prevPtr->nextPtr = nLNode;
7740411Sbostic 	}
7840411Sbostic 	lNode->prevPtr = nLNode;
7940411Sbostic 
8040411Sbostic 	if (lNode == list->firstPtr) {
8140411Sbostic 	    list->firstPtr = nLNode;
8240411Sbostic 	}
8340411Sbostic     }
8440411Sbostic 
8540411Sbostic     return (SUCCESS);
8640411Sbostic }
8740411Sbostic 
88