1 const char list_rcs[] = "$Id: list.c,v 1.1 2001/05/31 21:11:53 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/list.c,v $
6 * Purpose : Declares functions to handle lists.
7 * Functions declared include:
8 * `destroy_list', `enlist' and `list_to_text'
10 * Copyright : Written by and Copyright (C) 2001 the SourceForge
11 * IJBSWA team. http://ijbswa.sourceforge.net
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Revision 1.1 2001/05/31 21:11:53 jongfoster
38 * - Moved linked list support to new "list.c" file.
39 * Structure definitions are still in project.h,
40 * function prototypes are now in "list.h".
41 * - Added support for "struct list_share", which is identical
42 * to "struct list" except it saves memory by not duplicating
43 * the strings. Obviously, this only works if there is some
44 * other way of managing the memory used by the strings.
45 * (These list_share lists are used for lists which last
46 * for only 1 request, and where all the list entries are
47 * just coming directly from entries in the actionsfile.)
48 * Note that you still need to destroy list_share lists
49 * properly to free the nodes - it's only the strings
53 *********************************************************************/
59 #include <sys/types.h>
73 const char list_h_rcs[] = LIST_H_VERSION;
75 /*********************************************************************
79 * Description : Append a string into a specified string list.
82 * 1 : header = pointer to list 'dummy' header
83 * 2 : str = string to add to the list (maybe NULL)
87 *********************************************************************/
88 void enlist(struct list *header, const char *str)
90 struct list *cur = (struct list *)malloc(sizeof(*cur));
95 cur->str = (str ? strdup(str) : NULL);
111 /*********************************************************************
113 * Function : enlist_unique
115 * Description : Append a string into a specified string list,
116 * if & only if it's not there already.
119 * 1 : header = pointer to list 'dummy' header
120 * 2 : str = string to add to the list (maybe NULL)
124 *********************************************************************/
125 void enlist_unique(struct list *header, const char *str)
128 struct list *cur = header->next;
132 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
140 cur = (struct list *)malloc(sizeof(*cur));
144 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
158 /*********************************************************************
160 * Function : destroy_list
162 * Description : Destroy a string list (opposite of enlist)
165 * 1 : header = pointer to list 'dummy' header
169 *********************************************************************/
170 void destroy_list(struct list *header)
174 for (p = header->next; p ; p = n)
181 memset(header, '\0', sizeof(*header));
186 /*********************************************************************
188 * Function : list_to_text
190 * Description : "Flaten" a string list into 1 long \r\n delimited string.
193 * 1 : h = pointer to list 'dummy' header
195 * Returns : NULL on malloc error, else new long string.
197 *********************************************************************/
198 char *list_to_text(struct list *h)
207 for (p = h->next; p ; p = p->next)
211 size += strlen(p->str) + 2;
215 if ((ret = (char *)malloc(size + 1)) == NULL)
224 for (p = h->next; p ; p = p->next)
230 *s++ = '\r'; *s++ = '\n';
239 /*********************************************************************
241 * Function : list_remove_item
243 * Description : Remove a string from a specified string list.
246 * 1 : header = pointer to list 'dummy' header
247 * 2 : str = string to remove from the list
249 * Returns : Number of times it was removed.
251 *********************************************************************/
252 int list_remove_item(struct list *header, const char *str)
254 struct list *prev = header;
255 struct list *cur = prev->next;
260 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
264 prev->next = cur->next;
281 /*********************************************************************
283 * Function : list_remove_list
285 * Description : Remove all strings in one list from another list.
286 * This is currently a brute-force algorithm
287 * (it compares every pair of strings).
290 * 1 : dest = list to change
291 * 2 : src = list of strings to remove
293 * Returns : Total number of strings removed.
295 *********************************************************************/
296 int list_remove_list(struct list *dest, const struct list *src)
298 struct list *cur = src->next;
303 if (cur->str != NULL)
305 count += list_remove_item(dest, cur->str);
314 /*********************************************************************
316 * Function : list_duplicate
318 * Description : Duplicate a string list
321 * 1 : dest = pointer to destination for copy. Caller allocs.
322 * 2 : src = pointer to source for copy.
326 *********************************************************************/
327 void list_duplicate(struct list *dest, const struct list *src)
329 struct list * cur_src = src->next;
330 struct list * cur_dest = dest;
332 memset(dest, '\0', sizeof(*dest));
336 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
337 if (cur_dest == NULL)
341 cur_dest->str = strdup(cur_src->str);
342 cur_src = cur_src->next;
345 dest->last = cur_dest;
350 /*********************************************************************
352 * Function : list_append_list_unique
354 * Description : Append a string list to another list.
355 * Duplicate items are not added.
358 * 1 : dest = pointer to destination for merge. Caller allocs.
359 * 2 : src = pointer to source for merge.
363 *********************************************************************/
364 void list_append_list_unique(struct list *dest, const struct list *src)
366 struct list * cur = src->next;
370 enlist_unique(dest, cur->str);