slist.c 3.8 KB

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