1 const char list_rcs[] = "$Id: list.c,v 1.4 2001/06/29 13:30:22 oes 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.4 2001/06/29 13:30:22 oes
38 * - Added Convenience function enlist_unique_header(),
39 * which takes the Header name and value as separate
40 * arguments and thus saves the pain of sprintf()ing
41 * and determining the Header name length to enlist_unique
43 * - Removed logentry from cancelled commit
45 * Revision 1.3 2001/06/03 19:12:24 oes
46 * functions for new struct map, extended enlist_unique
48 * Revision 1.2 2001/06/01 18:49:17 jongfoster
49 * Replaced "list_share" with "list" - the tiny memory gain was not
50 * worth the extra complexity.
52 * Revision 1.1 2001/05/31 21:11:53 jongfoster
53 * - Moved linked list support to new "list.c" file.
54 * Structure definitions are still in project.h,
55 * function prototypes are now in "list.h".
56 * - Added support for "struct list_share", which is identical
57 * to "struct list" except it saves memory by not duplicating
58 * the strings. Obviously, this only works if there is some
59 * other way of managing the memory used by the strings.
60 * (These list_share lists are used for lists which last
61 * for only 1 request, and where all the list entries are
62 * just coming directly from entries in the actionsfile.)
63 * Note that you still need to destroy list_share lists
64 * properly to free the nodes - it's only the strings
68 *********************************************************************/
74 #include <sys/types.h>
88 const char list_h_rcs[] = LIST_H_VERSION;
90 /*********************************************************************
94 * Description : Append a string into a specified string list.
97 * 1 : header = pointer to list 'dummy' header
98 * 2 : str = string to add to the list (maybe NULL)
102 *********************************************************************/
103 void enlist(struct list *header, const char *str)
105 struct list *cur = (struct list *)malloc(sizeof(*cur));
110 cur->str = (str ? strdup(str) : NULL);
126 /*********************************************************************
128 * Function : enlist_first
130 * Description : Append a string as first element into a specified
134 * 1 : header = pointer to list 'dummy' header
135 * 2 : str = string to add to the list (maybe NULL)
139 *********************************************************************/
140 void enlist_first(struct list *header, const char *str)
142 struct list *cur = (struct list *)malloc(sizeof(*cur));
146 cur->str = (str ? strdup(str) : NULL);
147 cur->next = header->next;
150 if (header->last == NULL)
159 /*********************************************************************
161 * Function : enlist_unique
163 * Description : Append a string into a specified string list,
164 * if & only if it's not there already.
165 * If the n argument is nonzero, only compare up to
169 * 1 : header = pointer to list 'dummy' header
170 * 2 : str = string to add to the list (maybe NULL)
171 * 3 : n = number of chars to use for uniqueness test
175 *********************************************************************/
176 void enlist_unique(struct list *header, const char *str, int n)
179 struct list *cur = header->next;
183 if ((cur->str != NULL) && (
184 (n && (0 == strncmp(str, cur->str, n))) ||
185 (!n && (0 == strcmp(str, cur->str)))))
193 cur = (struct list *)malloc(sizeof(*cur));
197 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
211 /*********************************************************************
213 * Function : enlist_unique_header
215 * Description : Make a HTTP header from the two strings name and value,
216 * and append the result into a specified string list,
217 * if & only if there isn't already a header with that name.
220 * 1 : header = pointer to list 'dummy' header
221 * 2 : first = first string to add to the list (maybe NULL)
222 * 3 : second = number of chars to use for uniqueness test
226 *********************************************************************/
227 void enlist_unique_header(struct list *header, const char *name, const char *value)
230 struct list *cur = header->next;
234 if (name == NULL || value == NULL) return;
236 dummy = strdup(name);
237 dummy = strsav(dummy, ": ");
238 length = strlen(dummy);
242 if ((cur->str != NULL) &&
243 (0 == strncmp(dummy, cur->str, length)))
251 cur = (struct list *)malloc(sizeof(*cur));
255 cur->str = strsav(dummy, value);
269 /*********************************************************************
271 * Function : destroy_list
273 * Description : Destroy a string list (opposite of enlist)
276 * 1 : header = pointer to list 'dummy' header
280 *********************************************************************/
281 void destroy_list(struct list *header)
285 for (p = header->next; p ; p = n)
292 memset(header, '\0', sizeof(*header));
297 /*********************************************************************
299 * Function : list_to_text
301 * Description : "Flaten" a string list into 1 long \r\n delimited string.
304 * 1 : h = pointer to list 'dummy' header
306 * Returns : NULL on malloc error, else new long string.
308 *********************************************************************/
309 char *list_to_text(struct list *h)
318 for (p = h->next; p ; p = p->next)
322 size += strlen(p->str) + 2;
326 if ((ret = (char *)malloc(size + 1)) == NULL)
335 for (p = h->next; p ; p = p->next)
341 *s++ = '\r'; *s++ = '\n';
350 /*********************************************************************
352 * Function : list_remove_item
354 * Description : Remove a string from a specified string list.
357 * 1 : header = pointer to list 'dummy' header
358 * 2 : str = string to remove from the list
360 * Returns : Number of times it was removed.
362 *********************************************************************/
363 int list_remove_item(struct list *header, const char *str)
365 struct list *prev = header;
366 struct list *cur = prev->next;
371 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
375 prev->next = cur->next;
392 /*********************************************************************
394 * Function : list_remove_list
396 * Description : Remove all strings in one list from another list.
397 * This is currently a brute-force algorithm
398 * (it compares every pair of strings).
401 * 1 : dest = list to change
402 * 2 : src = list of strings to remove
404 * Returns : Total number of strings removed.
406 *********************************************************************/
407 int list_remove_list(struct list *dest, const struct list *src)
409 struct list *cur = src->next;
414 if (cur->str != NULL)
416 count += list_remove_item(dest, cur->str);
425 /*********************************************************************
427 * Function : list_duplicate
429 * Description : Duplicate a string list
432 * 1 : dest = pointer to destination for copy. Caller allocs.
433 * 2 : src = pointer to source for copy.
437 *********************************************************************/
438 void list_duplicate(struct list *dest, const struct list *src)
440 struct list * cur_src = src->next;
441 struct list * cur_dest = dest;
443 memset(dest, '\0', sizeof(*dest));
447 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
448 if (cur_dest == NULL)
452 cur_dest->str = strdup(cur_src->str);
453 cur_src = cur_src->next;
456 dest->last = cur_dest;
461 /*********************************************************************
463 * Function : list_append_list_unique
465 * Description : Append a string list to another list.
466 * Duplicate items are not added.
469 * 1 : dest = pointer to destination for merge. Caller allocs.
470 * 2 : src = pointer to source for merge.
474 *********************************************************************/
475 void list_append_list_unique(struct list *dest, const struct list *src)
477 struct list * cur = src->next;
481 enlist_unique(dest, cur->str, 0);
487 /*********************************************************************
491 * Description : Add a mapping from given name to given value to a
494 * Note: Since all strings will be free()d in free_map()
495 * later, use the copy flags for constants or
496 * strings that will be independantly free()d.
499 * 1 : map = map to add to
500 * 2 : name = name to add
501 * 3 : nc = flag set if a copy of name should be used
502 * 4 : value = value to add
503 * 5 : vc = flag set if a copy of value should be used
505 * Returns : pointer to extended map, or NULL if failiure
507 *********************************************************************/
508 struct map *map(struct map *map, char *name, int nc, char *value, int vc)
512 if (NULL == (cur = zalloc(sizeof(*cur))))
517 cur->name = nc ? strdup(name) : name;
518 cur->value = vc ? strdup(value) : value;
526 /*********************************************************************
530 * Description : Look up an item with a given name in a map, and
534 * 1 : name = name parameter to look for
536 * Returns : the value if found, else the empty string
538 *********************************************************************/
539 char *lookup(struct map *map, char *name)
545 if (!strcmp(name, p->name))
556 /*********************************************************************
558 * Function : free_map
560 * Description : Free the memory occupied by a map and its
564 * 1 : list = list to bee freed
568 *********************************************************************/
569 void free_map(struct map *map)