Annotation of embedaddon/curl/lib/slist.c, revision 1.1
1.1 ! misho 1: /***************************************************************************
! 2: * _ _ ____ _
! 3: * Project ___| | | | _ \| |
! 4: * / __| | | | |_) | |
! 5: * | (__| |_| | _ <| |___
! 6: * \___|\___/|_| \_\_____|
! 7: *
! 8: * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
! 9: *
! 10: * This software is licensed as described in the file COPYING, which
! 11: * you should have received as part of this distribution. The terms
! 12: * are also available at https://curl.haxx.se/docs/copyright.html.
! 13: *
! 14: * You may opt to use, copy, modify, merge, publish, distribute and/or sell
! 15: * copies of the Software, and permit persons to whom the Software is
! 16: * furnished to do so, under the terms of the COPYING file.
! 17: *
! 18: * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
! 19: * KIND, either express or implied.
! 20: *
! 21: ***************************************************************************/
! 22:
! 23: #include "curl_setup.h"
! 24:
! 25: #include <curl/curl.h>
! 26:
! 27: #include "slist.h"
! 28:
! 29: /* The last #include files should be: */
! 30: #include "curl_memory.h"
! 31: #include "memdebug.h"
! 32:
! 33: /* returns last node in linked list */
! 34: static struct curl_slist *slist_get_last(struct curl_slist *list)
! 35: {
! 36: struct curl_slist *item;
! 37:
! 38: /* if caller passed us a NULL, return now */
! 39: if(!list)
! 40: return NULL;
! 41:
! 42: /* loop through to find the last item */
! 43: item = list;
! 44: while(item->next) {
! 45: item = item->next;
! 46: }
! 47: return item;
! 48: }
! 49:
! 50: /*
! 51: * Curl_slist_append_nodup() appends a string to the linked list. Rather than
! 52: * copying the string in dynamic storage, it takes its ownership. The string
! 53: * should have been malloc()ated. Curl_slist_append_nodup always returns
! 54: * the address of the first record, so that you can use this function as an
! 55: * initialization function as well as an append function.
! 56: * If an error occurs, NULL is returned and the string argument is NOT
! 57: * released.
! 58: */
! 59: struct curl_slist *Curl_slist_append_nodup(struct curl_slist *list, char *data)
! 60: {
! 61: struct curl_slist *last;
! 62: struct curl_slist *new_item;
! 63:
! 64: DEBUGASSERT(data);
! 65:
! 66: new_item = malloc(sizeof(struct curl_slist));
! 67: if(!new_item)
! 68: return NULL;
! 69:
! 70: new_item->next = NULL;
! 71: new_item->data = data;
! 72:
! 73: /* if this is the first item, then new_item *is* the list */
! 74: if(!list)
! 75: return new_item;
! 76:
! 77: last = slist_get_last(list);
! 78: last->next = new_item;
! 79: return list;
! 80: }
! 81:
! 82: /*
! 83: * curl_slist_append() appends a string to the linked list. It always returns
! 84: * the address of the first record, so that you can use this function as an
! 85: * initialization function as well as an append function. If you find this
! 86: * bothersome, then simply create a separate _init function and call it
! 87: * appropriately from within the program.
! 88: */
! 89: struct curl_slist *curl_slist_append(struct curl_slist *list,
! 90: const char *data)
! 91: {
! 92: char *dupdata = strdup(data);
! 93:
! 94: if(!dupdata)
! 95: return NULL;
! 96:
! 97: list = Curl_slist_append_nodup(list, dupdata);
! 98: if(!list)
! 99: free(dupdata);
! 100:
! 101: return list;
! 102: }
! 103:
! 104: /*
! 105: * Curl_slist_duplicate() duplicates a linked list. It always returns the
! 106: * address of the first record of the cloned list or NULL in case of an
! 107: * error (or if the input list was NULL).
! 108: */
! 109: struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
! 110: {
! 111: struct curl_slist *outlist = NULL;
! 112: struct curl_slist *tmp;
! 113:
! 114: while(inlist) {
! 115: tmp = curl_slist_append(outlist, inlist->data);
! 116:
! 117: if(!tmp) {
! 118: curl_slist_free_all(outlist);
! 119: return NULL;
! 120: }
! 121:
! 122: outlist = tmp;
! 123: inlist = inlist->next;
! 124: }
! 125: return outlist;
! 126: }
! 127:
! 128: /* be nice and clean up resources */
! 129: void curl_slist_free_all(struct curl_slist *list)
! 130: {
! 131: struct curl_slist *next;
! 132: struct curl_slist *item;
! 133:
! 134: if(!list)
! 135: return;
! 136:
! 137: item = list;
! 138: do {
! 139: next = item->next;
! 140: Curl_safefree(item->data);
! 141: free(item);
! 142: item = next;
! 143: } while(next);
! 144: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>