1 /* list.c - Functions for manipulating linked lists of objects. */
4 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #if defined (HAVE_UNISTD_H)
26 # include <sys/types.h>
33 /* A global variable which acts as a sentinel for an `error' list return. */
34 GENERIC_LIST global_error_list
;
37 /* Call FUNCTION on every member of LIST, a generic list. */
39 list_walk (list
, function
)
41 sh_glist_func_t
*function
;
43 for ( ; list
; list
= list
->next
)
44 if ((*function
) (list
) < 0)
48 /* Call FUNCTION on every string in WORDS. */
50 wlist_walk (words
, function
)
52 sh_icpfunc_t
*function
;
54 for ( ; words
; words
= words
->next
)
55 if ((*function
) (words
->word
->word
) < 0)
58 #endif /* INCLUDE_UNUSED */
60 /* Reverse the chain of structures in LIST. Output the new head
61 of the chain. You should always assign the output value of this
62 function to something, or you will lose the chain. */
67 register GENERIC_LIST
*next
, *prev
;
69 for (prev
= (GENERIC_LIST
*)NULL
; list
; )
79 /* Return the number of elements in LIST, a generic list. */
86 for (i
= 0; list
; list
= list
->next
, i
++);
90 /* Append TAIL to HEAD. Return the header of the list. */
92 list_append (head
, tail
)
93 GENERIC_LIST
*head
, *tail
;
95 register GENERIC_LIST
*t_head
;
100 for (t_head
= head
; t_head
->next
; t_head
= t_head
->next
)
106 #ifdef INCLUDE_UNUSED
107 /* Delete the element of LIST which satisfies the predicate function COMPARER.
108 Returns the element that was deleted, so you can dispose of it, or -1 if
109 the element wasn't found. COMPARER is called with the list element and
110 then ARG. Note that LIST contains the address of a variable which points
111 to the list. You might call this function like this:
113 SHELL_VAR *elt = list_remove (&variable_list, check_var_has_name, "foo");
114 dispose_variable (elt);
117 list_remove (list
, comparer
, arg
)
122 register GENERIC_LIST
*prev
, *temp
;
124 for (prev
= (GENERIC_LIST
*)NULL
, temp
= *list
; temp
; prev
= temp
, temp
= temp
->next
)
126 if ((*comparer
) (temp
, arg
))
129 prev
->next
= temp
->next
;
135 return ((GENERIC_LIST
*)&global_error_list
);