argz.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Functions for dealing with '\0' separated arg vectors.
  2. Copyright (C) 1995-1998, 2000-2002, 2006, 2008-2017 Free Software
  3. Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, see <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include <argz.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /* Add BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
  21. error_t
  22. argz_append (char **argz, size_t *argz_len, const char *buf, size_t buf_len)
  23. {
  24. size_t new_argz_len = *argz_len + buf_len;
  25. char *new_argz = realloc (*argz, new_argz_len);
  26. if (new_argz)
  27. {
  28. memcpy (new_argz + *argz_len, buf, buf_len);
  29. *argz = new_argz;
  30. *argz_len = new_argz_len;
  31. return 0;
  32. }
  33. else
  34. return ENOMEM;
  35. }
  36. /* Add STR to the argz vector in ARGZ & ARGZ_LEN. This should be moved into
  37. argz.c in libshouldbelibc. */
  38. error_t
  39. argz_add (char **argz, size_t *argz_len, const char *str)
  40. {
  41. return argz_append (argz, argz_len, str, strlen (str) + 1);
  42. }
  43. error_t
  44. argz_add_sep (char **argz, size_t *argz_len, const char *string, int delim)
  45. {
  46. size_t nlen = strlen (string) + 1;
  47. if (nlen > 1)
  48. {
  49. const char *rp;
  50. char *wp;
  51. *argz = (char *) realloc (*argz, *argz_len + nlen);
  52. if (*argz == NULL)
  53. return ENOMEM;
  54. wp = *argz + *argz_len;
  55. rp = string;
  56. do
  57. if (*rp == delim)
  58. {
  59. if (wp > *argz && wp[-1] != '\0')
  60. *wp++ = '\0';
  61. else
  62. --nlen;
  63. }
  64. else
  65. *wp++ = *rp;
  66. while (*rp++ != '\0');
  67. *argz_len += nlen;
  68. }
  69. return 0;
  70. }
  71. error_t
  72. argz_create_sep (const char *string, int delim, char **argz, size_t *len)
  73. {
  74. size_t nlen = strlen (string) + 1;
  75. if (nlen > 1)
  76. {
  77. const char *rp;
  78. char *wp;
  79. *argz = (char *) malloc (nlen);
  80. if (*argz == NULL)
  81. return ENOMEM;
  82. rp = string;
  83. wp = *argz;
  84. do
  85. if (*rp == delim)
  86. {
  87. if (wp > *argz && wp[-1] != '\0')
  88. *wp++ = '\0';
  89. else
  90. --nlen;
  91. }
  92. else
  93. *wp++ = *rp;
  94. while (*rp++ != '\0');
  95. if (nlen == 0)
  96. {
  97. free (*argz);
  98. *argz = NULL;
  99. *len = 0;
  100. }
  101. *len = nlen;
  102. }
  103. else
  104. {
  105. *argz = NULL;
  106. *len = 0;
  107. }
  108. return 0;
  109. }
  110. /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
  111. existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
  112. Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
  113. ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
  114. in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
  115. ARGZ, ENOMEM is returned, else 0. */
  116. error_t
  117. argz_insert (char **argz, size_t *argz_len, char *before, const char *entry)
  118. {
  119. if (! before)
  120. return argz_add (argz, argz_len, entry);
  121. if (before < *argz || before >= *argz + *argz_len)
  122. return EINVAL;
  123. if (before > *argz)
  124. /* Make sure before is actually the beginning of an entry. */
  125. while (before[-1])
  126. before--;
  127. {
  128. size_t after_before = *argz_len - (before - *argz);
  129. size_t entry_len = strlen (entry) + 1;
  130. size_t new_argz_len = *argz_len + entry_len;
  131. char *new_argz = realloc (*argz, new_argz_len);
  132. if (new_argz)
  133. {
  134. before = new_argz + (before - *argz);
  135. memmove (before + entry_len, before, after_before);
  136. memmove (before, entry, entry_len);
  137. *argz = new_argz;
  138. *argz_len = new_argz_len;
  139. return 0;
  140. }
  141. else
  142. return ENOMEM;
  143. }
  144. }
  145. char *
  146. argz_next (const char *argz, size_t argz_len, const char *entry)
  147. {
  148. if (entry)
  149. {
  150. if (entry < argz + argz_len)
  151. entry = strchr (entry, '\0') + 1;
  152. return entry >= argz + argz_len ? NULL : (char *) entry;
  153. }
  154. else
  155. if (argz_len > 0)
  156. return (char *) argz;
  157. else
  158. return NULL;
  159. }
  160. /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
  161. except the last into the character SEP. */
  162. void
  163. argz_stringify (char *argz, size_t len, int sep)
  164. {
  165. if (len > 0)
  166. while (1)
  167. {
  168. size_t part_len = strnlen (argz, len);
  169. argz += part_len;
  170. len -= part_len;
  171. if (len-- <= 1) /* includes final '\0' we want to stop at */
  172. break;
  173. *argz++ = sep;
  174. }
  175. }
  176. /* Returns the number of strings in ARGZ. */
  177. size_t
  178. argz_count (const char *argz, size_t len)
  179. {
  180. size_t count = 0;
  181. while (len > 0)
  182. {
  183. size_t part_len = strlen (argz);
  184. argz += part_len + 1;
  185. len -= part_len + 1;
  186. count++;
  187. }
  188. return count;
  189. }
  190. /* Puts pointers to each string in ARGZ, plus a terminating 0 element, into
  191. ARGV, which must be large enough to hold them all. */
  192. void
  193. argz_extract (const char *argz, size_t len, char **argv)
  194. {
  195. while (len > 0)
  196. {
  197. size_t part_len = strlen (argz);
  198. *argv++ = (char *) argz;
  199. argz += part_len + 1;
  200. len -= part_len + 1;
  201. }
  202. *argv = 0;
  203. }
  204. /* Make a '\0' separated arg vector from a unix argv vector, returning it in
  205. ARGZ, and the total length in LEN. If a memory allocation error occurs,
  206. ENOMEM is returned, otherwise 0. */
  207. error_t
  208. argz_create (char *const argv[], char **argz, size_t *len)
  209. {
  210. int argc;
  211. size_t tlen = 0;
  212. char *const *ap;
  213. char *p;
  214. for (argc = 0; argv[argc] != NULL; ++argc)
  215. tlen += strlen (argv[argc]) + 1;
  216. if (tlen == 0)
  217. *argz = NULL;
  218. else
  219. {
  220. *argz = malloc (tlen);
  221. if (*argz == NULL)
  222. return ENOMEM;
  223. for (p = *argz, ap = argv; *ap; ++ap, ++p)
  224. p = stpcpy (p, *ap);
  225. }
  226. *len = tlen;
  227. return 0;
  228. }
  229. /* Delete ENTRY from ARGZ & ARGZ_LEN, if any. */
  230. void
  231. argz_delete (char **argz, size_t *argz_len, char *entry)
  232. {
  233. if (entry)
  234. /* Get rid of the old value for NAME. */
  235. {
  236. size_t entry_len = strlen (entry) + 1;
  237. *argz_len -= entry_len;
  238. memmove (entry, entry + entry_len, *argz_len - (entry - *argz));
  239. if (*argz_len == 0)
  240. {
  241. free (*argz);
  242. *argz = 0;
  243. }
  244. }
  245. }
  246. /* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
  247. updating *TO & *TO_LEN appropriately. If an allocation error occurs,
  248. *TO's old value is freed, and *TO is set to 0. */
  249. static void
  250. str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
  251. {
  252. size_t new_len = *to_len + buf_len;
  253. char *new_to = realloc (*to, new_len + 1);
  254. if (new_to)
  255. {
  256. *((char *) mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
  257. *to = new_to;
  258. *to_len = new_len;
  259. }
  260. else
  261. {
  262. free (*to);
  263. *to = 0;
  264. }
  265. }
  266. /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
  267. ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
  268. incremented by number of replacements performed. */
  269. error_t
  270. argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
  271. unsigned *replace_count)
  272. {
  273. error_t err = 0;
  274. if (str && *str)
  275. {
  276. char *arg = 0;
  277. char *src = *argz;
  278. size_t src_len = *argz_len;
  279. char *dst = 0;
  280. size_t dst_len = 0;
  281. int delayed_copy = 1; /* True while we've avoided copying anything. */
  282. size_t str_len = strlen (str), with_len = strlen (with);
  283. while (!err && (arg = argz_next (src, src_len, arg)))
  284. {
  285. char *match = strstr (arg, str);
  286. if (match)
  287. {
  288. char *from = match + str_len;
  289. size_t to_len = match - arg;
  290. char *to = strndup (arg, to_len);
  291. while (to && from)
  292. {
  293. str_append (&to, &to_len, with, with_len);
  294. if (to)
  295. {
  296. match = strstr (from, str);
  297. if (match)
  298. {
  299. str_append (&to, &to_len, from, match - from);
  300. from = match + str_len;
  301. }
  302. else
  303. {
  304. str_append (&to, &to_len, from, strlen (from));
  305. from = 0;
  306. }
  307. }
  308. }
  309. if (to)
  310. {
  311. if (delayed_copy)
  312. /* We avoided copying SRC to DST until we found a match;
  313. now that we've done so, copy everything from the start
  314. of SRC. */
  315. {
  316. if (arg > src)
  317. err = argz_append (&dst, &dst_len, src, (arg - src));
  318. delayed_copy = 0;
  319. }
  320. if (! err)
  321. err = argz_add (&dst, &dst_len, to);
  322. free (to);
  323. }
  324. else
  325. err = ENOMEM;
  326. if (replace_count)
  327. (*replace_count)++;
  328. }
  329. else if (! delayed_copy)
  330. err = argz_add (&dst, &dst_len, arg);
  331. }
  332. if (! err)
  333. {
  334. if (! delayed_copy)
  335. /* We never found any instances of str. */
  336. {
  337. free (src);
  338. *argz = dst;
  339. *argz_len = dst_len;
  340. }
  341. }
  342. else if (dst_len > 0)
  343. free (dst);
  344. }
  345. return err;
  346. }