1*40411Sbostic /*-
2*40411Sbostic  * LstInsert.c --
3*40411Sbostic  *	Insert a new datum before an old one
4*40411Sbostic  *
5*40411Sbostic  * Copyright (c) 1988 by University of California Regents
6*40411Sbostic  *
7*40411Sbostic  * Permission to use, copy, modify, and distribute this
8*40411Sbostic  * software and its documentation for any purpose and without
9*40411Sbostic  * fee is hereby granted, provided that the above copyright
10*40411Sbostic  * notice appears in all copies.  Neither the University of California nor
11*40411Sbostic  * Adam de Boor makes any representations about the suitability of this
12*40411Sbostic  * software for any purpose.  It is provided "as is" without
13*40411Sbostic  * express or implied warranty.
14*40411Sbostic  */
15*40411Sbostic #ifndef lint
16*40411Sbostic static char *rcsid =
17*40411Sbostic "$Id: lstInsert.c,v 1.6 89/06/13 15:01:43 adam Exp $ SPRITE (Berkeley)";
18*40411Sbostic #endif lint
19*40411Sbostic 
20*40411Sbostic #include	"lstInt.h"
21*40411Sbostic 
22*40411Sbostic /*-
23*40411Sbostic  *-----------------------------------------------------------------------
24*40411Sbostic  * Lst_Insert --
25*40411Sbostic  *	Insert a new node with the given piece of data before the given
26*40411Sbostic  *	node in the given list.
27*40411Sbostic  *
28*40411Sbostic  * Results:
29*40411Sbostic  *	SUCCESS or FAILURE.
30*40411Sbostic  *
31*40411Sbostic  * Side Effects:
32*40411Sbostic  *	the firstPtr field will be changed if ln is the first node in the
33*40411Sbostic  *	list.
34*40411Sbostic  *
35*40411Sbostic  *-----------------------------------------------------------------------
36*40411Sbostic  */
37*40411Sbostic ReturnStatus
38*40411Sbostic Lst_Insert (l, ln, d)
39*40411Sbostic     Lst	    	  	l;	/* list to manipulate */
40*40411Sbostic     LstNode	  	ln;	/* node before which to insert d */
41*40411Sbostic     ClientData	  	d;	/* datum to be inserted */
42*40411Sbostic {
43*40411Sbostic     register ListNode	nLNode;	/* new lnode for d */
44*40411Sbostic     register ListNode	lNode = (ListNode)ln;
45*40411Sbostic     register List 	list = (List)l;
46*40411Sbostic 
47*40411Sbostic 
48*40411Sbostic     /*
49*40411Sbostic      * check validity of arguments
50*40411Sbostic      */
51*40411Sbostic     if (LstValid (l) && (LstIsEmpty (l) && ln == NILLNODE))
52*40411Sbostic 	goto ok;
53*40411Sbostic 
54*40411Sbostic     if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) {
55*40411Sbostic 	return (FAILURE);
56*40411Sbostic     }
57*40411Sbostic 
58*40411Sbostic     ok:
59*40411Sbostic     PAlloc (nLNode, ListNode);
60*40411Sbostic 
61*40411Sbostic     nLNode->datum = d;
62*40411Sbostic     nLNode->useCount = nLNode->flags = 0;
63*40411Sbostic 
64*40411Sbostic     if (ln == NILLNODE) {
65*40411Sbostic 	if (list->isCirc) {
66*40411Sbostic 	    nLNode->prevPtr = nLNode->nextPtr = nLNode;
67*40411Sbostic 	} else {
68*40411Sbostic 	    nLNode->prevPtr = nLNode->nextPtr = NilListNode;
69*40411Sbostic 	}
70*40411Sbostic 	list->firstPtr = list->lastPtr = nLNode;
71*40411Sbostic     } else {
72*40411Sbostic 	nLNode->prevPtr = lNode->prevPtr;
73*40411Sbostic 	nLNode->nextPtr = lNode;
74*40411Sbostic 
75*40411Sbostic 	if (nLNode->prevPtr != NilListNode) {
76*40411Sbostic 	    nLNode->prevPtr->nextPtr = nLNode;
77*40411Sbostic 	}
78*40411Sbostic 	lNode->prevPtr = nLNode;
79*40411Sbostic 
80*40411Sbostic 	if (lNode == list->firstPtr) {
81*40411Sbostic 	    list->firstPtr = nLNode;
82*40411Sbostic 	}
83*40411Sbostic     }
84*40411Sbostic 
85*40411Sbostic     return (SUCCESS);
86*40411Sbostic }
87*40411Sbostic 
88