i-bash/list.c

137 lines
3.4 KiB
C
Raw Permalink Normal View History

1996-12-23 17:02:34 +00:00
/* list.c - Functions for manipulating linked lists of objects. */
2009-01-12 13:36:28 +00:00
/* Copyright (C) 1996-2009 Free Software Foundation, Inc.
1996-12-23 17:02:34 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-12-23 17:02:34 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-12-23 17:02:34 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
1996-12-23 17:02:34 +00:00
#include "config.h"
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
#include "shell.h"
/* A global variable which acts as a sentinel for an `error' list return. */
GENERIC_LIST global_error_list;
1998-04-17 19:52:44 +00:00
#ifdef INCLUDE_UNUSED
1996-12-23 17:02:34 +00:00
/* Call FUNCTION on every member of LIST, a generic list. */
void
2002-07-17 14:10:11 +00:00
list_walk (list, function)
1996-12-23 17:02:34 +00:00
GENERIC_LIST *list;
2001-11-13 17:56:06 +00:00
sh_glist_func_t *function;
1996-12-23 17:02:34 +00:00
{
for ( ; list; list = list->next)
2002-07-17 14:10:11 +00:00
if ((*function) (list) < 0)
return;
1996-12-23 17:02:34 +00:00
}
/* Call FUNCTION on every string in WORDS. */
void
2002-07-17 14:10:11 +00:00
wlist_walk (words, function)
1996-12-23 17:02:34 +00:00
WORD_LIST *words;
2001-11-13 17:56:06 +00:00
sh_icpfunc_t *function;
1996-12-23 17:02:34 +00:00
{
for ( ; words; words = words->next)
2002-07-17 14:10:11 +00:00
if ((*function) (words->word->word) < 0)
return;
1996-12-23 17:02:34 +00:00
}
1998-04-17 19:52:44 +00:00
#endif /* INCLUDE_UNUSED */
1996-12-23 17:02:34 +00:00
/* Reverse the chain of structures in LIST. Output the new head
of the chain. You should always assign the output value of this
function to something, or you will lose the chain. */
GENERIC_LIST *
2002-07-17 14:10:11 +00:00
list_reverse (list)
1996-12-23 17:02:34 +00:00
GENERIC_LIST *list;
{
register GENERIC_LIST *next, *prev;
for (prev = (GENERIC_LIST *)NULL; list; )
{
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return (prev);
}
/* Return the number of elements in LIST, a generic list. */
int
list_length (list)
GENERIC_LIST *list;
{
register int i;
for (i = 0; list; list = list->next, i++);
return (i);
}
/* Append TAIL to HEAD. Return the header of the list. */
GENERIC_LIST *
list_append (head, tail)
GENERIC_LIST *head, *tail;
{
register GENERIC_LIST *t_head;
if (head == 0)
return (tail);
for (t_head = head; t_head->next; t_head = t_head->next)
;
t_head->next = tail;
return (head);
}
1997-06-05 14:59:13 +00:00
#ifdef INCLUDE_UNUSED
1996-12-23 17:02:34 +00:00
/* Delete the element of LIST which satisfies the predicate function COMPARER.
Returns the element that was deleted, so you can dispose of it, or -1 if
the element wasn't found. COMPARER is called with the list element and
then ARG. Note that LIST contains the address of a variable which points
to the list. You might call this function like this:
2002-07-17 14:10:11 +00:00
SHELL_VAR *elt = list_remove (&variable_list, check_var_has_name, "foo");
1996-12-23 17:02:34 +00:00
dispose_variable (elt);
*/
GENERIC_LIST *
2002-07-17 14:10:11 +00:00
list_remove (list, comparer, arg)
1996-12-23 17:02:34 +00:00
GENERIC_LIST **list;
Function *comparer;
char *arg;
{
register GENERIC_LIST *prev, *temp;
for (prev = (GENERIC_LIST *)NULL, temp = *list; temp; prev = temp, temp = temp->next)
{
if ((*comparer) (temp, arg))
{
if (prev)
prev->next = temp->next;
else
*list = temp->next;
return (temp);
}
}
return ((GENERIC_LIST *)&global_error_list);
}
1997-06-05 14:59:13 +00:00
#endif