1 const char list_rcs[] = "$Id: list.c,v 1.11 2001/10/23 21:21:03 jongfoster 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.11 2001/10/23 21:21:03 jongfoster
38 * New error handling - error codes are now jb_errs, not ints.
39 * Changed the way map() handles out-of-memory, to dramatically
40 * reduce the amount of error-checking clutter needed.
42 * Revision 1.10 2001/09/16 17:30:24 jongfoster
43 * Fixing a compiler warning.
45 * Revision 1.9 2001/09/16 13:20:29 jongfoster
46 * Rewrite of list library. Now has seperate header and list_entry
47 * structures. Also added a large sprinking of assert()s to the list
50 * Revision 1.8 2001/08/07 14:00:20 oes
53 * Revision 1.7 2001/08/05 16:06:20 jongfoster
54 * Modifiying "struct map" so that there are now separate header and
55 * "map_entry" structures. This means that functions which modify a
56 * map no longer need to return a pointer to the modified map.
57 * Also, it no longer reverses the order of the entries (which may be
58 * important with some advanced template substitutions).
60 * Revision 1.6 2001/07/31 14:44:51 oes
61 * list_to_text() now appends empty line at end
63 * Revision 1.5 2001/06/29 21:45:41 oes
64 * Indentation, CRLF->LF, Tab-> Space
66 * Revision 1.4 2001/06/29 13:30:22 oes
67 * - Added Convenience function enlist_unique_header(),
68 * which takes the Header name and value as separate
69 * arguments and thus saves the pain of sprintf()ing
70 * and determining the Header name length to enlist_unique
72 * - Removed logentry from cancelled commit
74 * Revision 1.3 2001/06/03 19:12:24 oes
75 * functions for new struct map, extended enlist_unique
77 * Revision 1.2 2001/06/01 18:49:17 jongfoster
78 * Replaced "list_share" with "list" - the tiny memory gain was not
79 * worth the extra complexity.
81 * Revision 1.1 2001/05/31 21:11:53 jongfoster
82 * - Moved linked list support to new "list.c" file.
83 * Structure definitions are still in project.h,
84 * function prototypes are now in "list.h".
85 * - Added support for "struct list_share", which is identical
86 * to "struct list" except it saves memory by not duplicating
87 * the strings. Obviously, this only works if there is some
88 * other way of managing the memory used by the strings.
89 * (These list_share lists are used for lists which last
90 * for only 1 request, and where all the list entries are
91 * just coming directly from entries in the actionsfile.)
92 * Note that you still need to destroy list_share lists
93 * properly to free the nodes - it's only the strings
97 *********************************************************************/
103 /* FIXME: The following headers are not needed for Win32. Are they
104 * needed on other platforms?
107 #include <sys/types.h>
113 #if !defined(_WIN32) && !defined(__OS2__)
121 #include "miscutil.h"
123 const char list_h_rcs[] = LIST_H_VERSION;
126 static int list_is_valid (const struct list *the_list);
129 /*********************************************************************
131 * Function : list_init
133 * Description : Create a new, empty list in user-allocated memory.
134 * Caller should allocate a "struct list" variable,
135 * then pass it to this function.
136 * (Implementation note: Rather than calling this
137 * function, you can also just memset the memory to
138 * zero, e.g. if you have a larger structure you
139 * want to initialize quickly. However, that isn't
140 * really good design.)
143 * 1 : the_list = pointer to list
147 *********************************************************************/
148 void init_list(struct list *the_list)
150 memset(the_list, '\0', sizeof(*the_list));
154 /*********************************************************************
156 * Function : destroy_list
158 * Description : Destroy a string list (opposite of list_init).
159 * On return, the memory used by the list entries has
160 * been freed, but not the memory used by the_list
161 * itself. You should not re-use the_list without
162 * calling list_init().
164 * (Implementation note: You *can* reuse the_list
165 * without calling list_init(), but please don't.
166 * If you want to remove all entries from a list
167 * and still have a usable list, then use
168 * list_remove_all().)
171 * 1 : the_list = pointer to list
175 *********************************************************************/
176 void destroy_list (struct list *the_list)
178 struct list_entry *cur_entry, *next_entry;
182 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
184 next_entry = cur_entry->next;
185 freez(cur_entry->str);
189 the_list->first = NULL;
190 the_list->last = NULL;
194 /*********************************************************************
196 * Function : list_is_valid
198 * Description : Check that a string list is valid. The intended
199 * usage is "assert(list_is_valid(the_list))".
200 * Currently this checks that "the_list->last"
201 * is correct, and that the list dosn't contain
202 * circular references. It is likely to crash if
203 * it's passed complete garbage.
206 * 1 : the_list = pointer to list. Must be non-null.
208 * Returns : 1 if list is valid, 0 otherwise.
210 *********************************************************************/
211 static int list_is_valid (const struct list *the_list)
214 * If you don't want this check, just change the line below
215 * from "#if 1" to "#if 0".
218 const struct list_entry *cur_entry;
219 const struct list_entry *last_entry = NULL;
224 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
226 last_entry = cur_entry;
231 * Just check that this string can be accessed - i.e. it's a valid
234 strlen(cur_entry->str);
238 * Check for looping back to first
240 if ((length != 0) && (cur_entry == the_list->first))
246 * Arbitrarily limit length to prevent infinite loops.
254 * Check this isn't marked as the last entry, unless of course it's
255 * *really* the last entry.
257 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
259 /* This is the last entry, but there's data after it !!?? */
264 return (the_list->last == last_entry);
270 /*********************************************************************
274 * Description : Append a string into a specified string list.
277 * 1 : the_list = pointer to list
278 * 2 : str = string to add to the list (maybe NULL)
280 * Returns : JB_ERR_OK on success
281 * JB_ERR_MEMORY on out-of-memory error.
282 * On error, the_list will be unchanged.
284 *********************************************************************/
285 jb_err enlist(struct list *the_list, const char *str)
287 struct list_entry *cur;
290 assert(list_is_valid(the_list));
292 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
294 return JB_ERR_MEMORY;
299 if (NULL == (cur->str = strdup(str)))
302 return JB_ERR_MEMORY;
305 /* else { cur->str = NULL; } - implied by zalloc */
307 /* cur->next = NULL; - implied by zalloc */
311 the_list->last->next = cur;
312 the_list->last = cur;
316 the_list->first = cur;
317 the_list->last = cur;
320 assert(list_is_valid(the_list));
325 /*********************************************************************
327 * Function : enlist_first
329 * Description : Append a string as first element into a specified
333 * 1 : the_list = pointer to list
334 * 2 : str = string to add to the list (maybe NULL)
336 * Returns : JB_ERR_OK on success
337 * JB_ERR_MEMORY on out-of-memory error.
338 * On error, the_list will be unchanged.
340 *********************************************************************/
341 jb_err enlist_first(struct list *the_list, const char *str)
343 struct list_entry *cur;
346 assert(list_is_valid(the_list));
348 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
350 return JB_ERR_MEMORY;
355 if (NULL == (cur->str = strdup(str)))
358 return JB_ERR_MEMORY;
361 /* else { cur->str = NULL; } - implied by zalloc */
363 cur->next = the_list->first;
365 the_list->first = cur;
366 if (the_list->last == NULL)
368 the_list->last = cur;
371 assert(list_is_valid(the_list));
376 /*********************************************************************
378 * Function : enlist_unique
380 * Description : Append a string into a specified string list,
381 * if & only if it's not there already.
382 * If the num_significant_chars argument is nonzero,
383 * only compare up to the nth character.
386 * 1 : the_list = pointer to list
387 * 2 : str = string to add to the list
388 * 3 : num_significant_chars = number of chars to use
389 * for uniqueness test, or 0 to require an exact match.
391 * Returns : JB_ERR_OK on success
392 * JB_ERR_MEMORY on out-of-memory error.
393 * On error, the_list will be unchanged.
394 * "Success" does not indicate whether or not the
395 * item was already in the list.
397 *********************************************************************/
398 jb_err enlist_unique(struct list *the_list, const char *str,
399 int num_significant_chars)
401 struct list_entry *cur_entry;
404 assert(list_is_valid(the_list));
406 assert(num_significant_chars >= 0);
407 assert((size_t)num_significant_chars <= strlen(str));
409 if (num_significant_chars > 0)
411 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
413 if ( (cur_entry->str != NULL)
414 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
423 /* Test whole string */
424 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
426 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
434 return enlist(the_list, str);
438 /*********************************************************************
440 * Function : enlist_unique_header
442 * Description : Make a HTTP header from the two strings name and value,
443 * and append the result into a specified string list,
444 * if & only if there isn't already a header with that name.
447 * 1 : the_list = pointer to list
448 * 2 : name = HTTP header name (e.g. "Content-type")
449 * 3 : value = HTTP header value (e.g. "text/html")
451 * Returns : JB_ERR_OK on success
452 * JB_ERR_MEMORY on out-of-memory error.
453 * On error, the_list will be unchanged.
454 * "Success" does not indicate whether or not the
455 * header was already in the list.
457 *********************************************************************/
458 jb_err enlist_unique_header(struct list *the_list, const char *name,
466 assert(list_is_valid(the_list));
470 length = strlen(name) + 2;
471 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
473 return JB_ERR_MEMORY;
476 str[length - 2] = ':';
477 str[length - 1] = ' ';
478 strcpy(str + length, value);
480 result = enlist_unique(the_list, str, length);
484 assert(list_is_valid(the_list));
490 /*********************************************************************
492 * Function : list_remove_all
494 * Description : Remove all entries from a list. On return, the_list
495 * is a valid, empty list. Note that this is similar
496 * to destroy_list(), but the difference is that this
497 * function guarantees that the list structure is still
498 * valid after the call.
501 * 1 : the_list = pointer to list
505 *********************************************************************/
506 void list_remove_all(struct list *the_list)
508 struct list_entry *cur_entry;
509 struct list_entry *next_entry;
512 assert(list_is_valid(the_list));
514 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
516 next_entry = cur_entry->next;
517 freez(cur_entry->str);
521 the_list->first = the_list->last = NULL;
523 assert(list_is_valid(the_list));
527 /*********************************************************************
529 * Function : list_to_text
531 * Description : "Flatten" a string list into 1 long \r\n delimited string,
532 * adding an empty line at the end. NULL entries are ignored.
533 * This function does not change the_list.
536 * 1 : the_list = pointer to list
538 * Returns : NULL on malloc error, else new long string.
539 * Caller must free() it.
541 *********************************************************************/
542 char *list_to_text(const struct list *the_list)
544 struct list_entry *cur_entry;
550 assert(list_is_valid(the_list));
552 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
556 size += strlen(cur_entry->str) + 2;
560 if ((ret = (char *)malloc(size + 1)) == NULL)
569 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
573 strcpy(s, cur_entry->str);
575 *s++ = '\r'; *s++ = '\n';
578 *s++ = '\r'; *s++ = '\n';
584 /*********************************************************************
586 * Function : list_remove_item
588 * Description : Remove a string from a specified string list.
591 * 1 : the_list = pointer to list
592 * 2 : str = string to remove from the list - non-NULL
594 * Returns : Number of times it was removed.
596 *********************************************************************/
597 int list_remove_item(struct list *the_list, const char *str)
599 struct list_entry *prev = NULL;
600 struct list_entry *cur;
601 struct list_entry *next;
605 assert(list_is_valid(the_list));
608 cur = the_list->first;
614 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
624 the_list->first = next;
626 free((char *)cur->str);
636 the_list->last = prev;
638 assert(list_is_valid(the_list));
644 /*********************************************************************
646 * Function : list_remove_list
648 * Description : Remove all strings in one list from another list.
649 * This is currently a brute-force algorithm
650 * (it compares every pair of strings).
653 * 1 : dest = list to change
654 * 2 : src = list of strings to remove
656 * Returns : Total number of strings removed.
658 *********************************************************************/
659 int list_remove_list(struct list *dest, const struct list *src)
661 struct list_entry *cur;
666 assert(list_is_valid(src));
667 assert(list_is_valid(dest));
669 for (cur = src->first; cur != NULL; cur = cur->next)
671 if (cur->str != NULL)
673 count += list_remove_item(dest, cur->str);
677 assert(list_is_valid(src));
678 assert(list_is_valid(dest));
684 /*********************************************************************
686 * Function : list_duplicate
688 * Description : Copy a string list
691 * 1 : dest = Destination list. Must be a valid list.
692 * All existing entries will be removed.
693 * 1 : src = pointer to source list for copy.
695 * Returns : JB_ERR_OK on success
696 * JB_ERR_MEMORY on out-of-memory error.
697 * On error, dest will be empty.
699 *********************************************************************/
700 jb_err list_duplicate(struct list *dest, const struct list *src)
702 struct list_entry * cur_src;
703 struct list_entry * cur_dest;
707 assert(list_is_valid(src));
708 assert(list_is_valid(dest));
710 list_remove_all(dest);
712 /* Need to process first entry specially so we can set dest->first */
713 cur_src = src->first;
716 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
717 if (cur_dest == NULL)
721 assert(list_is_valid(src));
722 assert(list_is_valid(dest));
724 return JB_ERR_MEMORY;
729 cur_dest->str = strdup(cur_src->str);
730 if (cur_dest->str == NULL)
734 assert(list_is_valid(src));
735 assert(list_is_valid(dest));
737 return JB_ERR_MEMORY;
740 /* else { cur_dest->str = NULL; } - implied by zalloc */
742 /* Now process the rest */
743 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
745 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
746 if (cur_dest == NULL)
750 assert(list_is_valid(src));
751 assert(list_is_valid(dest));
753 return JB_ERR_MEMORY;
757 cur_dest->str = strdup(cur_src->str);
758 if (cur_dest->str == NULL)
762 assert(list_is_valid(src));
763 assert(list_is_valid(dest));
765 return JB_ERR_MEMORY;
768 /* else { cur_dest->str = NULL; } - implied by zalloc */
771 dest->last = cur_dest;
774 assert(list_is_valid(src));
775 assert(list_is_valid(dest));
781 /*********************************************************************
783 * Function : list_append_list_unique
785 * Description : Append a string list to another list.
786 * Duplicate items are not added.
789 * 1 : dest = pointer to destination list for merge.
790 * 2 : src = pointer to source for merge.
792 * Returns : JB_ERR_OK on success
793 * JB_ERR_MEMORY on out-of-memory error.
794 * On error, some (but not all) of src might have
795 * been copied into dest.
797 *********************************************************************/
798 jb_err list_append_list_unique(struct list *dest, const struct list *src)
800 struct list_entry * cur;
804 assert(list_is_valid(src));
805 assert(list_is_valid(dest));
807 for (cur = src->first; cur; cur = cur->next)
811 if (enlist_unique(dest, cur->str, 0))
813 assert(list_is_valid(src));
814 assert(list_is_valid(dest));
816 return JB_ERR_MEMORY;
821 assert(list_is_valid(src));
822 assert(list_is_valid(dest));
828 /*********************************************************************
830 * Function : list_is_empty
832 * Description : Test whether a list is empty. Does not change the list.
835 * 1 : the_list = pointer to list to test.
837 * Returns : Nonzero iff the list contains no entries.
839 *********************************************************************/
840 int list_is_empty(const struct list *the_list)
843 assert(list_is_valid(the_list));
845 return (the_list->first == NULL);
849 /*********************************************************************
853 * Description : Create a new, empty map.
857 * Returns : A new, empty map, or NULL if out of memory.
859 *********************************************************************/
860 struct map *new_map(void)
862 return (struct map *) zalloc(sizeof(struct map));
866 /*********************************************************************
868 * Function : free_map
870 * Description : Free the memory occupied by a map and its
874 * 1 : the_map = map to be freed. May be NULL.
878 *********************************************************************/
879 void free_map(struct map *the_map)
881 struct map_entry *cur_entry;
882 struct map_entry *next_entry;
889 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
891 freez(cur_entry->name);
892 freez(cur_entry->value);
894 next_entry = cur_entry->next;
898 the_map->first = the_map->last = NULL;
904 /*********************************************************************
908 * Description : Add a mapping from given name to given value to a
911 * Note: Since all strings will be free()d in free_map()
912 * later, set the copy flags for constants or
913 * strings that will be independantly free()d.
915 * Note2: This function allows NULL parameters - it
916 * returns JB_ERR_MEMORY in that case.
918 * Note3: If this function returns JB_ERR_MEMORY,
919 * it will free(name) unless you specify
920 * name_needs_copying, and similarly it will
921 * free(value) unless you specify
922 * value_needs_copying.
924 * Due to Note2 and Note3 above, the following code
925 * is legal, and will never crash or leak memory even
926 * if the system runs out of memory:
928 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
930 * err will be set to JB_ERR_MEMORY if either call runs
931 * out-of-memory. Without these features, you would
932 * need to check the return value of html_encode in the
933 * above example for NULL, which (at least) doubles the
934 * amount of error-checking code needed.
937 * 1 : the_map = map to add to
938 * 2 : name = name to add
939 * 3 : name_needs_copying = flag set if a copy of name should be used
940 * 4 : value = value to add
941 * 5 : value_needs_copying = flag set if a copy of value should be used
943 * Returns : JB_ERR_OK on success
944 * JB_ERR_MEMORY on out-of-memory error.
946 *********************************************************************/
947 jb_err map(struct map *the_map,
948 const char *name, int name_needs_copying,
949 const char *value, int value_needs_copying)
951 struct map_entry *new_entry;
957 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
959 if ((name != NULL) && (!name_needs_copying))
963 if ((value != NULL) && (!value_needs_copying))
967 return JB_ERR_MEMORY;
970 if (name_needs_copying)
972 if (NULL == (name = strdup(name)))
975 if (!value_needs_copying)
979 return JB_ERR_MEMORY;
983 if (value_needs_copying)
985 if (NULL == (value = strdup(value)))
989 return JB_ERR_MEMORY;
993 new_entry->name = name;
994 new_entry->value = value;
995 /* new_entry->next = NULL; - implied by zalloc */
999 the_map->last->next = new_entry;
1000 the_map->last = new_entry;
1004 the_map->first = new_entry;
1005 the_map->last = new_entry;
1012 /*********************************************************************
1016 * Description : Look up an item with a given name in a map, and
1020 * 1 : the_map = map to look in
1021 * 2 : name = name parameter to look for
1023 * Returns : the value if found, else the empty string.
1024 * Return value is alloced as part of the map, so
1025 * it is freed when the map is destroyed. Caller
1026 * must not free or modify it.
1028 *********************************************************************/
1029 const char *lookup(const struct map *the_map, const char *name)
1031 const struct map_entry *cur_entry;
1036 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1038 if (!strcmp(name, cur_entry->name))
1040 return cur_entry->value;