VERSION: Bump version up to Samba 4.17.8...
[Samba.git] / lib / util / util_strlist.c
blobb56e17379cb04c877b2099250990d164b32df4e8
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Jelmer Vernooij 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "replace.h"
22 #include "debug.h"
23 #include "tsort.h"
25 #include "util_strlist.h"
27 #undef strcasecmp
29 /**
30 * @file
31 * @brief String list manipulation
34 /**
35 build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
37 _PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx)
39 char **ret = talloc_zero_array(mem_ctx, char *, 1);
40 return ret;
43 /**
44 place the only element 'entry' into a new, NULL terminated string list
46 _PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry)
48 char **ret = NULL;
50 ret = talloc_array(mem_ctx, char *, 2);
51 if (ret == NULL) {
52 return NULL;
55 ret[0] = talloc_strdup(ret, entry);
56 if (!ret[0]) {
57 talloc_free(ret);
58 return NULL;
60 ret[1] = NULL;
62 return ret;
65 /**
66 build a null terminated list of strings from a input string and a
67 separator list. The separator list must contain characters less than
68 or equal to 0x2f for this to work correctly on multi-byte strings
70 _PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
72 int num_elements = 0;
73 char **ret = NULL;
75 if (sep == NULL) {
76 sep = LIST_SEP;
79 ret = talloc_array(mem_ctx, char *, 1);
80 if (ret == NULL) {
81 return NULL;
84 while (string && *string) {
85 size_t len = strcspn(string, sep);
86 char **ret2;
88 if (len == 0) {
89 string += strspn(string, sep);
90 continue;
93 ret2 = talloc_realloc(mem_ctx, ret, char *,
94 num_elements+2);
95 if (ret2 == NULL) {
96 talloc_free(ret);
97 return NULL;
99 ret = ret2;
101 ret[num_elements] = talloc_strndup(ret, string, len);
102 if (ret[num_elements] == NULL) {
103 talloc_free(ret);
104 return NULL;
107 num_elements++;
108 string += len;
111 ret[num_elements] = NULL;
113 return ret;
117 * build a null terminated list of strings from an argv-like input string
118 * Entries are separated by spaces and can be enclosed by quotes.
119 * Does NOT support escaping
121 _PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
123 int num_elements = 0;
124 char **ret = NULL;
126 ret = talloc_array(mem_ctx, char *, 1);
127 if (ret == NULL) {
128 return NULL;
131 if (sep == NULL)
132 sep = " \t\n\r";
134 while (string && *string) {
135 size_t len = strcspn(string, sep);
136 char *element;
137 char **ret2;
139 if (len == 0) {
140 string += strspn(string, sep);
141 continue;
144 if (*string == '\"') {
145 string++;
146 len = strcspn(string, "\"");
147 element = talloc_strndup(ret, string, len);
148 string += len + 1;
149 } else {
150 element = talloc_strndup(ret, string, len);
151 string += len;
154 if (element == NULL) {
155 talloc_free(ret);
156 return NULL;
159 ret2 = talloc_realloc(mem_ctx, ret, char *, num_elements+2);
160 if (ret2 == NULL) {
161 talloc_free(ret);
162 return NULL;
164 ret = ret2;
166 ret[num_elements] = element;
168 num_elements++;
171 ret[num_elements] = NULL;
173 return ret;
178 * join a list back to one string
180 _PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char separator)
182 char *ret = NULL;
183 int i;
185 if (list[0] == NULL)
186 return talloc_strdup(mem_ctx, "");
188 ret = talloc_strdup(mem_ctx, list[0]);
190 for (i = 1; list[i]; i++) {
191 ret = talloc_asprintf_append_buffer(ret, "%c%s", separator, list[i]);
194 return ret;
197 /** join a list back to one (shell-like) string; entries
198 * separated by spaces, using quotes where necessary */
199 _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
201 char *ret = NULL;
202 int i;
204 if (list[0] == NULL)
205 return talloc_strdup(mem_ctx, "");
207 if (strchr(list[0], ' ') || strlen(list[0]) == 0)
208 ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
209 else
210 ret = talloc_strdup(mem_ctx, list[0]);
212 for (i = 1; list[i]; i++) {
213 if (strchr(list[i], ' ') || strlen(list[i]) == 0)
214 ret = talloc_asprintf_append_buffer(ret, "%c\"%s\"", sep, list[i]);
215 else
216 ret = talloc_asprintf_append_buffer(ret, "%c%s", sep, list[i]);
219 return ret;
223 return the number of elements in a string list
225 _PUBLIC_ size_t str_list_length(const char * const *list)
227 size_t ret;
228 for (ret=0;list && list[ret];ret++) /* noop */ ;
229 return ret;
234 copy a string list
236 _PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
238 int i;
239 char **ret;
241 if (list == NULL)
242 return NULL;
244 ret = talloc_array(mem_ctx, char *, str_list_length(list)+1);
245 if (ret == NULL)
246 return NULL;
248 for (i=0;list && list[i];i++) {
249 ret[i] = talloc_strdup(ret, list[i]);
250 if (ret[i] == NULL) {
251 talloc_free(ret);
252 return NULL;
255 ret[i] = NULL;
256 return ret;
260 Return true if all the elements of the list match exactly.
262 _PUBLIC_ bool str_list_equal(const char * const *list1,
263 const char * const *list2)
265 int i;
267 if (list1 == NULL || list2 == NULL) {
268 return (list1 == list2);
271 for (i=0;list1[i] && list2[i];i++) {
272 if (strcmp(list1[i], list2[i]) != 0) {
273 return false;
276 if (list1[i] || list2[i]) {
277 return false;
279 return true;
284 add an entry to a string list
286 _PUBLIC_ const char **str_list_add(const char **list, const char *s)
288 size_t len = str_list_length(list);
289 const char **ret;
291 ret = talloc_realloc(NULL, list, const char *, len+2);
292 if (ret == NULL) return NULL;
294 ret[len] = talloc_strdup(ret, s);
295 if (ret[len] == NULL) return NULL;
297 ret[len+1] = NULL;
299 return ret;
303 * @brief Extend a talloc'ed string list with a printf'ed string
305 * str_list_add_printf() does nothing if *plist is NULL and it sets
306 * *plist to NULL on failure. It is designed to avoid intermediate
307 * NULL checks:
309 * argv = str_list_make_empty(ctx);
310 * str_list_add_printf(&argv, "smbstatus");
311 * str_list_add_printf(&argv, "--configfile=%s", config);
312 * if (argv == NULL) {
313 * goto nomem;
316 * @param[in,out] plist The talloc'ed list to extend
317 * @param[in] fmt The format string
319 void str_list_add_printf(char ***plist, const char *fmt, ...)
321 char **list = *plist;
322 size_t len;
323 char **tmp = NULL;
324 va_list ap;
326 if (list == NULL) {
327 return;
329 len = str_list_length((const char * const *)list);
331 tmp = talloc_realloc(NULL, list, char *, len+2);
332 if (tmp == NULL) {
333 goto fail;
335 list = tmp;
336 list[len+1] = NULL;
338 va_start(ap, fmt);
339 list[len] = talloc_vasprintf(list, fmt, ap);
340 va_end(ap);
342 if (list[len] == NULL) {
343 goto fail;
345 *plist = list;
347 return;
348 fail:
349 TALLOC_FREE(list);
350 *plist = NULL;
354 remove an entry from a string list
356 _PUBLIC_ void str_list_remove(const char **list, const char *s)
358 int i;
360 for (i=0;list[i];i++) {
361 if (strcmp(list[i], s) == 0) break;
363 if (!list[i]) return;
365 for (;list[i];i++) {
366 list[i] = list[i+1];
372 return true if a string is in a list
374 _PUBLIC_ bool str_list_check(const char **list, const char *s)
376 int i;
378 for (i=0; list != NULL && list[i] != NULL; i++) {
379 if (strcmp(list[i], s) == 0) return true;
381 return false;
385 return true if a string is in a list, case insensitively
387 _PUBLIC_ bool str_list_check_ci(const char **list, const char *s)
389 int i;
391 for (i=0; list != NULL && list[i] != NULL; i++) {
392 if (strcasecmp(list[i], s) == 0) return true;
394 return false;
399 append one list to another - expanding list1
401 _PUBLIC_ const char **str_list_append(const char **list1,
402 const char * const *list2)
404 size_t len1 = str_list_length(list1);
405 size_t len2 = str_list_length(list2);
406 const char **ret;
407 size_t i;
409 ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
410 if (ret == NULL) return NULL;
412 for (i=len1;i<len1+len2;i++) {
413 ret[i] = talloc_strdup(ret, list2[i-len1]);
414 if (ret[i] == NULL) {
415 return NULL;
418 ret[i] = NULL;
420 return ret;
423 static int list_cmp(const char **el1, const char **el2)
425 return strcmp(*el1, *el2);
429 return a list that only contains the unique elements of a list,
430 removing any duplicates
432 _PUBLIC_ const char **str_list_unique(const char **list)
434 size_t len = str_list_length(list);
435 const char **list2;
436 size_t i, j;
437 if (len < 2) {
438 return list;
440 list2 = (const char **)talloc_memdup(list, list,
441 sizeof(list[0])*(len+1));
442 TYPESAFE_QSORT(list2, len, list_cmp);
443 list[0] = list2[0];
444 for (i=j=1;i<len;i++) {
445 if (strcmp(list2[i], list[j-1]) != 0) {
446 list[j] = list2[i];
447 j++;
450 list[j] = NULL;
451 list = talloc_realloc(NULL, list, const char *, j + 1);
452 talloc_free(list2);
453 return list;
457 very useful when debugging complex list related code
459 _PUBLIC_ void str_list_show(const char **list)
461 int i;
462 DEBUG(0,("{ "));
463 for (i=0;list && list[i];i++) {
464 DEBUG(0,("\"%s\", ", list[i]));
466 DEBUG(0,("}\n"));
472 append one list to another - expanding list1
473 this assumes the elements of list2 are const pointers, so we can re-use them
475 _PUBLIC_ const char **str_list_append_const(const char **list1,
476 const char **list2)
478 size_t len1 = str_list_length(list1);
479 size_t len2 = str_list_length(list2);
480 const char **ret;
481 size_t i;
483 ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
484 if (ret == NULL) return NULL;
486 for (i=len1;i<len1+len2;i++) {
487 ret[i] = list2[i-len1];
489 ret[i] = NULL;
491 return ret;
495 * Add a string to an array of strings.
497 * num should be a pointer to an integer that holds the current
498 * number of elements in strings. It will be updated by this function.
500 _PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
501 const char *str, const char ***strings, size_t *num)
503 char *dup_str = talloc_strdup(mem_ctx, str);
505 *strings = talloc_realloc(mem_ctx,
506 *strings,
507 const char *, ((*num)+1));
509 if ((*strings == NULL) || (dup_str == NULL)) {
510 *num = 0;
511 return false;
514 (*strings)[*num] = dup_str;
515 *num += 1;
517 return true;
521 add an entry to a string list
522 this assumes s will not change
524 _PUBLIC_ const char **str_list_add_const(const char **list, const char *s)
526 size_t len = str_list_length(list);
527 const char **ret;
529 ret = talloc_realloc(NULL, list, const char *, len+2);
530 if (ret == NULL) return NULL;
532 ret[len] = s;
533 ret[len+1] = NULL;
535 return ret;
539 copy a string list
540 this assumes list will not change
542 _PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
543 const char **list)
545 int i;
546 const char **ret;
548 if (list == NULL)
549 return NULL;
551 ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
552 if (ret == NULL)
553 return NULL;
555 for (i=0;list && list[i];i++) {
556 ret[i] = list[i];
558 ret[i] = NULL;
559 return ret;