1236769Sobrien/*	$NetBSD: lstRemove.c,v 1.14 2008/12/13 15:19:29 dsl Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990, 1993
5236769Sobrien *	The Regents of the University of California.  All rights reserved.
6236769Sobrien *
7236769Sobrien * This code is derived from software contributed to Berkeley by
8236769Sobrien * Adam de Boor.
9236769Sobrien *
10236769Sobrien * Redistribution and use in source and binary forms, with or without
11236769Sobrien * modification, are permitted provided that the following conditions
12236769Sobrien * are met:
13236769Sobrien * 1. Redistributions of source code must retain the above copyright
14236769Sobrien *    notice, this list of conditions and the following disclaimer.
15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
16236769Sobrien *    notice, this list of conditions and the following disclaimer in the
17236769Sobrien *    documentation and/or other materials provided with the distribution.
18236769Sobrien * 3. Neither the name of the University nor the names of its contributors
19236769Sobrien *    may be used to endorse or promote products derived from this software
20236769Sobrien *    without specific prior written permission.
21236769Sobrien *
22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32236769Sobrien * SUCH DAMAGE.
33236769Sobrien */
34236769Sobrien
35236769Sobrien#ifndef MAKE_NATIVE
36236769Sobrienstatic char rcsid[] = "$NetBSD: lstRemove.c,v 1.14 2008/12/13 15:19:29 dsl Exp $";
37236769Sobrien#else
38236769Sobrien#include <sys/cdefs.h>
39236769Sobrien#ifndef lint
40236769Sobrien#if 0
41236769Sobrienstatic char sccsid[] = "@(#)lstRemove.c	8.1 (Berkeley) 6/6/93";
42236769Sobrien#else
43236769Sobrien__RCSID("$NetBSD: lstRemove.c,v 1.14 2008/12/13 15:19:29 dsl Exp $");
44236769Sobrien#endif
45236769Sobrien#endif /* not lint */
46236769Sobrien#endif
47236769Sobrien
48236769Sobrien/*-
49236769Sobrien * LstRemove.c --
50236769Sobrien *	Remove an element from a list
51236769Sobrien */
52236769Sobrien
53236769Sobrien#include	"lstInt.h"
54236769Sobrien
55236769Sobrien/*-
56236769Sobrien *-----------------------------------------------------------------------
57236769Sobrien * Lst_Remove --
58236769Sobrien *	Remove the given node from the given list.
59236769Sobrien *
60236769Sobrien * Results:
61236769Sobrien *	SUCCESS or FAILURE.
62236769Sobrien *
63236769Sobrien * Side Effects:
64236769Sobrien *	The list's firstPtr will be set to NULL if ln is the last
65236769Sobrien *	node on the list. firsPtr and lastPtr will be altered if ln is
66236769Sobrien *	either the first or last node, respectively, on the list.
67236769Sobrien *
68236769Sobrien *-----------------------------------------------------------------------
69236769Sobrien */
70236769SobrienReturnStatus
71236769SobrienLst_Remove(Lst l, LstNode ln)
72236769Sobrien{
73236769Sobrien    List 	list = l;
74236769Sobrien    ListNode	lNode = ln;
75236769Sobrien
76236769Sobrien    if (!LstValid (l) ||
77236769Sobrien	!LstNodeValid (ln, l)) {
78236769Sobrien	    return (FAILURE);
79236769Sobrien    }
80236769Sobrien
81236769Sobrien    /*
82236769Sobrien     * unlink it from the list
83236769Sobrien     */
84236769Sobrien    if (lNode->nextPtr != NULL) {
85236769Sobrien	lNode->nextPtr->prevPtr = lNode->prevPtr;
86236769Sobrien    }
87236769Sobrien    if (lNode->prevPtr != NULL) {
88236769Sobrien	lNode->prevPtr->nextPtr = lNode->nextPtr;
89236769Sobrien    }
90236769Sobrien
91236769Sobrien    /*
92236769Sobrien     * if either the firstPtr or lastPtr of the list point to this node,
93236769Sobrien     * adjust them accordingly
94236769Sobrien     */
95236769Sobrien    if (list->firstPtr == lNode) {
96236769Sobrien	list->firstPtr = lNode->nextPtr;
97236769Sobrien    }
98236769Sobrien    if (list->lastPtr == lNode) {
99236769Sobrien	list->lastPtr = lNode->prevPtr;
100236769Sobrien    }
101236769Sobrien
102236769Sobrien    /*
103236769Sobrien     * Sequential access stuff. If the node we're removing is the current
104236769Sobrien     * node in the list, reset the current node to the previous one. If the
105236769Sobrien     * previous one was non-existent (prevPtr == NULL), we set the
106236769Sobrien     * end to be Unknown, since it is.
107236769Sobrien     */
108236769Sobrien    if (list->isOpen && (list->curPtr == lNode)) {
109236769Sobrien	list->curPtr = list->prevPtr;
110236769Sobrien	if (list->curPtr == NULL) {
111236769Sobrien	    list->atEnd = Unknown;
112236769Sobrien	}
113236769Sobrien    }
114236769Sobrien
115236769Sobrien    /*
116236769Sobrien     * the only way firstPtr can still point to ln is if ln is the last
117236769Sobrien     * node on the list (the list is circular, so lNode->nextptr == lNode in
118236769Sobrien     * this case). The list is, therefore, empty and is marked as such
119236769Sobrien     */
120236769Sobrien    if (list->firstPtr == lNode) {
121236769Sobrien	list->firstPtr = NULL;
122236769Sobrien    }
123236769Sobrien
124236769Sobrien    /*
125236769Sobrien     * note that the datum is unmolested. The caller must free it as
126236769Sobrien     * necessary and as expected.
127236769Sobrien     */
128236769Sobrien    if (lNode->useCount == 0) {
129236769Sobrien	free(ln);
130236769Sobrien    } else {
131236769Sobrien	lNode->flags |= LN_DELETED;
132236769Sobrien    }
133236769Sobrien
134236769Sobrien    return (SUCCESS);
135236769Sobrien}
136236769Sobrien
137