b43a38d92bce8f64852d7e8ed7338efb1ccfe3ec
[ircu2.10.12-pk.git] / libs / dbprim / ll_iter.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$
20 */
21 #include "dbprim.h"
22 #include "dbprim_int.h"
23
24 RCSTAG("@(#)$Id$");
25
26 /** \ingroup dbprim_link
27  * \brief Iterate over each entry in a linked list.
28  *
29  * This function iterates over a linked list, executing the given \p
30  * iter_func for each entry.
31  *
32  * \param list  A pointer to a #link_head_t.
33  * \param iter_func
34  *              A pointer to a callback function used to perform
35  *              user-specified actions on an element in a linked
36  *              list.  \c NULL is an invalid value.  See the
37  *              documentation for #link_iter_t for more information.
38  * \param extra A \c void pointer that will be passed to \p
39  *              iter_func.
40  *
41  * \retval DB_ERR_BADARGS       An argument was invalid.
42  */
43 unsigned long
44 ll_iter(link_head_t *list, link_iter_t iter_func, void *extra)
45 {
46   unsigned long retval;
47   link_elem_t *elem;
48
49   initialize_dbpr_error_table(); /* initialize error table */
50
51   if (!ll_verify(list) || !iter_func) /* verify arguments */
52     return DB_ERR_BADARGS;
53
54   /* Walk through list and return first non-zero return value */
55   for (elem = list->lh_first; elem; elem = elem->le_next)
56     if ((retval = (*iter_func)(list, elem, extra)))
57       return retval;
58
59   return 0;
60 }