1 const char list_rcs[] = "$Id: list.c,v 1.5 2001/06/29 21:45:41 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.5 2001/06/29 21:45:41 oes
38 * Indentation, CRLF->LF, Tab-> Space
40 * Revision 1.4 2001/06/29 13:30:22 oes
41 * - Added Convenience function enlist_unique_header(),
42 * which takes the Header name and value as separate
43 * arguments and thus saves the pain of sprintf()ing
44 * and determining the Header name length to enlist_unique
46 * - Removed logentry from cancelled commit
48 * Revision 1.3 2001/06/03 19:12:24 oes
49 * functions for new struct map, extended enlist_unique
51 * Revision 1.2 2001/06/01 18:49:17 jongfoster
52 * Replaced "list_share" with "list" - the tiny memory gain was not
53 * worth the extra complexity.
55 * Revision 1.1 2001/05/31 21:11:53 jongfoster
56 * - Moved linked list support to new "list.c" file.
57 * Structure definitions are still in project.h,
58 * function prototypes are now in "list.h".
59 * - Added support for "struct list_share", which is identical
60 * to "struct list" except it saves memory by not duplicating
61 * the strings. Obviously, this only works if there is some
62 * other way of managing the memory used by the strings.
63 * (These list_share lists are used for lists which last
64 * for only 1 request, and where all the list entries are
65 * just coming directly from entries in the actionsfile.)
66 * Note that you still need to destroy list_share lists
67 * properly to free the nodes - it's only the strings
71 *********************************************************************/
77 #include <sys/types.h>
91 const char list_h_rcs[] = LIST_H_VERSION;
93 /*********************************************************************
97 * Description : Append a string into a specified string list.
100 * 1 : header = pointer to list 'dummy' header
101 * 2 : str = string to add to the list (maybe NULL)
105 *********************************************************************/
106 void enlist(struct list *header, const char *str)
108 struct list *cur = (struct list *)malloc(sizeof(*cur));
113 cur->str = (str ? strdup(str) : NULL);
129 /*********************************************************************
131 * Function : enlist_first
133 * Description : Append a string as first element into a specified
137 * 1 : header = pointer to list 'dummy' header
138 * 2 : str = string to add to the list (maybe NULL)
142 *********************************************************************/
143 void enlist_first(struct list *header, const char *str)
145 struct list *cur = (struct list *)malloc(sizeof(*cur));
149 cur->str = (str ? strdup(str) : NULL);
150 cur->next = header->next;
153 if (header->last == NULL)
162 /*********************************************************************
164 * Function : enlist_unique
166 * Description : Append a string into a specified string list,
167 * if & only if it's not there already.
168 * If the n argument is nonzero, only compare up to
172 * 1 : header = pointer to list 'dummy' header
173 * 2 : str = string to add to the list (maybe NULL)
174 * 3 : n = number of chars to use for uniqueness test
178 *********************************************************************/
179 void enlist_unique(struct list *header, const char *str, int n)
182 struct list *cur = header->next;
186 if ((cur->str != NULL) && (
187 (n && (0 == strncmp(str, cur->str, n))) ||
188 (!n && (0 == strcmp(str, cur->str)))))
196 cur = (struct list *)malloc(sizeof(*cur));
200 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
214 /*********************************************************************
216 * Function : enlist_unique_header
218 * Description : Make a HTTP header from the two strings name and value,
219 * and append the result into a specified string list,
220 * if & only if there isn't already a header with that name.
223 * 1 : header = pointer to list 'dummy' header
224 * 2 : first = first string to add to the list (maybe NULL)
225 * 3 : second = number of chars to use for uniqueness test
229 *********************************************************************/
230 void enlist_unique_header(struct list *header, const char *name, const char *value)
233 struct list *cur = header->next;
237 if (name == NULL || value == NULL) return;
239 dummy = strdup(name);
240 dummy = strsav(dummy, ": ");
241 length = strlen(dummy);
245 if ((cur->str != NULL) &&
246 (0 == strncmp(dummy, cur->str, length)))
254 cur = (struct list *)malloc(sizeof(*cur));
258 cur->str = strsav(dummy, value);
272 /*********************************************************************
274 * Function : destroy_list
276 * Description : Destroy a string list (opposite of enlist)
279 * 1 : header = pointer to list 'dummy' header
283 *********************************************************************/
284 void destroy_list(struct list *header)
288 for (p = header->next; p ; p = n)
295 memset(header, '\0', sizeof(*header));
300 /*********************************************************************
302 * Function : list_to_text
304 * Description : "Flaten" a string list into 1 long \r\n delimited string,
305 * adding an empty line at the end.
308 * 1 : h = pointer to list 'dummy' header
310 * Returns : NULL on malloc error, else new long string.
312 *********************************************************************/
313 char *list_to_text(struct list *h)
320 for (p = h->next; p ; p = p->next)
324 size += strlen(p->str) + 2;
328 if ((ret = (char *)malloc(size + 1)) == NULL)
337 for (p = h->next; p ; p = p->next)
343 *s++ = '\r'; *s++ = '\n';
346 *s++ = '\r'; *s++ = '\n';
353 /*********************************************************************
355 * Function : list_remove_item
357 * Description : Remove a string from a specified string list.
360 * 1 : header = pointer to list 'dummy' header
361 * 2 : str = string to remove from the list
363 * Returns : Number of times it was removed.
365 *********************************************************************/
366 int list_remove_item(struct list *header, const char *str)
368 struct list *prev = header;
369 struct list *cur = prev->next;
374 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
378 prev->next = cur->next;
395 /*********************************************************************
397 * Function : list_remove_list
399 * Description : Remove all strings in one list from another list.
400 * This is currently a brute-force algorithm
401 * (it compares every pair of strings).
404 * 1 : dest = list to change
405 * 2 : src = list of strings to remove
407 * Returns : Total number of strings removed.
409 *********************************************************************/
410 int list_remove_list(struct list *dest, const struct list *src)
412 struct list *cur = src->next;
417 if (cur->str != NULL)
419 count += list_remove_item(dest, cur->str);
428 /*********************************************************************
430 * Function : list_duplicate
432 * Description : Duplicate a string list
435 * 1 : dest = pointer to destination for copy. Caller allocs.
436 * 2 : src = pointer to source for copy.
440 *********************************************************************/
441 void list_duplicate(struct list *dest, const struct list *src)
443 struct list * cur_src = src->next;
444 struct list * cur_dest = dest;
446 memset(dest, '\0', sizeof(*dest));
450 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
451 if (cur_dest == NULL)
455 cur_dest->str = strdup(cur_src->str);
456 cur_src = cur_src->next;
459 dest->last = cur_dest;
464 /*********************************************************************
466 * Function : list_append_list_unique
468 * Description : Append a string list to another list.
469 * Duplicate items are not added.
472 * 1 : dest = pointer to destination for merge. Caller allocs.
473 * 2 : src = pointer to source for merge.
477 *********************************************************************/
478 void list_append_list_unique(struct list *dest, const struct list *src)
480 struct list * cur = src->next;
484 enlist_unique(dest, cur->str, 0);
490 /*********************************************************************
494 * Description : Add a mapping from given name to given value to a
497 * Note: Since all strings will be free()d in free_map()
498 * later, use the copy flags for constants or
499 * strings that will be independantly free()d.
502 * 1 : map = map to add to
503 * 2 : name = name to add
504 * 3 : nc = flag set if a copy of name should be used
505 * 4 : value = value to add
506 * 5 : vc = flag set if a copy of value should be used
508 * Returns : pointer to extended map, or NULL if failiure
510 *********************************************************************/
511 struct map *map(struct map *map, char *name, int nc, char *value, int vc)
515 if (NULL == (cur = zalloc(sizeof(*cur))))
520 cur->name = nc ? strdup(name) : name;
521 cur->value = vc ? strdup(value) : value;
529 /*********************************************************************
533 * Description : Look up an item with a given name in a map, and
537 * 1 : name = name parameter to look for
539 * Returns : the value if found, else the empty string
541 *********************************************************************/
542 char *lookup(struct map *map, char *name)
548 if (!strcmp(name, p->name))
559 /*********************************************************************
561 * Function : free_map
563 * Description : Free the memory occupied by a map and its
567 * 1 : list = list to bee freed
571 *********************************************************************/
572 void free_map(struct map *map)