1 const char list_rcs[] = "$Id: list.c,v 1.12 2001/10/25 03:40:48 david__schmidt 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.12 2001/10/25 03:40:48 david__schmidt
38 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
39 * threads to call select() simultaneously. So, it's time to do a real, live,
40 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
41 * (native). Both versions will work, but using __OS2__ offers multi-threading.
43 * Revision 1.11 2001/10/23 21:21:03 jongfoster
44 * New error handling - error codes are now jb_errs, not ints.
45 * Changed the way map() handles out-of-memory, to dramatically
46 * reduce the amount of error-checking clutter needed.
48 * Revision 1.10 2001/09/16 17:30:24 jongfoster
49 * Fixing a compiler warning.
51 * Revision 1.9 2001/09/16 13:20:29 jongfoster
52 * Rewrite of list library. Now has seperate header and list_entry
53 * structures. Also added a large sprinking of assert()s to the list
56 * Revision 1.8 2001/08/07 14:00:20 oes
59 * Revision 1.7 2001/08/05 16:06:20 jongfoster
60 * Modifiying "struct map" so that there are now separate header and
61 * "map_entry" structures. This means that functions which modify a
62 * map no longer need to return a pointer to the modified map.
63 * Also, it no longer reverses the order of the entries (which may be
64 * important with some advanced template substitutions).
66 * Revision 1.6 2001/07/31 14:44:51 oes
67 * list_to_text() now appends empty line at end
69 * Revision 1.5 2001/06/29 21:45:41 oes
70 * Indentation, CRLF->LF, Tab-> Space
72 * Revision 1.4 2001/06/29 13:30:22 oes
73 * - Added Convenience function enlist_unique_header(),
74 * which takes the Header name and value as separate
75 * arguments and thus saves the pain of sprintf()ing
76 * and determining the Header name length to enlist_unique
78 * - Removed logentry from cancelled commit
80 * Revision 1.3 2001/06/03 19:12:24 oes
81 * functions for new struct map, extended enlist_unique
83 * Revision 1.2 2001/06/01 18:49:17 jongfoster
84 * Replaced "list_share" with "list" - the tiny memory gain was not
85 * worth the extra complexity.
87 * Revision 1.1 2001/05/31 21:11:53 jongfoster
88 * - Moved linked list support to new "list.c" file.
89 * Structure definitions are still in project.h,
90 * function prototypes are now in "list.h".
91 * - Added support for "struct list_share", which is identical
92 * to "struct list" except it saves memory by not duplicating
93 * the strings. Obviously, this only works if there is some
94 * other way of managing the memory used by the strings.
95 * (These list_share lists are used for lists which last
96 * for only 1 request, and where all the list entries are
97 * just coming directly from entries in the actionsfile.)
98 * Note that you still need to destroy list_share lists
99 * properly to free the nodes - it's only the strings
103 *********************************************************************/
109 /* FIXME: The following headers are not needed for Win32. Are they
110 * needed on other platforms?
113 #include <sys/types.h>
119 #if !defined(_WIN32) && !defined(__OS2__)
127 #include "miscutil.h"
129 const char list_h_rcs[] = LIST_H_VERSION;
132 static int list_is_valid (const struct list *the_list);
135 /*********************************************************************
137 * Function : list_init
139 * Description : Create a new, empty list in user-allocated memory.
140 * Caller should allocate a "struct list" variable,
141 * then pass it to this function.
142 * (Implementation note: Rather than calling this
143 * function, you can also just memset the memory to
144 * zero, e.g. if you have a larger structure you
145 * want to initialize quickly. However, that isn't
146 * really good design.)
149 * 1 : the_list = pointer to list
153 *********************************************************************/
154 void init_list(struct list *the_list)
156 memset(the_list, '\0', sizeof(*the_list));
160 /*********************************************************************
162 * Function : destroy_list
164 * Description : Destroy a string list (opposite of list_init).
165 * On return, the memory used by the list entries has
166 * been freed, but not the memory used by the_list
167 * itself. You should not re-use the_list without
168 * calling list_init().
170 * (Implementation note: You *can* reuse the_list
171 * without calling list_init(), but please don't.
172 * If you want to remove all entries from a list
173 * and still have a usable list, then use
174 * list_remove_all().)
177 * 1 : the_list = pointer to list
181 *********************************************************************/
182 void destroy_list (struct list *the_list)
184 struct list_entry *cur_entry, *next_entry;
188 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
190 next_entry = cur_entry->next;
191 freez(cur_entry->str);
195 the_list->first = NULL;
196 the_list->last = NULL;
200 /*********************************************************************
202 * Function : list_is_valid
204 * Description : Check that a string list is valid. The intended
205 * usage is "assert(list_is_valid(the_list))".
206 * Currently this checks that "the_list->last"
207 * is correct, and that the list dosn't contain
208 * circular references. It is likely to crash if
209 * it's passed complete garbage.
212 * 1 : the_list = pointer to list. Must be non-null.
214 * Returns : 1 if list is valid, 0 otherwise.
216 *********************************************************************/
217 static int list_is_valid (const struct list *the_list)
220 * If you don't want this check, just change the line below
221 * from "#if 1" to "#if 0".
224 const struct list_entry *cur_entry;
225 const struct list_entry *last_entry = NULL;
230 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
232 last_entry = cur_entry;
237 * Just check that this string can be accessed - i.e. it's a valid
240 strlen(cur_entry->str);
244 * Check for looping back to first
246 if ((length != 0) && (cur_entry == the_list->first))
252 * Arbitrarily limit length to prevent infinite loops.
260 * Check this isn't marked as the last entry, unless of course it's
261 * *really* the last entry.
263 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
265 /* This is the last entry, but there's data after it !!?? */
270 return (the_list->last == last_entry);
276 /*********************************************************************
280 * Description : Append a string into a specified string list.
283 * 1 : the_list = pointer to list
284 * 2 : str = string to add to the list (maybe NULL)
286 * Returns : JB_ERR_OK on success
287 * JB_ERR_MEMORY on out-of-memory error.
288 * On error, the_list will be unchanged.
290 *********************************************************************/
291 jb_err enlist(struct list *the_list, const char *str)
293 struct list_entry *cur;
296 assert(list_is_valid(the_list));
298 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
300 return JB_ERR_MEMORY;
305 if (NULL == (cur->str = strdup(str)))
308 return JB_ERR_MEMORY;
311 /* else { cur->str = NULL; } - implied by zalloc */
313 /* cur->next = NULL; - implied by zalloc */
317 the_list->last->next = cur;
318 the_list->last = cur;
322 the_list->first = cur;
323 the_list->last = cur;
326 assert(list_is_valid(the_list));
331 /*********************************************************************
333 * Function : enlist_first
335 * Description : Append a string as first element into a specified
339 * 1 : the_list = pointer to list
340 * 2 : str = string to add to the list (maybe NULL)
342 * Returns : JB_ERR_OK on success
343 * JB_ERR_MEMORY on out-of-memory error.
344 * On error, the_list will be unchanged.
346 *********************************************************************/
347 jb_err enlist_first(struct list *the_list, const char *str)
349 struct list_entry *cur;
352 assert(list_is_valid(the_list));
354 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
356 return JB_ERR_MEMORY;
361 if (NULL == (cur->str = strdup(str)))
364 return JB_ERR_MEMORY;
367 /* else { cur->str = NULL; } - implied by zalloc */
369 cur->next = the_list->first;
371 the_list->first = cur;
372 if (the_list->last == NULL)
374 the_list->last = cur;
377 assert(list_is_valid(the_list));
382 /*********************************************************************
384 * Function : enlist_unique
386 * Description : Append a string into a specified string list,
387 * if & only if it's not there already.
388 * If the num_significant_chars argument is nonzero,
389 * only compare up to the nth character.
392 * 1 : the_list = pointer to list
393 * 2 : str = string to add to the list
394 * 3 : num_significant_chars = number of chars to use
395 * for uniqueness test, or 0 to require an exact match.
397 * Returns : JB_ERR_OK on success
398 * JB_ERR_MEMORY on out-of-memory error.
399 * On error, the_list will be unchanged.
400 * "Success" does not indicate whether or not the
401 * item was already in the list.
403 *********************************************************************/
404 jb_err enlist_unique(struct list *the_list, const char *str,
405 size_t num_significant_chars)
407 struct list_entry *cur_entry;
410 assert(list_is_valid(the_list));
412 assert(num_significant_chars >= 0);
413 assert(num_significant_chars <= strlen(str));
415 if (num_significant_chars > 0)
417 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
419 if ( (cur_entry->str != NULL)
420 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
429 /* Test whole string */
430 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
432 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
440 return enlist(the_list, str);
444 /*********************************************************************
446 * Function : enlist_unique_header
448 * Description : Make a HTTP header from the two strings name and value,
449 * and append the result into a specified string list,
450 * if & only if there isn't already a header with that name.
453 * 1 : the_list = pointer to list
454 * 2 : name = HTTP header name (e.g. "Content-type")
455 * 3 : value = HTTP header value (e.g. "text/html")
457 * Returns : JB_ERR_OK on success
458 * JB_ERR_MEMORY on out-of-memory error.
459 * On error, the_list will be unchanged.
460 * "Success" does not indicate whether or not the
461 * header was already in the list.
463 *********************************************************************/
464 jb_err enlist_unique_header(struct list *the_list, const char *name,
472 assert(list_is_valid(the_list));
476 length = strlen(name) + 2;
477 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
479 return JB_ERR_MEMORY;
482 str[length - 2] = ':';
483 str[length - 1] = ' ';
484 strcpy(str + length, value);
486 result = enlist_unique(the_list, str, length);
490 assert(list_is_valid(the_list));
497 /*********************************************************************
499 * Function : list_remove_all
501 * Description : Remove all entries from a list. On return, the_list
502 * is a valid, empty list. Note that this is similar
503 * to destroy_list(), but the difference is that this
504 * function guarantees that the list structure is still
505 * valid after the call.
508 * 1 : the_list = pointer to list
512 *********************************************************************/
513 void list_remove_all(struct list *the_list)
515 struct list_entry *cur_entry;
516 struct list_entry *next_entry;
519 assert(list_is_valid(the_list));
521 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
523 next_entry = cur_entry->next;
524 freez(cur_entry->str);
528 the_list->first = the_list->last = NULL;
530 assert(list_is_valid(the_list));
534 /*********************************************************************
536 * Function : list_to_text
538 * Description : "Flatten" a string list into 1 long \r\n delimited string,
539 * adding an empty line at the end. NULL entries are ignored.
540 * This function does not change the_list.
543 * 1 : the_list = pointer to list
545 * Returns : NULL on malloc error, else new long string.
546 * Caller must free() it.
548 *********************************************************************/
549 char *list_to_text(const struct list *the_list)
551 struct list_entry *cur_entry;
557 assert(list_is_valid(the_list));
559 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
563 size += strlen(cur_entry->str) + 2;
567 if ((ret = (char *)malloc(size + 1)) == NULL)
576 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
580 strcpy(s, cur_entry->str);
582 *s++ = '\r'; *s++ = '\n';
585 *s++ = '\r'; *s++ = '\n';
591 /*********************************************************************
593 * Function : list_remove_item
595 * Description : Remove a string from a specified string list.
598 * 1 : the_list = pointer to list
599 * 2 : str = string to remove from the list - non-NULL
601 * Returns : Number of times it was removed.
603 *********************************************************************/
604 int list_remove_item(struct list *the_list, const char *str)
606 struct list_entry *prev = NULL;
607 struct list_entry *cur;
608 struct list_entry *next;
612 assert(list_is_valid(the_list));
615 cur = the_list->first;
621 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
631 the_list->first = next;
633 free((char *)cur->str);
643 the_list->last = prev;
645 assert(list_is_valid(the_list));
651 /*********************************************************************
653 * Function : list_remove_list
655 * Description : Remove all strings in one list from another list.
656 * This is currently a brute-force algorithm
657 * (it compares every pair of strings).
660 * 1 : dest = list to change
661 * 2 : src = list of strings to remove
663 * Returns : Total number of strings removed.
665 *********************************************************************/
666 int list_remove_list(struct list *dest, const struct list *src)
668 struct list_entry *cur;
673 assert(list_is_valid(src));
674 assert(list_is_valid(dest));
676 for (cur = src->first; cur != NULL; cur = cur->next)
678 if (cur->str != NULL)
680 count += list_remove_item(dest, cur->str);
684 assert(list_is_valid(src));
685 assert(list_is_valid(dest));
691 /*********************************************************************
693 * Function : list_duplicate
695 * Description : Copy a string list
698 * 1 : dest = Destination list. Must be a valid list.
699 * All existing entries will be removed.
700 * 1 : src = pointer to source list for copy.
702 * Returns : JB_ERR_OK on success
703 * JB_ERR_MEMORY on out-of-memory error.
704 * On error, dest will be empty.
706 *********************************************************************/
707 jb_err list_duplicate(struct list *dest, const struct list *src)
709 struct list_entry * cur_src;
710 struct list_entry * cur_dest;
714 assert(list_is_valid(src));
715 assert(list_is_valid(dest));
717 list_remove_all(dest);
719 /* Need to process first entry specially so we can set dest->first */
720 cur_src = src->first;
723 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
724 if (cur_dest == NULL)
728 assert(list_is_valid(src));
729 assert(list_is_valid(dest));
731 return JB_ERR_MEMORY;
736 cur_dest->str = strdup(cur_src->str);
737 if (cur_dest->str == NULL)
741 assert(list_is_valid(src));
742 assert(list_is_valid(dest));
744 return JB_ERR_MEMORY;
747 /* else { cur_dest->str = NULL; } - implied by zalloc */
749 /* Now process the rest */
750 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
752 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
753 if (cur_dest == NULL)
757 assert(list_is_valid(src));
758 assert(list_is_valid(dest));
760 return JB_ERR_MEMORY;
764 cur_dest->str = strdup(cur_src->str);
765 if (cur_dest->str == NULL)
769 assert(list_is_valid(src));
770 assert(list_is_valid(dest));
772 return JB_ERR_MEMORY;
775 /* else { cur_dest->str = NULL; } - implied by zalloc */
778 dest->last = cur_dest;
781 assert(list_is_valid(src));
782 assert(list_is_valid(dest));
788 /*********************************************************************
790 * Function : list_append_list_unique
792 * Description : Append a string list to another list.
793 * Duplicate items are not added.
796 * 1 : dest = pointer to destination list for merge.
797 * 2 : src = pointer to source for merge.
799 * Returns : JB_ERR_OK on success
800 * JB_ERR_MEMORY on out-of-memory error.
801 * On error, some (but not all) of src might have
802 * been copied into dest.
804 *********************************************************************/
805 jb_err list_append_list_unique(struct list *dest, const struct list *src)
807 struct list_entry * cur;
811 assert(list_is_valid(src));
812 assert(list_is_valid(dest));
814 for (cur = src->first; cur; cur = cur->next)
818 if (enlist_unique(dest, cur->str, 0))
820 assert(list_is_valid(src));
821 assert(list_is_valid(dest));
823 return JB_ERR_MEMORY;
828 assert(list_is_valid(src));
829 assert(list_is_valid(dest));
835 /*********************************************************************
837 * Function : list_is_empty
839 * Description : Test whether a list is empty. Does not change the list.
842 * 1 : the_list = pointer to list to test.
844 * Returns : Nonzero iff the list contains no entries.
846 *********************************************************************/
847 int list_is_empty(const struct list *the_list)
850 assert(list_is_valid(the_list));
852 return (the_list->first == NULL);
856 /*********************************************************************
860 * Description : Create a new, empty map.
864 * Returns : A new, empty map, or NULL if out of memory.
866 *********************************************************************/
867 struct map *new_map(void)
869 return (struct map *) zalloc(sizeof(struct map));
873 /*********************************************************************
875 * Function : free_map
877 * Description : Free the memory occupied by a map and its
881 * 1 : the_map = map to be freed. May be NULL.
885 *********************************************************************/
886 void free_map(struct map *the_map)
888 struct map_entry *cur_entry;
889 struct map_entry *next_entry;
896 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
898 freez(cur_entry->name);
899 freez(cur_entry->value);
901 next_entry = cur_entry->next;
905 the_map->first = the_map->last = NULL;
911 /*********************************************************************
915 * Description : Add a mapping from given name to given value to a
918 * Note: Since all strings will be free()d in free_map()
919 * later, set the copy flags for constants or
920 * strings that will be independantly free()d.
922 * Note2: This function allows NULL parameters - it
923 * returns JB_ERR_MEMORY in that case.
925 * Note3: If this function returns JB_ERR_MEMORY,
926 * it will free(name) unless you specify
927 * name_needs_copying, and similarly it will
928 * free(value) unless you specify
929 * value_needs_copying.
931 * Due to Note2 and Note3 above, the following code
932 * is legal, and will never crash or leak memory even
933 * if the system runs out of memory:
935 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
937 * err will be set to JB_ERR_MEMORY if either call runs
938 * out-of-memory. Without these features, you would
939 * need to check the return value of html_encode in the
940 * above example for NULL, which (at least) doubles the
941 * amount of error-checking code needed.
944 * 1 : the_map = map to add to
945 * 2 : name = name to add
946 * 3 : name_needs_copying = flag set if a copy of name should be used
947 * 4 : value = value to add
948 * 5 : value_needs_copying = flag set if a copy of value should be used
950 * Returns : JB_ERR_OK on success
951 * JB_ERR_MEMORY on out-of-memory error.
953 *********************************************************************/
954 jb_err map(struct map *the_map,
955 const char *name, int name_needs_copying,
956 const char *value, int value_needs_copying)
958 struct map_entry *new_entry;
964 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
966 if ((name != NULL) && (!name_needs_copying))
970 if ((value != NULL) && (!value_needs_copying))
974 return JB_ERR_MEMORY;
977 if (name_needs_copying)
979 if (NULL == (name = strdup(name)))
982 if (!value_needs_copying)
986 return JB_ERR_MEMORY;
990 if (value_needs_copying)
992 if (NULL == (value = strdup(value)))
996 return JB_ERR_MEMORY;
1000 new_entry->name = name;
1001 new_entry->value = value;
1002 /* new_entry->next = NULL; - implied by zalloc */
1006 the_map->last->next = new_entry;
1007 the_map->last = new_entry;
1011 the_map->first = new_entry;
1012 the_map->last = new_entry;
1019 /*********************************************************************
1023 * Description : Look up an item with a given name in a map, and
1027 * 1 : the_map = map to look in
1028 * 2 : name = name parameter to look for
1030 * Returns : the value if found, else the empty string.
1031 * Return value is alloced as part of the map, so
1032 * it is freed when the map is destroyed. Caller
1033 * must not free or modify it.
1035 *********************************************************************/
1036 const char *lookup(const struct map *the_map, const char *name)
1038 const struct map_entry *cur_entry;
1043 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1045 if (!strcmp(name, cur_entry->name))
1047 return cur_entry->value;