]> jfr.im git - irc/quakenet/snircd.git/blob - libs/dbprim/ll_remove.c
import of 2.10.12.05
[irc/quakenet/snircd.git] / libs / dbprim / ll_remove.c
1 /*
2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 ** MA 02111-1307, USA
18 **
19 ** @(#)$Id: ll_remove.c,v 1.1 2003/03/07 02:36:11 klmitch Exp $
20 */
21 #include "dbprim.h"
22 #include "dbprim_int.h"
23
24 RCSTAG("@(#)$Id: ll_remove.c,v 1.1 2003/03/07 02:36:11 klmitch Exp $");
25
26 /** \ingroup dbprim_link
27 * \brief Remove an element from a linked list.
28 *
29 * This function removes a specified element from a linked list.
30 *
31 * \param list A pointer to a #link_head_t.
32 * \param elem A pointer to the #link_elem_t describing the element
33 * to be removed.
34 *
35 * \retval DB_ERR_BADARGS An argument was invalid.
36 * \retval DB_ERR_UNUSED \p elem is not in a linked list.
37 * \retval DB_ERR_WRONGTABLE \p elem is not in this linked list.
38 */
39 unsigned long
40 ll_remove(link_head_t *list, link_elem_t *elem)
41 {
42 initialize_dbpr_error_table(); /* initialize error table */
43
44 if (!ll_verify(list) || !le_verify(elem)) /* First, verify the arguments */
45 return DB_ERR_BADARGS;
46
47 if (!elem->le_head) /* is the element even being used? */
48 return DB_ERR_UNUSED;
49 if (list != elem->le_head) /* Verify that the element is in this list */
50 return DB_ERR_WRONGTABLE;
51
52 list->lh_count--; /* OK, reduce the list count */
53
54 if (elem->le_next) /* Clip the list back together */
55 elem->le_next->le_prev = elem->le_prev;
56 if (elem->le_prev)
57 elem->le_prev->le_next = elem->le_next;
58 else
59 list->lh_first = elem->le_next;
60
61 if (list->lh_last == elem) /* Make sure we know where the last element is */
62 list->lh_last = elem->le_prev;
63
64 elem->le_next = 0; /* Clear the element out */
65 elem->le_prev = 0;
66 elem->le_head = 0;
67
68 return 0;
69 }