slist.c 3.8 KB

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