su_strdup.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@ingroup su_alloc
  25. *
  26. * @CFILE su_strdup.c Home-based string duplication functions
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. *
  30. * @date Created: Wed Jul 19 10:06:14 2000 ppessi
  31. */
  32. #include "config.h"
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include "sofia-sip/su_alloc.h"
  36. /** Duplicate a string, allocate memory from @a home.
  37. *
  38. * The function su_strdup() duplicates the string @a s. It allocates @c
  39. * strlen(s)+1 bytes from @a home, copies the contents of @a s to the newly
  40. * allocated memory, and returns pointer to the duplicated string.
  41. *
  42. * @param home pointer to memory home
  43. * @param s string to be duplicated
  44. *
  45. * @return The function su_strdup() returns pointer to the newly created
  46. * string, or @c NULL upon an error.
  47. */
  48. char *su_strdup(su_home_t *home, char const *s)
  49. {
  50. if (s) {
  51. size_t n = strlen(s);
  52. char *retval = su_alloc(home, n + 1);
  53. if (retval)
  54. snprintf(retval, n + 1, "%s", s);
  55. return retval;
  56. }
  57. return NULL;
  58. }
  59. /**Concate two strings, allocate memory for result from @a home.
  60. *
  61. * Concatenate the strings @a s1 and @a s2. The @c strlen(s1)+strlen(s2)+1
  62. * bytes is allocated from @a home, the contents of @a s1 and @a s2 is
  63. * copied to the newly allocated memory area, and pointer to the
  64. * concatenated string is returned.
  65. *
  66. * @param home pointer to memory home
  67. * @param s1 string to be first string
  68. * @param s2 string to be first string
  69. *
  70. * @return Pointer to the newly created string is returned, or @c NULL upon
  71. * an error.
  72. */
  73. char *su_strcat(su_home_t *home, char const *s1, char const *s2)
  74. {
  75. size_t n1, n2;
  76. char *retval;
  77. if (s1 == NULL)
  78. return su_strdup(home, s2);
  79. else if (s2 == NULL)
  80. return su_strdup(home, s1);
  81. n1 = strlen(s1); n2 = strlen(s2);
  82. retval = su_alloc(home, n1 + n2 + 1);
  83. if (retval) {
  84. memcpy(retval, s1, n1);
  85. memcpy(retval + n1, s2, n2);
  86. retval[n1 + n2] = '\0';
  87. }
  88. return retval;
  89. }
  90. /**Concate multiple strings, allocate memory for result from @a home.
  91. *
  92. * Concatenate the strings in list. The lenght of result is calculate,
  93. * result is allocated from @a home, the contents of strings is copied to
  94. * the newly allocated memory arex, and pointer to the concatenated string is
  95. * returned.
  96. *
  97. * @param home pointer to memory home
  98. * @param ... NULL-terminated list of strings to be concatenated
  99. *
  100. * @return Pointer to the newly created string is returned, or @c NULL upon
  101. * an error.
  102. */
  103. char *su_strcat_all(su_home_t *home, ...)
  104. {
  105. int i, n;
  106. size_t size = 0;
  107. va_list va;
  108. char *s, *retval, *end;
  109. /* Count number arguments and their size */
  110. va_start(va, home);
  111. s = va_arg(va, char *);
  112. for (n = 0; s; s = va_arg(va, char *), n++)
  113. size += strlen(s);
  114. va_end(va);
  115. retval = su_alloc(home, size + 1);
  116. if (retval) {
  117. s = retval;
  118. end = s + size + 1;
  119. va_start(va, home);
  120. for (i = 0; i < n; i++)
  121. s = (char *)memccpy(s, va_arg(va, char const *), '\0', end - s) - 1;
  122. va_end(va);
  123. retval[size] = '\0';
  124. }
  125. return retval;
  126. }
  127. /** Duplicate a string with given size, allocate memory from @a home.
  128. *
  129. * The function su_strndup() duplicates the string @a s. It allocates @c n+1
  130. * bytes from @a home, copies the contents of @a s to the newly allocated
  131. * memory, and returns pointer to the duplicated string. The duplicated
  132. * string is always NUL-terminated.
  133. *
  134. * @param home pointer to memory home
  135. * @param s string to be duplicated
  136. * @param n size of the resulting string
  137. *
  138. * @return The function su_strndup() returns pointer to the newly created
  139. * string, or @c NULL upon an error.
  140. */
  141. char *su_strndup(su_home_t *home, char const *s, isize_t n)
  142. {
  143. if (s) {
  144. char *retval = su_alloc(home, n + 1);
  145. if (retval)
  146. strncpy(retval, s, n)[n] = 0;
  147. return retval;
  148. }
  149. return NULL;
  150. }