1 const char list_rcs[] = "$Id: list.c,v 1.8 2001/08/07 14:00:20 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.8 2001/08/07 14:00:20 oes
40 * Revision 1.7 2001/08/05 16:06:20 jongfoster
41 * Modifiying "struct map" so that there are now separate header and
42 * "map_entry" structures. This means that functions which modify a
43 * map no longer need to return a pointer to the modified map.
44 * Also, it no longer reverses the order of the entries (which may be
45 * important with some advanced template substitutions).
47 * Revision 1.6 2001/07/31 14:44:51 oes
48 * list_to_text() now appends empty line at end
50 * Revision 1.5 2001/06/29 21:45:41 oes
51 * Indentation, CRLF->LF, Tab-> Space
53 * Revision 1.4 2001/06/29 13:30:22 oes
54 * - Added Convenience function enlist_unique_header(),
55 * which takes the Header name and value as separate
56 * arguments and thus saves the pain of sprintf()ing
57 * and determining the Header name length to enlist_unique
59 * - Removed logentry from cancelled commit
61 * Revision 1.3 2001/06/03 19:12:24 oes
62 * functions for new struct map, extended enlist_unique
64 * Revision 1.2 2001/06/01 18:49:17 jongfoster
65 * Replaced "list_share" with "list" - the tiny memory gain was not
66 * worth the extra complexity.
68 * Revision 1.1 2001/05/31 21:11:53 jongfoster
69 * - Moved linked list support to new "list.c" file.
70 * Structure definitions are still in project.h,
71 * function prototypes are now in "list.h".
72 * - Added support for "struct list_share", which is identical
73 * to "struct list" except it saves memory by not duplicating
74 * the strings. Obviously, this only works if there is some
75 * other way of managing the memory used by the strings.
76 * (These list_share lists are used for lists which last
77 * for only 1 request, and where all the list entries are
78 * just coming directly from entries in the actionsfile.)
79 * Note that you still need to destroy list_share lists
80 * properly to free the nodes - it's only the strings
84 *********************************************************************/
90 #include <sys/types.h>
104 #include "miscutil.h"
106 const char list_h_rcs[] = LIST_H_VERSION;
109 /*********************************************************************
111 * Function : list_init
113 * Description : Create a new, empty list in user-allocated memory.
114 * Caller should allocate a "struct list" variable,
115 * then pass it to this function.
116 * (Implementation note: Rather than calling this
117 * function, you can also just memset the memory to
118 * zero, e.g. if you have a larger structure you
119 * want to initialize quickly. However, that isn't
120 * really good design.)
123 * 1 : the_list = pointer to list
127 *********************************************************************/
128 void init_list(struct list *the_list)
130 memset(the_list, '\0', sizeof(*the_list));
134 /*********************************************************************
136 * Function : destroy_list
138 * Description : Destroy a string list (opposite of list_init).
139 * On return, the memory used by the list entries has
140 * been freed, but not the memory used by the_list
141 * itself. You should not re-use the_list without
142 * calling list_init().
144 * (Implementation note: You *can* reuse the_list
145 * without calling list_init(), but please don't.
146 * If you want to remove all entries from a list
147 * and still have a usable list, then use
148 * list_remove_all().)
151 * 1 : the_list = pointer to list
155 *********************************************************************/
156 void destroy_list (struct list *the_list)
158 struct list_entry *cur_entry, *next_entry;
162 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
164 next_entry = cur_entry->next;
165 freez((char *)cur_entry->str);
169 the_list->first = NULL;
170 the_list->last = NULL;
174 /*********************************************************************
176 * Function : list_is_valid
178 * Description : Check that a string list is valid. The intended
179 * usage is "assert(list_is_valid(the_list))".
180 * Currently this checks that "the_list->last"
181 * is correct, and that the list dosn't contain
182 * circular references. It is likely to crash if
183 * it's passed complete garbage.
186 * 1 : the_list = pointer to list. Must be non-null.
188 * Returns : 1 if list is valid, 0 otherwise.
190 *********************************************************************/
191 static int list_is_valid (const struct list *the_list)
194 * If you don't want this check, just change the line below
195 * from "#if 1" to "#if 0".
198 const struct list_entry *cur_entry;
199 const struct list_entry *last_entry = NULL;
204 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
206 last_entry = cur_entry;
211 * Just check that this string can be accessed - i.e. it's a valid
214 strlen(cur_entry->str);
218 * Check for looping back to first
220 if ((length != 0) && (cur_entry == the_list->first))
226 * Arbitrarily limit length to prevent infinite loops.
234 * Check this isn't marked as the last entry, unless of course it's
235 * *really* the last entry.
237 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
239 /* This is the last entry, but there's data after it !!?? */
244 return (the_list->last == last_entry);
250 /*********************************************************************
254 * Description : Append a string into a specified string list.
257 * 1 : the_list = pointer to list
258 * 2 : str = string to add to the list (maybe NULL)
260 * Returns : 0 on success, nonzero on out-of-memory error. On
261 * error, the_list will be unchanged.
263 *********************************************************************/
264 int enlist(struct list *the_list, const char *str)
266 struct list_entry *cur;
269 assert(list_is_valid(the_list));
271 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
278 if (NULL == (cur->str = strdup(str)))
284 /* else { cur->str = NULL; } - implied by zalloc */
286 /* cur->next = NULL; - implied by zalloc */
290 the_list->last->next = cur;
291 the_list->last = cur;
295 the_list->first = cur;
296 the_list->last = cur;
299 assert(list_is_valid(the_list));
304 /*********************************************************************
306 * Function : enlist_first
308 * Description : Append a string as first element into a specified
312 * 1 : the_list = pointer to list
313 * 2 : str = string to add to the list (maybe NULL)
315 * Returns : 0 on success, nonzero on out-of-memory error. On
316 * error, the_list will be unchanged.
318 *********************************************************************/
319 int enlist_first(struct list *the_list, const char *str)
321 struct list_entry *cur;
324 assert(list_is_valid(the_list));
326 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
333 if (NULL == (cur->str = strdup(str)))
339 /* else { cur->str = NULL; } - implied by zalloc */
341 cur->next = the_list->first;
343 the_list->first = cur;
344 if (the_list->last == NULL)
346 the_list->last = cur;
349 assert(list_is_valid(the_list));
354 /*********************************************************************
356 * Function : enlist_unique
358 * Description : Append a string into a specified string list,
359 * if & only if it's not there already.
360 * If the num_significant_chars argument is nonzero,
361 * only compare up to the nth character.
364 * 1 : the_list = pointer to list
365 * 2 : str = string to add to the list
366 * 3 : num_significant_chars = number of chars to use
367 * for uniqueness test, or 0 to require an exact match.
369 * Returns : 0 on success, nonzero on out-of-memory error. On
370 * error, the_list will be unchanged. "Success"
371 * does not indicate whether or not the item was
372 * already in the list.
374 *********************************************************************/
375 int enlist_unique(struct list *the_list, const char *str,
376 int num_significant_chars)
378 struct list_entry *cur_entry;
381 assert(list_is_valid(the_list));
383 assert(num_significant_chars >= 0);
384 assert((size_t)num_significant_chars <= strlen(str));
386 if (num_significant_chars > 0)
388 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
390 if ( (cur_entry->str != NULL)
391 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
400 /* Test whole string */
401 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
403 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
411 return enlist(the_list, str);
415 /*********************************************************************
417 * Function : enlist_unique_header
419 * Description : Make a HTTP header from the two strings name and value,
420 * and append the result into a specified string list,
421 * if & only if there isn't already a header with that name.
424 * 1 : the_list = pointer to list
425 * 2 : name = HTTP header name (e.g. "Content-type")
426 * 3 : value = HTTP header value (e.g. "text/html")
428 * Returns : 0 on success, nonzero on out-of-memory error. On
429 * error, the_list will be unchanged. "Success"
430 * does not indicate whether or not the header was
431 * already in the list.
433 *********************************************************************/
434 int enlist_unique_header(struct list *the_list, const char *name, const char *value)
441 assert(list_is_valid(the_list));
445 length = strlen(name) + 2;
446 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
451 str[length - 2] = ':';
452 str[length - 1] = ' ';
453 strcpy(str + length, value);
455 result = enlist_unique(the_list, str, length);
459 assert(list_is_valid(the_list));
465 /*********************************************************************
467 * Function : list_remove_all
469 * Description : Remove all entries from a list. On return, the_list
470 * is a valid, empty list. Note that this is similar
471 * to destroy_list(), but the difference is that this
472 * function guarantees that the list structure is still
473 * valid after the call.
476 * 1 : the_list = pointer to list
480 *********************************************************************/
481 void list_remove_all(struct list *the_list)
483 struct list_entry *cur_entry;
484 struct list_entry *next_entry;
487 assert(list_is_valid(the_list));
489 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
491 next_entry = cur_entry->next;
492 freez((char *)cur_entry->str);
496 the_list->first = the_list->last = NULL;
498 assert(list_is_valid(the_list));
502 /*********************************************************************
504 * Function : list_to_text
506 * Description : "Flatten" a string list into 1 long \r\n delimited string,
507 * adding an empty line at the end. NULL entries are ignored.
508 * This function does not change the_list.
511 * 1 : the_list = pointer to list
513 * Returns : NULL on malloc error, else new long string.
514 * Caller must free() it.
516 *********************************************************************/
517 char *list_to_text(const struct list *the_list)
519 struct list_entry *cur_entry;
525 assert(list_is_valid(the_list));
527 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
531 size += strlen(cur_entry->str) + 2;
535 if ((ret = (char *)malloc(size + 1)) == NULL)
544 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
548 strcpy(s, cur_entry->str);
550 *s++ = '\r'; *s++ = '\n';
553 *s++ = '\r'; *s++ = '\n';
559 /*********************************************************************
561 * Function : list_remove_item
563 * Description : Remove a string from a specified string list.
566 * 1 : the_list = pointer to list
567 * 2 : str = string to remove from the list - non-NULL
569 * Returns : Number of times it was removed.
571 *********************************************************************/
572 int list_remove_item(struct list *the_list, const char *str)
574 struct list_entry *prev = NULL;
575 struct list_entry *cur;
576 struct list_entry *next;
580 assert(list_is_valid(the_list));
583 cur = the_list->first;
589 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
599 the_list->first = next;
601 free((char *)cur->str);
611 the_list->last = prev;
613 assert(list_is_valid(the_list));
619 /*********************************************************************
621 * Function : list_remove_list
623 * Description : Remove all strings in one list from another list.
624 * This is currently a brute-force algorithm
625 * (it compares every pair of strings).
628 * 1 : dest = list to change
629 * 2 : src = list of strings to remove
631 * Returns : Total number of strings removed.
633 *********************************************************************/
634 int list_remove_list(struct list *dest, const struct list *src)
636 struct list_entry *cur;
641 assert(list_is_valid(src));
642 assert(list_is_valid(dest));
644 for (cur = src->first; cur != NULL; cur = cur->next)
646 if (cur->str != NULL)
648 count += list_remove_item(dest, cur->str);
652 assert(list_is_valid(src));
653 assert(list_is_valid(dest));
659 /*********************************************************************
661 * Function : list_duplicate
663 * Description : Copy a string list
666 * 1 : dest = Destination list. Must be a valid list.
667 * All existing entries will be removed.
668 * 1 : src = pointer to source list for copy.
670 * Returns : 0 on success, nonzero on error. On error, dest
673 *********************************************************************/
674 int list_duplicate(struct list *dest, const struct list *src)
676 struct list_entry * cur_src;
677 struct list_entry * cur_dest;
681 assert(list_is_valid(src));
682 assert(list_is_valid(dest));
684 list_remove_all(dest);
686 /* Need to process first entry specially so we can set dest->first */
687 cur_src = src->first;
690 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
691 if (cur_dest == NULL)
695 assert(list_is_valid(src));
696 assert(list_is_valid(dest));
703 cur_dest->str = strdup(cur_src->str);
704 if (cur_dest->str == NULL)
708 assert(list_is_valid(src));
709 assert(list_is_valid(dest));
714 /* else { cur_dest->str = NULL; } - implied by zalloc */
716 /* Now process the rest */
717 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
719 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
720 if (cur_dest == NULL)
724 assert(list_is_valid(src));
725 assert(list_is_valid(dest));
731 cur_dest->str = strdup(cur_src->str);
732 if (cur_dest->str == NULL)
736 assert(list_is_valid(src));
737 assert(list_is_valid(dest));
742 /* else { cur_dest->str = NULL; } - implied by zalloc */
745 dest->last = cur_dest;
748 assert(list_is_valid(src));
749 assert(list_is_valid(dest));
755 /*********************************************************************
757 * Function : list_append_list_unique
759 * Description : Append a string list to another list.
760 * Duplicate items are not added.
763 * 1 : dest = pointer to destination list for merge.
764 * 2 : src = pointer to source for merge.
766 * Returns : 0 on success, nonzero on out-of-memory error.
767 * On error, some (but not all) of src might have
768 * been copied into dest.
770 *********************************************************************/
771 int list_append_list_unique(struct list *dest, const struct list *src)
773 struct list_entry * cur;
777 assert(list_is_valid(src));
778 assert(list_is_valid(dest));
780 for (cur = src->first; cur; cur = cur->next)
784 if (enlist_unique(dest, cur->str, 0))
786 assert(list_is_valid(src));
787 assert(list_is_valid(dest));
794 assert(list_is_valid(src));
795 assert(list_is_valid(dest));
801 /*********************************************************************
803 * Function : list_is_empty
805 * Description : Test whether a list is empty. Does not change the list.
808 * 1 : the_list = pointer to list to test.
810 * Returns : Nonzero iff the list contains no entries.
812 *********************************************************************/
813 int list_is_empty(const struct list *the_list)
816 assert(list_is_valid(the_list));
818 return (the_list->first == NULL);
822 /*********************************************************************
826 * Description : Create a new, empty map.
830 * Returns : A new, empty map, or NULL if out of memory.
832 *********************************************************************/
833 struct map *new_map(void)
835 return (struct map *) zalloc(sizeof(struct map));
839 /*********************************************************************
841 * Function : free_map
843 * Description : Free the memory occupied by a map and its
847 * 1 : the_map = map to be freed. May be NULL.
851 *********************************************************************/
852 void free_map(struct map *the_map)
854 struct map_entry *cur_entry;
855 struct map_entry *next_entry;
862 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
864 freez((char *)cur_entry->name);
865 freez((char *)cur_entry->value);
867 next_entry = cur_entry->next;
871 the_map->first = the_map->last = NULL;
877 /*********************************************************************
881 * Description : Add a mapping from given name to given value to a
884 * Note: Since all strings will be free()d in free_map()
885 * later, set the copy flags for constants or
886 * strings that will be independantly free()d.
889 * 1 : the_map = map to add to
890 * 2 : name = name to add
891 * 3 : name_needs_copying = flag set if a copy of name should be used
892 * 4 : value = value to add
893 * 5 : value_needs_copying = flag set if a copy of value should be used
895 * Returns : 0 on success, nonzero on out-of-memory error.
897 *********************************************************************/
898 int map(struct map *the_map,
899 const char *name, int name_needs_copying,
900 const char *value, int value_needs_copying)
902 struct map_entry *new_entry;
908 if (NULL == (new_entry = zalloc(sizeof(*new_entry))))
913 if (name_needs_copying)
915 if (NULL == (name = strdup(name)))
922 if (value_needs_copying)
924 if (NULL == (value = strdup(value)))
926 if (name_needs_copying)
935 new_entry->name = name;
936 new_entry->value = value;
937 /* new_entry->next = NULL; - implied by zalloc */
941 the_map->last->next = new_entry;
942 the_map->last = new_entry;
946 the_map->first = new_entry;
947 the_map->last = new_entry;
954 /*********************************************************************
958 * Description : Look up an item with a given name in a map, and
962 * 1 : the_map = map to look in
963 * 2 : name = name parameter to look for
965 * Returns : the value if found, else the empty string.
966 * Return value is alloced as part of the map, so
967 * it is freed when the map is destroyed. Caller
968 * must not free or modify it.
970 *********************************************************************/
971 const char *lookup(const struct map *the_map, const char *name)
973 const struct map_entry *cur_entry;
978 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
980 if (!strcmp(name, cur_entry->name))
982 return cur_entry->value;