113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include <curl/curl.h>
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include "slist.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci/* The last #include files should be: */
3213498266Sopenharmony_ci#include "curl_memory.h"
3313498266Sopenharmony_ci#include "memdebug.h"
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci/* returns last node in linked list */
3613498266Sopenharmony_cistatic struct curl_slist *slist_get_last(struct curl_slist *list)
3713498266Sopenharmony_ci{
3813498266Sopenharmony_ci  struct curl_slist     *item;
3913498266Sopenharmony_ci
4013498266Sopenharmony_ci  /* if caller passed us a NULL, return now */
4113498266Sopenharmony_ci  if(!list)
4213498266Sopenharmony_ci    return NULL;
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci  /* loop through to find the last item */
4513498266Sopenharmony_ci  item = list;
4613498266Sopenharmony_ci  while(item->next) {
4713498266Sopenharmony_ci    item = item->next;
4813498266Sopenharmony_ci  }
4913498266Sopenharmony_ci  return item;
5013498266Sopenharmony_ci}
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci/*
5313498266Sopenharmony_ci * Curl_slist_append_nodup() appends a string to the linked list. Rather than
5413498266Sopenharmony_ci * copying the string in dynamic storage, it takes its ownership. The string
5513498266Sopenharmony_ci * should have been malloc()ated. Curl_slist_append_nodup always returns
5613498266Sopenharmony_ci * the address of the first record, so that you can use this function as an
5713498266Sopenharmony_ci * initialization function as well as an append function.
5813498266Sopenharmony_ci * If an error occurs, NULL is returned and the string argument is NOT
5913498266Sopenharmony_ci * released.
6013498266Sopenharmony_ci */
6113498266Sopenharmony_cistruct curl_slist *Curl_slist_append_nodup(struct curl_slist *list, char *data)
6213498266Sopenharmony_ci{
6313498266Sopenharmony_ci  struct curl_slist     *last;
6413498266Sopenharmony_ci  struct curl_slist     *new_item;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  DEBUGASSERT(data);
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  new_item = malloc(sizeof(struct curl_slist));
6913498266Sopenharmony_ci  if(!new_item)
7013498266Sopenharmony_ci    return NULL;
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  new_item->next = NULL;
7313498266Sopenharmony_ci  new_item->data = data;
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  /* if this is the first item, then new_item *is* the list */
7613498266Sopenharmony_ci  if(!list)
7713498266Sopenharmony_ci    return new_item;
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci  last = slist_get_last(list);
8013498266Sopenharmony_ci  last->next = new_item;
8113498266Sopenharmony_ci  return list;
8213498266Sopenharmony_ci}
8313498266Sopenharmony_ci
8413498266Sopenharmony_ci/*
8513498266Sopenharmony_ci * curl_slist_append() appends a string to the linked list. It always returns
8613498266Sopenharmony_ci * the address of the first record, so that you can use this function as an
8713498266Sopenharmony_ci * initialization function as well as an append function. If you find this
8813498266Sopenharmony_ci * bothersome, then simply create a separate _init function and call it
8913498266Sopenharmony_ci * appropriately from within the program.
9013498266Sopenharmony_ci */
9113498266Sopenharmony_cistruct curl_slist *curl_slist_append(struct curl_slist *list,
9213498266Sopenharmony_ci                                     const char *data)
9313498266Sopenharmony_ci{
9413498266Sopenharmony_ci  char *dupdata = strdup(data);
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  if(!dupdata)
9713498266Sopenharmony_ci    return NULL;
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci  list = Curl_slist_append_nodup(list, dupdata);
10013498266Sopenharmony_ci  if(!list)
10113498266Sopenharmony_ci    free(dupdata);
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  return list;
10413498266Sopenharmony_ci}
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci/*
10713498266Sopenharmony_ci * Curl_slist_duplicate() duplicates a linked list. It always returns the
10813498266Sopenharmony_ci * address of the first record of the cloned list or NULL in case of an
10913498266Sopenharmony_ci * error (or if the input list was NULL).
11013498266Sopenharmony_ci */
11113498266Sopenharmony_cistruct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
11213498266Sopenharmony_ci{
11313498266Sopenharmony_ci  struct curl_slist *outlist = NULL;
11413498266Sopenharmony_ci  struct curl_slist *tmp;
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci  while(inlist) {
11713498266Sopenharmony_ci    tmp = curl_slist_append(outlist, inlist->data);
11813498266Sopenharmony_ci
11913498266Sopenharmony_ci    if(!tmp) {
12013498266Sopenharmony_ci      curl_slist_free_all(outlist);
12113498266Sopenharmony_ci      return NULL;
12213498266Sopenharmony_ci    }
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci    outlist = tmp;
12513498266Sopenharmony_ci    inlist = inlist->next;
12613498266Sopenharmony_ci  }
12713498266Sopenharmony_ci  return outlist;
12813498266Sopenharmony_ci}
12913498266Sopenharmony_ci
13013498266Sopenharmony_ci/* be nice and clean up resources */
13113498266Sopenharmony_civoid curl_slist_free_all(struct curl_slist *list)
13213498266Sopenharmony_ci{
13313498266Sopenharmony_ci  struct curl_slist     *next;
13413498266Sopenharmony_ci  struct curl_slist     *item;
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  if(!list)
13713498266Sopenharmony_ci    return;
13813498266Sopenharmony_ci
13913498266Sopenharmony_ci  item = list;
14013498266Sopenharmony_ci  do {
14113498266Sopenharmony_ci    next = item->next;
14213498266Sopenharmony_ci    Curl_safefree(item->data);
14313498266Sopenharmony_ci    free(item);
14413498266Sopenharmony_ci    item = next;
14513498266Sopenharmony_ci  } while(next);
14613498266Sopenharmony_ci}
147