1 /* list.c - Functions for manipulating linked lists of objects. */
3 /* Copyright (C) 1996-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
32 /* A global variable which acts as a sentinel for an `error' list return. */
33 GENERIC_LIST global_error_list
;
36 /* Call FUNCTION on every member of LIST, a generic list. */
38 list_walk (list
, function
)
40 sh_glist_func_t
*function
;
42 for ( ; list
; list
= list
->next
)
43 if ((*function
) (list
) < 0)
47 /* Call FUNCTION on every string in WORDS. */
49 wlist_walk (words
, function
)
51 sh_icpfunc_t
*function
;
53 for ( ; words
; words
= words
->next
)
54 if ((*function
) (words
->word
->word
) < 0)
57 #endif /* INCLUDE_UNUSED */
59 /* Reverse the chain of structures in LIST. Output the new head
60 of the chain. You should always assign the output value of this
61 function to something, or you will lose the chain. */
66 register GENERIC_LIST
*next
, *prev
;
68 for (prev
= (GENERIC_LIST
*)NULL
; list
; )
78 /* Return the number of elements in LIST, a generic list. */
85 for (i
= 0; list
; list
= list
->next
, i
++);
89 /* Append TAIL to HEAD. Return the header of the list. */
91 list_append (head
, tail
)
92 GENERIC_LIST
*head
, *tail
;
94 register GENERIC_LIST
*t_head
;
99 for (t_head
= head
; t_head
->next
; t_head
= t_head
->next
)
105 #ifdef INCLUDE_UNUSED
106 /* Delete the element of LIST which satisfies the predicate function COMPARER.
107 Returns the element that was deleted, so you can dispose of it, or -1 if
108 the element wasn't found. COMPARER is called with the list element and
109 then ARG. Note that LIST contains the address of a variable which points
110 to the list. You might call this function like this:
112 SHELL_VAR *elt = list_remove (&variable_list, check_var_has_name, "foo");
113 dispose_variable (elt);
116 list_remove (list
, comparer
, arg
)
121 register GENERIC_LIST
*prev
, *temp
;
123 for (prev
= (GENERIC_LIST
*)NULL
, temp
= *list
; temp
; prev
= temp
, temp
= temp
->next
)
125 if ((*comparer
) (temp
, arg
))
128 prev
->next
= temp
->next
;
134 return ((GENERIC_LIST
*)&global_error_list
);