1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include <curl/curl.h>
31 /* The last #include files should be: */
32 #include "curl_memory.h"
35 /* returns last node in linked list */
36 static struct curl_slist *slist_get_last(struct curl_slist *list)
38 struct curl_slist *item;
40 /* if caller passed us a NULL, return now */
44 /* loop through to find the last item */
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
61 struct curl_slist *Curl_slist_append_nodup(struct curl_slist *list, char *data)
63 struct curl_slist *last;
64 struct curl_slist *new_item;
68 new_item = malloc(sizeof(struct curl_slist));
72 new_item->next = NULL;
73 new_item->data = data;
75 /* if this is the first item, then new_item *is* the list */
79 last = slist_get_last(list);
80 last->next = new_item;
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.
91 struct curl_slist *curl_slist_append(struct curl_slist *list,
94 char *dupdata = strdup(data);
99 list = Curl_slist_append_nodup(list, dupdata);
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).
111 struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
113 struct curl_slist *outlist = NULL;
114 struct curl_slist *tmp;
117 tmp = curl_slist_append(outlist, inlist->data);
120 curl_slist_free_all(outlist);
125 inlist = inlist->next;
130 /* be nice and clean up resources */
131 void curl_slist_free_all(struct curl_slist *list)
133 struct curl_slist *next;
134 struct curl_slist *item;
142 Curl_safefree(item->data);