1 const char list_rcs[] = "$Id: list.c,v NOT CHECKED IN $";
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.
38 *********************************************************************/
44 #include <sys/types.h>
58 const char list_h_rcs[] = LIST_H_VERSION;
60 /*********************************************************************
64 * Description : Append a string into a specified string list.
67 * 1 : header = pointer to list 'dummy' header
68 * 2 : str = string to add to the list (maybe NULL)
72 *********************************************************************/
73 void enlist(struct list *header, const char *str)
75 struct list *cur = (struct list *)malloc(sizeof(*cur));
80 cur->str = (str ? strdup(str) : NULL);
96 /*********************************************************************
98 * Function : enlist_unique
100 * Description : Append a string into a specified string list,
101 * if & only if it's not there already.
104 * 1 : header = pointer to list 'dummy' header
105 * 2 : str = string to add to the list (maybe NULL)
109 *********************************************************************/
110 void enlist_unique(struct list *header, const char *str)
113 struct list *cur = header->next;
117 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
125 cur = (struct list *)malloc(sizeof(*cur));
129 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
143 /*********************************************************************
145 * Function : destroy_list
147 * Description : Destroy a string list (opposite of enlist)
150 * 1 : header = pointer to list 'dummy' header
154 *********************************************************************/
155 void destroy_list(struct list *header)
159 for (p = header->next; p ; p = n)
166 memset(header, '\0', sizeof(*header));
171 /*********************************************************************
173 * Function : list_to_text
175 * Description : "Flaten" a string list into 1 long \r\n delimited string.
178 * 1 : h = pointer to list 'dummy' header
180 * Returns : NULL on malloc error, else new long string.
182 *********************************************************************/
183 char *list_to_text(struct list *h)
192 for (p = h->next; p ; p = p->next)
196 size += strlen(p->str) + 2;
200 if ((ret = (char *)malloc(size + 1)) == NULL)
209 for (p = h->next; p ; p = p->next)
215 *s++ = '\r'; *s++ = '\n';
224 /*********************************************************************
226 * Function : list_remove_item
228 * Description : Remove a string from a specified string list.
231 * 1 : header = pointer to list 'dummy' header
232 * 2 : str = string to remove from the list
234 * Returns : Number of times it was removed.
236 *********************************************************************/
237 int list_remove_item(struct list *header, const char *str)
239 struct list *prev = header;
240 struct list *cur = prev->next;
245 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
249 prev->next = cur->next;
266 /*********************************************************************
268 * Function : list_remove_list
270 * Description : Remove all strings in one list from another list.
271 * This is currently a brute-force algorithm
272 * (it compares every pair of strings).
275 * 1 : dest = list to change
276 * 2 : src = list of strings to remove
278 * Returns : Total number of strings removed.
280 *********************************************************************/
281 int list_remove_list(struct list *dest, const struct list *src)
283 struct list *cur = src->next;
288 if (cur->str != NULL)
290 count += list_remove_item(dest, cur->str);
299 /*********************************************************************
301 * Function : list_duplicate
303 * Description : Duplicate a string list
306 * 1 : dest = pointer to destination for copy. Caller allocs.
307 * 2 : src = pointer to source for copy.
311 *********************************************************************/
312 void list_duplicate(struct list *dest, const struct list *src)
314 struct list * cur_src = src->next;
315 struct list * cur_dest = dest;
317 memset(dest, '\0', sizeof(*dest));
321 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
322 if (cur_dest == NULL)
326 cur_dest->str = strdup(cur_src->str);
327 cur_src = cur_src->next;
330 dest->last = cur_dest;
335 /*********************************************************************
337 * Function : list_append_list_unique
339 * Description : Append a string list to another list
342 * 1 : dest = pointer to destination for merge. Caller allocs.
343 * 2 : src = pointer to source for merge.
347 *********************************************************************/
348 void list_append_list_unique(struct list *dest, const struct list *src)
350 struct list * cur = src->next;
354 enlist_unique(dest, cur->str);
360 /*********************************************************************
362 * Function : destroy_list_share
364 * Description : Destroy a string list (opposite of enlist)
367 * 1 : h = pointer to list 'dummy' header
371 *********************************************************************/
372 void destroy_list_share(struct list_share *h)
374 struct list_share *p, *n;
376 for (p = h->next; p ; p = n)
382 memset(h, '\0', sizeof(*h));
387 /*********************************************************************
389 * Function : enlist_share
391 * Description : Append a string into a specified string list.
394 * 1 : header = pointer to list 'dummy' header
395 * 2 : str = string to add to the list (maybe NULL)
399 *********************************************************************/
400 void enlist_share(struct list_share *header, const char *str)
402 struct list_share *cur = (struct list_share *)malloc(sizeof(*cur));
403 struct list_share *last;
407 cur->str = (str ? strdup(str) : NULL);
423 /*********************************************************************
425 * Function : enlist_unique_share
427 * Description : Append a string into a specified string list,
428 * if & only if it's not there already.
431 * 1 : header = pointer to list 'dummy' header
432 * 2 : str = string to add to the list (maybe NULL)
436 *********************************************************************/
437 void enlist_unique_share(struct list_share *header, const char *str)
439 struct list_share *last;
440 struct list_share *cur = header->next;
444 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
452 cur = (struct list_share *)malloc(sizeof(*cur));
470 /*********************************************************************
472 * Function : list_append_list_unique_share
474 * Description : Append a string list to another list
477 * 1 : dest = pointer to destination for merge. Caller allocs.
478 * 2 : src = pointer to source for merge.
482 *********************************************************************/
483 void list_append_list_unique_share(struct list_share *dest, const struct list *src)
485 struct list * cur = src->next;
489 enlist_unique_share(dest, cur->str);
495 /*********************************************************************
497 * Function : list_remove_item_share
499 * Description : Remove a string from a specified string list.
502 * 1 : header = pointer to list 'dummy' header
503 * 2 : str = string to remove from the list
505 * Returns : Number of times it was removed.
507 *********************************************************************/
508 int list_remove_item_share(struct list_share *header, const char *str)
510 struct list_share *prev = header;
511 struct list_share *cur = prev->next;
516 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
520 prev->next = cur->next;
536 /*********************************************************************
538 * Function : list_remove_list_share
540 * Description : Remove all strings in one list from another list.
541 * This is currently a brute-force algorithm
542 * (it compares every pair of strings).
545 * 1 : dest = list to change
546 * 2 : src = list of strings to remove
548 * Returns : Total number of strings removed.
550 *********************************************************************/
551 int list_remove_list_share(struct list_share *dest, const struct list *src)
553 struct list *cur = src->next;
558 if (cur->str != NULL)
560 count += list_remove_item_share(dest, cur->str);
569 /*********************************************************************
571 * Function : list_duplicate_share
573 * Description : Duplicate a string list
576 * 1 : dest = pointer to destination for copy. Caller allocs.
577 * 2 : src = pointer to source for copy.
581 *********************************************************************/
582 void list_duplicate_share(struct list_share *dest, const struct list *src)
584 struct list * cur_src = src->next;
585 struct list_share * cur_dest = dest;
587 memset(dest, '\0', sizeof(*dest));
591 cur_dest = cur_dest->next = (struct list_share *)zalloc(sizeof(*cur_dest));
592 if (cur_dest == NULL)
596 cur_dest->str = cur_src->str;
597 cur_src = cur_src->next;
600 dest->last = cur_dest;