1 const char list_rcs[] = "$Id: list.c,v 1.3 2001/06/03 11:03:48 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.3 2001/06/03 11:03:48 oes
44 * adapted to new enlist_unique arg format
48 * introduced confdir option
50 * filters.c filtrers.h
52 * extracted-CGI relevant stuff
60 * support for new cgi mechansim
64 * functions for new list type: "map"
65 * extended enlist_unique
72 * deleted const struct interceptors
80 * added struct http_response
81 * changes struct interceptors to struct cgi_dispatcher
82 * moved HTML stuff to cgi.h
91 * Revision 1.2 2001/06/01 18:49:17 jongfoster
92 * Replaced "list_share" with "list" - the tiny memory gain was not
93 * worth the extra complexity.
95 * Revision 1.1 2001/05/31 21:11:53 jongfoster
96 * - Moved linked list support to new "list.c" file.
97 * Structure definitions are still in project.h,
98 * function prototypes are now in "list.h".
99 * - Added support for "struct list_share", which is identical
100 * to "struct list" except it saves memory by not duplicating
101 * the strings. Obviously, this only works if there is some
102 * other way of managing the memory used by the strings.
103 * (These list_share lists are used for lists which last
104 * for only 1 request, and where all the list entries are
105 * just coming directly from entries in the actionsfile.)
106 * Note that you still need to destroy list_share lists
107 * properly to free the nodes - it's only the strings
111 *********************************************************************/
117 #include <sys/types.h>
129 #include "miscutil.h"
131 const char list_h_rcs[] = LIST_H_VERSION;
133 /*********************************************************************
137 * Description : Append a string into a specified string list.
140 * 1 : header = pointer to list 'dummy' header
141 * 2 : str = string to add to the list (maybe NULL)
145 *********************************************************************/
146 void enlist(struct list *header, const char *str)
148 struct list *cur = (struct list *)malloc(sizeof(*cur));
153 cur->str = (str ? strdup(str) : NULL);
169 /*********************************************************************
171 * Function : enlist_first
173 * Description : Append a string as first element into a specified
177 * 1 : header = pointer to list 'dummy' header
178 * 2 : str = string to add to the list (maybe NULL)
182 *********************************************************************/
183 void enlist_first(struct list *header, const char *str)
185 struct list *cur = (struct list *)malloc(sizeof(*cur));
189 cur->str = (str ? strdup(str) : NULL);
190 cur->next = header->next;
193 if (header->last == NULL)
202 /*********************************************************************
204 * Function : enlist_unique
206 * Description : Append a string into a specified string list,
207 * if & only if it's not there already.
208 * If the n argument is nonzero, only compare up to
212 * 1 : header = pointer to list 'dummy' header
213 * 2 : str = string to add to the list (maybe NULL)
214 * 3 : n = number of chars to use for uniqueness test
218 *********************************************************************/
219 void enlist_unique(struct list *header, const char *str, int n)
222 struct list *cur = header->next;
226 if ((cur->str != NULL) && (
227 (n && (0 == strncmp(str, cur->str, n))) ||
228 (!n && (0 == strcmp(str, cur->str)))))
236 cur = (struct list *)malloc(sizeof(*cur));
240 cur->str = (str ? strdup(str) : NULL); /* FIXME check retval */
254 /*********************************************************************
256 * Function : destroy_list
258 * Description : Destroy a string list (opposite of enlist)
261 * 1 : header = pointer to list 'dummy' header
265 *********************************************************************/
266 void destroy_list(struct list *header)
270 for (p = header->next; p ; p = n)
277 memset(header, '\0', sizeof(*header));
282 /*********************************************************************
284 * Function : list_to_text
286 * Description : "Flaten" a string list into 1 long \r\n delimited string.
289 * 1 : h = pointer to list 'dummy' header
291 * Returns : NULL on malloc error, else new long string.
293 *********************************************************************/
294 char *list_to_text(struct list *h)
303 for (p = h->next; p ; p = p->next)
307 size += strlen(p->str) + 2;
311 if ((ret = (char *)malloc(size + 1)) == NULL)
320 for (p = h->next; p ; p = p->next)
326 *s++ = '\r'; *s++ = '\n';
335 /*********************************************************************
337 * Function : list_remove_item
339 * Description : Remove a string from a specified string list.
342 * 1 : header = pointer to list 'dummy' header
343 * 2 : str = string to remove from the list
345 * Returns : Number of times it was removed.
347 *********************************************************************/
348 int list_remove_item(struct list *header, const char *str)
350 struct list *prev = header;
351 struct list *cur = prev->next;
356 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
360 prev->next = cur->next;
377 /*********************************************************************
379 * Function : list_remove_list
381 * Description : Remove all strings in one list from another list.
382 * This is currently a brute-force algorithm
383 * (it compares every pair of strings).
386 * 1 : dest = list to change
387 * 2 : src = list of strings to remove
389 * Returns : Total number of strings removed.
391 *********************************************************************/
392 int list_remove_list(struct list *dest, const struct list *src)
394 struct list *cur = src->next;
399 if (cur->str != NULL)
401 count += list_remove_item(dest, cur->str);
410 /*********************************************************************
412 * Function : list_duplicate
414 * Description : Duplicate a string list
417 * 1 : dest = pointer to destination for copy. Caller allocs.
418 * 2 : src = pointer to source for copy.
422 *********************************************************************/
423 void list_duplicate(struct list *dest, const struct list *src)
425 struct list * cur_src = src->next;
426 struct list * cur_dest = dest;
428 memset(dest, '\0', sizeof(*dest));
432 cur_dest = cur_dest->next = (struct list *)zalloc(sizeof(*cur_dest));
433 if (cur_dest == NULL)
437 cur_dest->str = strdup(cur_src->str);
438 cur_src = cur_src->next;
441 dest->last = cur_dest;
446 /*********************************************************************
448 * Function : list_append_list_unique
450 * Description : Append a string list to another list.
451 * Duplicate items are not added.
454 * 1 : dest = pointer to destination for merge. Caller allocs.
455 * 2 : src = pointer to source for merge.
459 *********************************************************************/
460 void list_append_list_unique(struct list *dest, const struct list *src)
462 struct list * cur = src->next;
466 enlist_unique(dest, cur->str, 0);
472 /*********************************************************************
476 * Description : Add a mapping from given name to given value to a
480 * 1 : map = map to add to
481 * 2 : name = name to add
482 * 3 : nc = flag set if name is string constant, and
483 * must be strdup()d, so it can later be free()d (FIXME!)
484 * 4 : value = value to add
485 * 5 : vc = flag set if value is string constant
487 * Returns : pointer to extended map, or NULL if failiure
489 *********************************************************************/
490 struct map *map(struct map *map, char *name, int nc, char *value, int vc)
494 if (NULL == (cur = zalloc(sizeof(*cur))))
499 cur->name = nc ? strdup(name) : name;
500 cur->value = vc ? strdup(value) : value;
508 /*********************************************************************
512 * Description : Look up an item with a given name in a map, and
516 * 1 : name = name parameter to look for
518 * Returns : the value if found, else the empty string
520 *********************************************************************/
521 char *lookup(struct map *map, char *name)
527 if (!strcmp(name, p->name))
537 /*********************************************************************
539 * Function : free_map
541 * Description : Free the memory occupied by a map and its
545 * 1 : list = list to bee freed
549 *********************************************************************/
550 void free_map(struct map *map)