summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2005-09-26 17:42:12 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:38:59 -0500
commit85abc86b214518b9b4d0728f3f0174bee0154c3c (patch)
treee76537b3c9b9f8706ad6691ff880b780ec760c88 /source4/lib
parent49839f356f493d0de1b719c8c3bfdee4713c0728 (diff)
downloadsamba-85abc86b214518b9b4d0728f3f0174bee0154c3c.tar.gz
samba-85abc86b214518b9b4d0728f3f0174bee0154c3c.tar.bz2
samba-85abc86b214518b9b4d0728f3f0174bee0154c3c.zip
r10514: Add str_list_make_shell() and str_list_join_shell()
(This used to be commit 8b86a5da73d38764deb8c1f639322b2911736f97)
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/util_strlist.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/source4/lib/util_strlist.c b/source4/lib/util_strlist.c
index 8efa1f92d2..7aff027d66 100644
--- a/source4/lib/util_strlist.c
+++ b/source4/lib/util_strlist.c
@@ -2,6 +2,7 @@
Unix SMB/CIFS implementation.
Copyright (C) Andrew Tridgell 2005
+ Copyright (C) Jelmer Vernooij 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -70,6 +71,63 @@ const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *
return ret;
}
+/* build a null terminated list of strings from an argv-like input string
+ Entries are seperated by spaces and can be enclosed by quotes.
+ Does NOT support escaping
+ */
+const char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string)
+{
+ int num_elements = 0;
+ const char **ret = NULL;
+
+ ret = talloc_array(mem_ctx, const char *, 1);
+ if (ret == NULL) {
+ return NULL;
+ }
+
+ while (string && *string) {
+ size_t len = strcspn(string, " ");
+ char *element;
+ const char **ret2;
+
+ if (len == 0) {
+ string += strspn(string, " ");
+ continue;
+ }
+
+ if (*string == '\"') {
+ string++;
+ len = strcspn(string, "\"");
+ element = talloc_strndup(ret, string, len);
+ string += len + 1;
+ } else {
+ element = talloc_strndup(ret, string, len);
+ string += len;
+ }
+
+ if (element == NULL) {
+ talloc_free(ret);
+ return NULL;
+ }
+
+ ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
+ if (ret2 == NULL) {
+ talloc_free(ret);
+ return NULL;
+ }
+ ret = ret2;
+
+ ret[num_elements] = element;
+
+ num_elements++;
+ }
+
+ ret[num_elements] = NULL;
+
+ return ret;
+
+}
+
/* join a list back to one string */
char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
{
@@ -88,6 +146,30 @@ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
return ret;
}
+/* join a list back to one (shell-like) string; entries
+ * seperated by spaces, using quotes where necessary */
+char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list)
+{
+ char *ret = NULL;
+ int i;
+
+ if (list[0] == NULL)
+ return talloc_strdup(mem_ctx, "");
+
+ if (strchr(list[0], ' ') || strlen(list[0]) == 0)
+ ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
+ else
+ ret = talloc_strdup(mem_ctx, list[0]);
+
+ for (i = 1; list[i]; i++) {
+ if (strchr(list[i], ' ') || strlen(list[i]) == 0)
+ ret = talloc_asprintf_append(ret, " \"%s\"", list[i]);
+ else
+ ret = talloc_asprintf_append(ret, " %s", list[i]);
+ }
+
+ return ret;
+}
/*
return the number of elements in a string list