1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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.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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 25#include "curl_setup.h" 26 27#include <curl/curl.h> 28 29#include "slist.h" 30 31/* The last #include files should be: */ 32#include "curl_memory.h" 33#include "memdebug.h" 34 35/* returns last node in linked list */ 36static struct curl_slist *slist_get_last(struct curl_slist *list) 37{ 38 struct curl_slist *item; 39 40 /* if caller passed us a NULL, return now */ 41 if(!list) 42 return NULL; 43 44 /* loop through to find the last item */ 45 item = list; 46 while(item->next) { 47 item = item->next; 48 } 49 return item; 50} 51 52/* 53 * Curl_slist_append_nodup() appends a string to the linked list. Rather than 54 * copying the string in dynamic storage, it takes its ownership. The string 55 * should have been malloc()ated. Curl_slist_append_nodup always returns 56 * the address of the first record, so that you can use this function as an 57 * initialization function as well as an append function. 58 * If an error occurs, NULL is returned and the string argument is NOT 59 * released. 60 */ 61struct curl_slist *Curl_slist_append_nodup(struct curl_slist *list, char *data) 62{ 63 struct curl_slist *last; 64 struct curl_slist *new_item; 65 66 DEBUGASSERT(data); 67 68 new_item = malloc(sizeof(struct curl_slist)); 69 if(!new_item) 70 return NULL; 71 72 new_item->next = NULL; 73 new_item->data = data; 74 75 /* if this is the first item, then new_item *is* the list */ 76 if(!list) 77 return new_item; 78 79 last = slist_get_last(list); 80 last->next = new_item; 81 return list; 82} 83 84/* 85 * curl_slist_append() appends a string to the linked list. It always returns 86 * the address of the first record, so that you can use this function as an 87 * initialization function as well as an append function. If you find this 88 * bothersome, then simply create a separate _init function and call it 89 * appropriately from within the program. 90 */ 91struct curl_slist *curl_slist_append(struct curl_slist *list, 92 const char *data) 93{ 94 char *dupdata = strdup(data); 95 96 if(!dupdata) 97 return NULL; 98 99 list = Curl_slist_append_nodup(list, dupdata); 100 if(!list) 101 free(dupdata); 102 103 return list; 104} 105 106/* 107 * Curl_slist_duplicate() duplicates a linked list. It always returns the 108 * address of the first record of the cloned list or NULL in case of an 109 * error (or if the input list was NULL). 110 */ 111struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist) 112{ 113 struct curl_slist *outlist = NULL; 114 struct curl_slist *tmp; 115 116 while(inlist) { 117 tmp = curl_slist_append(outlist, inlist->data); 118 119 if(!tmp) { 120 curl_slist_free_all(outlist); 121 return NULL; 122 } 123 124 outlist = tmp; 125 inlist = inlist->next; 126 } 127 return outlist; 128} 129 130/* be nice and clean up resources */ 131void curl_slist_free_all(struct curl_slist *list) 132{ 133 struct curl_slist *next; 134 struct curl_slist *item; 135 136 if(!list) 137 return; 138 139 item = list; 140 do { 141 next = item->next; 142 Curl_safefree(item->data); 143 free(item); 144 item = next; 145 } while(next); 146} 147