1 const char list_rcs[] = "$Id: list.c,v 1.6 2001/07/31 14:44:51 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.6 2001/07/31 14:44:51 oes
38 * list_to_text() now appends empty line at end
40 * Revision 1.5 2001/06/29 21:45:41 oes
41 * Indentation, CRLF->LF, Tab-> Space
43 * Revision 1.4 2001/06/29 13:30:22 oes
44 * - Added Convenience function enlist_unique_header(),
45 * which takes the Header name and value as separate
46 * arguments and thus saves the pain of sprintf()ing
47 * and determining the Header name length to enlist_unique
49 * - Removed logentry from cancelled commit
51 * Revision 1.3 2001/06/03 19:12:24 oes
52 * functions for new struct map, extended enlist_unique
54 * Revision 1.2 2001/06/01 18:49:17 jongfoster
55 * Replaced "list_share" with "list" - the tiny memory gain was not
56 * worth the extra complexity.
58 * Revision 1.1 2001/05/31 21:11:53 jongfoster
59 * - Moved linked list support to new "list.c" file.
60 * Structure definitions are still in project.h,
61 * function prototypes are now in "list.h".
62 * - Added support for "struct list_share", which is identical
63 * to "struct list" except it saves memory by not duplicating
64 * the strings. Obviously, this only works if there is some
65 * other way of managing the memory used by the strings.
66 * (These list_share lists are used for lists which last
67 * for only 1 request, and where all the list entries are
68 * just coming directly from entries in the actionsfile.)
69 * Note that you still need to destroy list_share lists
70 * properly to free the nodes - it's only the strings
74 *********************************************************************/
80 #include <sys/types.h>
96 const char list_h_rcs[] = LIST_H_VERSION;
98 /*********************************************************************
102 * Description : Append a string into a specified string list.
105 * 1 : header = pointer to list 'dummy' header
106 * 2 : str = string to add to the list (maybe NULL)
110 *********************************************************************/
111 void enlist(struct list *header, const char *str)
113 struct list *cur = (struct list *)malloc(sizeof(*cur));
118 cur->str = (str ? strdup(str) : NULL);
134 /*********************************************************************
136 * Function : enlist_first
138 * Description : Append a string as first element into a specified
142 * 1 : header = pointer to list 'dummy' header
143 * 2 : str = string to add to the list (maybe NULL)
147 *********************************************************************/
148 void enlist_first(struct list *header, const char *str)
150 struct list *cur = (struct list *)malloc(sizeof(*cur));
154 cur->str = (str ? strdup(str) : NULL);
155 cur->next = header->next;
158 if (header->last == NULL)
167 /*********************************************************************
169 * Function : enlist_unique
171 * Description : Append a string into a specified string list,
172 * if & only if it's not there already.
173 * If the n argument is nonzero, only compare up to
177 * 1 : header = pointer to list 'dummy' header
178 * 2 : str = string to add to the list (maybe NULL)
179 * 3 : n = number of chars to use for uniqueness test
183 *********************************************************************/
184 void enlist_unique(struct list *header, const char *str, int n)
187 struct list *cur = header->next;
191 if ((cur->str != NULL) && (
192 (n && (0 == strncmp(str, cur->str, n))) ||
193 (!n && (0 == strcmp(str, cur->str)))))
201 cur = (struct list *)malloc(sizeof(*cur));
205 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
219 /*********************************************************************
221 * Function : enlist_unique_header
223 * Description : Make a HTTP header from the two strings name and value,
224 * and append the result into a specified string list,
225 * if & only if there isn't already a header with that name.
228 * 1 : header = pointer to list 'dummy' header
229 * 2 : first = first string to add to the list (maybe NULL)
230 * 3 : second = number of chars to use for uniqueness test
234 *********************************************************************/
235 void enlist_unique_header(struct list *header, const char *name, const char *value)
238 struct list *cur = header->next;
242 if (name == NULL || value == NULL) return;
244 dummy = strdup(name);
245 dummy = strsav(dummy, ": ");
246 length = strlen(dummy);
250 if ((cur->str != NULL) &&
251 (0 == strncmp(dummy, cur->str, length)))
259 cur = (struct list *)malloc(sizeof(*cur));
263 cur->str = strsav(dummy, value);
277 /*********************************************************************
279 * Function : destroy_list
281 * Description : Destroy a string list (opposite of enlist)
284 * 1 : header = pointer to list 'dummy' header
288 *********************************************************************/
289 void destroy_list(struct list *header)
293 for (p = header->next; p ; p = n)
300 memset(header, '\0', sizeof(*header));
305 /*********************************************************************
307 * Function : list_to_text
309 * Description : "Flaten" a string list into 1 long \r\n delimited string,
310 * adding an empty line at the end.
313 * 1 : h = pointer to list 'dummy' header
315 * Returns : NULL on malloc error, else new long string.
317 *********************************************************************/
318 char *list_to_text(struct list *h)
325 for (p = h->next; p ; p = p->next)
329 size += strlen(p->str) + 2;
333 if ((ret = (char *)malloc(size + 1)) == NULL)
342 for (p = h->next; p ; p = p->next)
348 *s++ = '\r'; *s++ = '\n';
351 *s++ = '\r'; *s++ = '\n';
358 /*********************************************************************
360 * Function : list_remove_item
362 * Description : Remove a string from a specified string list.
365 * 1 : header = pointer to list 'dummy' header
366 * 2 : str = string to remove from the list
368 * Returns : Number of times it was removed.
370 *********************************************************************/
371 int list_remove_item(struct list *header, const char *str)
373 struct list *prev = header;
374 struct list *cur = prev->next;
379 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
383 prev->next = cur->next;
400 /*********************************************************************
402 * Function : list_remove_list
404 * Description : Remove all strings in one list from another list.
405 * This is currently a brute-force algorithm
406 * (it compares every pair of strings).
409 * 1 : dest = list to change
410 * 2 : src = list of strings to remove
412 * Returns : Total number of strings removed.
414 *********************************************************************/
415 int list_remove_list(struct list *dest, const struct list *src)
417 struct list *cur = src->next;
422 if (cur->str != NULL)
424 count += list_remove_item(dest, cur->str);
433 /*********************************************************************
435 * Function : list_duplicate
437 * Description : Duplicate a string list
440 * 1 : dest = pointer to destination for copy. Caller allocs.
441 * 2 : src = pointer to source for copy.
445 *********************************************************************/
446 void list_duplicate(struct list *dest, const struct list *src)
448 struct list * cur_src = src->next;
449 struct list * cur_dest = dest;
451 memset(dest, '\0', sizeof(*dest));
455 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
456 if (cur_dest == NULL)
460 cur_dest->str = strdup(cur_src->str);
461 cur_src = cur_src->next;
464 dest->last = cur_dest;
469 /*********************************************************************
471 * Function : list_append_list_unique
473 * Description : Append a string list to another list.
474 * Duplicate items are not added.
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(struct list *dest, const struct list *src)
485 struct list * cur = src->next;
489 enlist_unique(dest, cur->str, 0);
495 /*********************************************************************
499 * Description : Add a mapping from given name to given value to a
502 * Note: Since all strings will be free()d in free_map()
503 * later, use the copy flags for constants or
504 * strings that will be independantly free()d.
507 * 1 : the_map = map to add to
508 * 2 : name = name to add
509 * 3 : nc = flag set if a copy of name should be used
510 * 4 : value = value to add
511 * 5 : vc = flag set if a copy of value should be used
515 *********************************************************************/
516 void map(struct map *the_map, const char *name, int nc, const char *value, int vc)
518 struct map_entry *new_entry;
520 if (NULL == (new_entry = zalloc(sizeof(*new_entry))))
525 new_entry->name = nc ? strdup(name) : name;
526 new_entry->value = vc ? strdup(value) : value;
527 /* new_entry->next = NULL; - implied by zalloc */
531 the_map->last = the_map->last->next = new_entry;
535 the_map->last = the_map->first = new_entry;
540 /*********************************************************************
544 * Description : Look up an item with a given name in a map, and
548 * 1 : name = name parameter to look for
550 * Returns : the value if found, else the empty string
552 *********************************************************************/
553 const char *lookup(const struct map *the_map, const char *name)
555 const struct map_entry *cur_entry = the_map->first;
559 if (!strcmp(name, cur_entry->name))
561 return cur_entry->value;
563 cur_entry = cur_entry->next;
569 /*********************************************************************
573 * Description : Create a new, empty map.
577 * Returns : A new, empty map, or NULL if out of memory.
579 *********************************************************************/
580 struct map *new_map(void)
582 return (struct map *) zalloc(sizeof(struct map));
586 /*********************************************************************
588 * Function : free_map
590 * Description : Free the memory occupied by a map and its
594 * 1 : cur_entry = map to be freed. May be NULL.
598 *********************************************************************/
599 void free_map(struct map *the_map)
601 struct map_entry *cur_entry;
602 struct map_entry *next_entry;
609 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
611 freez((char *)cur_entry->name);
612 freez((char *)cur_entry->value);
614 next_entry = cur_entry->next;
618 the_map->first = the_map->last = NULL;