lt__argz.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* lt__argz.c -- argz implementation for non-glibc systems
  2. Copyright (C) 2004, 2006-2008, 2011-2015 Free Software Foundation,
  3. Inc.
  4. Written by Gary V. Vaughan, 2004
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #if defined LTDL && defined LT_CONFIG_H
  26. # include LT_CONFIG_H
  27. #else
  28. # include <config.h>
  29. #endif
  30. #include <lt__argz.h>
  31. #include <assert.h>
  32. #include <stdlib.h>
  33. #include <sys/types.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #define EOS_CHAR '\0'
  37. error_t
  38. argz_append (char **pargz, size_t *pargz_len, const char *buf, size_t buf_len)
  39. {
  40. size_t argz_len;
  41. char *argz;
  42. assert (pargz);
  43. assert (pargz_len);
  44. assert ((*pargz && *pargz_len) || (!*pargz && !*pargz_len));
  45. /* If nothing needs to be appended, no more work is required. */
  46. if (buf_len == 0)
  47. return 0;
  48. /* Ensure there is enough room to append BUF_LEN. */
  49. argz_len = *pargz_len + buf_len;
  50. argz = (char *) realloc (*pargz, argz_len);
  51. if (!argz)
  52. return ENOMEM;
  53. /* Copy characters from BUF after terminating '\0' in ARGZ. */
  54. memcpy (argz + *pargz_len, buf, buf_len);
  55. /* Assign new values. */
  56. *pargz = argz;
  57. *pargz_len = argz_len;
  58. return 0;
  59. }
  60. error_t
  61. argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
  62. {
  63. size_t argz_len;
  64. char *argz = 0;
  65. assert (str);
  66. assert (pargz);
  67. assert (pargz_len);
  68. /* Make a copy of STR, but replacing each occurrence of
  69. DELIM with '\0'. */
  70. argz_len = 1+ strlen (str);
  71. if (argz_len)
  72. {
  73. const char *p;
  74. char *q;
  75. argz = (char *) malloc (argz_len);
  76. if (!argz)
  77. return ENOMEM;
  78. for (p = str, q = argz; *p != EOS_CHAR; ++p)
  79. {
  80. if (*p == delim)
  81. {
  82. /* Ignore leading delimiters, and fold consecutive
  83. delimiters in STR into a single '\0' in ARGZ. */
  84. if ((q > argz) && (q[-1] != EOS_CHAR))
  85. *q++ = EOS_CHAR;
  86. else
  87. --argz_len;
  88. }
  89. else
  90. *q++ = *p;
  91. }
  92. /* Copy terminating EOS_CHAR. */
  93. *q = *p;
  94. }
  95. /* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory. */
  96. if (!argz_len)
  97. argz = (free (argz), (char *) 0);
  98. /* Assign new values. */
  99. *pargz = argz;
  100. *pargz_len = argz_len;
  101. return 0;
  102. }
  103. error_t
  104. argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
  105. {
  106. assert (pargz);
  107. assert (pargz_len);
  108. assert (entry && *entry);
  109. /* No BEFORE address indicates ENTRY should be inserted after the
  110. current last element. */
  111. if (!before)
  112. return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
  113. /* This probably indicates a programmer error, but to preserve
  114. semantics, scan back to the start of an entry if BEFORE points
  115. into the middle of it. */
  116. while ((before > *pargz) && (before[-1] != EOS_CHAR))
  117. --before;
  118. {
  119. size_t entry_len = 1+ strlen (entry);
  120. size_t argz_len = *pargz_len + entry_len;
  121. size_t offset = before - *pargz;
  122. char *argz = (char *) realloc (*pargz, argz_len);
  123. if (!argz)
  124. return ENOMEM;
  125. /* Make BEFORE point to the equivalent offset in ARGZ that it
  126. used to have in *PARGZ incase realloc() moved the block. */
  127. before = argz + offset;
  128. /* Move the ARGZ entries starting at BEFORE up into the new
  129. space at the end -- making room to copy ENTRY into the
  130. resulting gap. */
  131. memmove (before + entry_len, before, *pargz_len - offset);
  132. memcpy (before, entry, entry_len);
  133. /* Assign new values. */
  134. *pargz = argz;
  135. *pargz_len = argz_len;
  136. }
  137. return 0;
  138. }
  139. char *
  140. argz_next (char *argz, size_t argz_len, const char *entry)
  141. {
  142. assert ((argz && argz_len) || (!argz && !argz_len));
  143. if (entry)
  144. {
  145. /* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
  146. within the ARGZ vector. */
  147. assert ((!argz && !argz_len)
  148. || ((argz <= entry) && (entry < (argz + argz_len))));
  149. /* Move to the char immediately after the terminating
  150. '\0' of ENTRY. */
  151. entry = 1+ strchr (entry, EOS_CHAR);
  152. /* Return either the new ENTRY, or else NULL if ARGZ is
  153. exhausted. */
  154. return (entry >= argz + argz_len) ? 0 : (char *) entry;
  155. }
  156. else
  157. {
  158. /* This should probably be flagged as a programmer error,
  159. since starting an argz_next loop with the iterator set
  160. to ARGZ is safer. To preserve semantics, handle the NULL
  161. case by returning the start of ARGZ (if any). */
  162. if (argz_len > 0)
  163. return argz;
  164. else
  165. return 0;
  166. }
  167. }
  168. void
  169. argz_stringify (char *argz, size_t argz_len, int sep)
  170. {
  171. assert ((argz && argz_len) || (!argz && !argz_len));
  172. if (sep)
  173. {
  174. --argz_len; /* don't stringify the terminating EOS */
  175. while (--argz_len > 0)
  176. {
  177. if (argz[argz_len] == EOS_CHAR)
  178. argz[argz_len] = sep;
  179. }
  180. }
  181. }