From 4f98b36562fb02f95c9af7af6fde548334ce9c34 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Wed, 8 Feb 2017 14:28:28 +0100 Subject: [PATCH 02/15] split_on_separator: move to a separate file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To be able to include split_on_separator() without additional dependencies (only talloc), it is moved into a separate file. Related to https://pagure.io/SSSD/sssd/issue/3050 Reviewed-by: Jakub Hrozek Reviewed-by: Lukáš Slebodník --- Makefile.am | 30 ++++++++++--- src/util/util.c | 93 ---------------------------------------- src/util/util_ext.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 100 deletions(-) create mode 100644 src/util/util_ext.c diff --git a/Makefile.am b/Makefile.am index 45b04de2638a745a189c0b4e5794ccd29913b10d..6dae4f2dd7f2dee501add82c7ab4f15fcbcc59ac 100644 --- a/Makefile.am +++ b/Makefile.am @@ -987,6 +987,7 @@ libsss_util_la_SOURCES = \ src/sbus/sssd_dbus_common_signals.c \ src/sbus/sssd_dbus_utils.c \ src/util/util.c \ + src/util/util_ext.c \ src/util/memory.c \ src/util/safe-format-string.c \ src/util/server.c \ @@ -2355,19 +2356,23 @@ test_authtok_SOURCES = \ src/tests/cmocka/test_authtok.c \ src/util/authtok.c \ src/util/authtok-utils.c \ - src/util/util.c + src/util/util.c \ + src/util/util_ext.c \ + $(NULL) test_authtok_CFLAGS = \ $(AM_CFLAGS) \ $(TALLOC_CFLAGS) \ $(POPT_CFLAGS) \ - $(DHASH_CFLAGS) + $(DHASH_CFLAGS) \ + $(NULL) test_authtok_LDADD = \ $(TALLOC_LIBS) \ $(CMOCKA_LIBS) \ $(DHASH_LIBS) \ $(POPT_LIBS) \ libsss_test_common.la \ - libsss_debug.la + libsss_debug.la \ + $(NULL) sss_nss_idmap_tests_SOURCES = \ src/tests/cmocka/sss_nss_idmap-tests.c @@ -2839,6 +2844,7 @@ test_child_common_SOURCES = \ src/util/atomic_io.c \ src/util/util_errors.c \ src/util/util.c \ + src/util/util_ext.c \ $(NULL) test_child_common_CFLAGS = \ $(AM_CFLAGS) \ @@ -3774,6 +3780,7 @@ krb5_child_SOURCES = \ src/util/authtok.c \ src/util/authtok-utils.c \ src/util/util.c \ + src/util/util_ext.c \ src/util/signal.c \ src/util/strtonum.c \ src/util/become_user.c \ @@ -3807,6 +3814,7 @@ ldap_child_SOURCES = \ src/util/authtok.c \ src/util/authtok-utils.c \ src/util/util.c \ + src/util/util_ext.c \ src/util/signal.c \ src/util/become_user.c \ $(NULL) @@ -3827,6 +3835,7 @@ selinux_child_SOURCES = \ src/util/sss_semanage.c \ src/util/atomic_io.c \ src/util/util.c \ + src/util/util_ext.c \ $(NULL) selinux_child_CFLAGS = \ $(AM_CFLAGS) \ @@ -3845,6 +3854,7 @@ gpo_child_SOURCES = \ src/providers/ad/ad_gpo_child.c \ src/util/atomic_io.c \ src/util/util.c \ + src/util/util_ext.c \ src/util/signal.c gpo_child_CFLAGS = \ $(AM_CFLAGS) \ @@ -3876,6 +3886,7 @@ p11_child_SOURCES = \ src/p11_child/p11_child_nss.c \ src/util/atomic_io.c \ src/util/util.c \ + src/util/util_ext.c \ $(NULL) p11_child_CFLAGS = \ $(AM_CFLAGS) \ @@ -3893,16 +3904,21 @@ p11_child_LDADD = \ memberof_la_SOURCES = \ src/ldb_modules/memberof.c \ - src/util/util.c + src/util/util.c \ + src/util/util_ext.c \ + $(NULL) memberof_la_CFLAGS = \ - $(AM_CFLAGS) + $(AM_CFLAGS) \ + $(NULL) memberof_la_LIBADD = \ libsss_debug.la \ $(LDB_LIBS) \ - $(DHASH_LIBS) + $(DHASH_LIBS) \ + $(NULL) memberof_la_LDFLAGS = \ -avoid-version \ - -module + -module \ + $(NULL) if BUILD_KRB5_LOCATOR_PLUGIN sssd_krb5_locator_plugin_la_SOURCES = \ diff --git a/src/util/util.c b/src/util/util.c index a528f0c0249c33bfc3d3275250e74d5edcef2e6f..9d6202f695d516f20d648621da81a2d5e746daa5 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -35,99 +35,6 @@ int socket_activated = 0; int dbus_activated = 0; -int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, - const char sep, bool trim, bool skip_empty, - char ***_list, int *size) -{ - int ret; - const char *substr_end = str; - const char *substr_begin = str; - const char *sep_pos = NULL; - size_t substr_len; - char **list = NULL; - int num_strings = 0; - TALLOC_CTX *tmp_ctx = NULL; - - if (str == NULL || *str == '\0' || _list == NULL) { - return EINVAL; - } - - tmp_ctx = talloc_new(NULL); - if (tmp_ctx == NULL) { - return ENOMEM; - } - - do { - substr_len = 0; - - /* If this is not the first substring, then move from the separator. */ - if (sep_pos != NULL) { - substr_end = sep_pos + 1; - substr_begin = sep_pos + 1; - } - - /* Find end of the first substring */ - while (*substr_end != sep && *substr_end != '\0') { - substr_end++; - substr_len++; - } - - sep_pos = substr_end; - - if (trim) { - /* Trim leading whitespace */ - while (isspace(*substr_begin) && substr_begin < substr_end) { - substr_begin++; - substr_len--; - } - - /* Trim trailing whitespace */ - while (substr_end - 1 > substr_begin && isspace(*(substr_end-1))) { - substr_end--; - substr_len--; - } - } - - /* Copy the substring to the output list of strings */ - if (skip_empty == false || substr_len > 0) { - list = talloc_realloc(tmp_ctx, list, char*, num_strings + 2); - if (list == NULL) { - ret = ENOMEM; - goto done; - } - - /* empty string is stored for substr_len == 0 */ - list[num_strings] = talloc_strndup(list, substr_begin, substr_len); - if (list[num_strings] == NULL) { - ret = ENOMEM; - goto done; - } - num_strings++; - } - - } while (*sep_pos != '\0'); - - if (list == NULL) { - /* No allocations were done, make space for the NULL */ - list = talloc(tmp_ctx, char *); - if (list == NULL) { - ret = ENOMEM; - goto done; - } - } - list[num_strings] = NULL; - - if (size) { - *size = num_strings; - } - - *_list = talloc_steal(mem_ctx, list); - ret = EOK; -done: - talloc_free(tmp_ctx); - return ret; -} - static void free_args(char **args) { int i; diff --git a/src/util/util_ext.c b/src/util/util_ext.c new file mode 100644 index 0000000000000000000000000000000000000000..fceb8c873a26471d476b39d5d4e567c445ed8d0b --- /dev/null +++ b/src/util/util_ext.c @@ -0,0 +1,121 @@ +/* + SSSD helper calls - can be used by libraries for external use as well + + Authors: + Simo Sorce + + Copyright (C) 2017 Red Hat + + 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 + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +#define EOK 0 + +int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, + const char sep, bool trim, bool skip_empty, + char ***_list, int *size) +{ + int ret; + const char *substr_end = str; + const char *substr_begin = str; + const char *sep_pos = NULL; + size_t substr_len; + char **list = NULL; + int num_strings = 0; + TALLOC_CTX *tmp_ctx = NULL; + + if (str == NULL || *str == '\0' || _list == NULL) { + return EINVAL; + } + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return ENOMEM; + } + + do { + substr_len = 0; + + /* If this is not the first substring, then move from the separator. */ + if (sep_pos != NULL) { + substr_end = sep_pos + 1; + substr_begin = sep_pos + 1; + } + + /* Find end of the first substring */ + while (*substr_end != sep && *substr_end != '\0') { + substr_end++; + substr_len++; + } + + sep_pos = substr_end; + + if (trim) { + /* Trim leading whitespace */ + while (isspace(*substr_begin) && substr_begin < substr_end) { + substr_begin++; + substr_len--; + } + + /* Trim trailing whitespace */ + while (substr_end - 1 > substr_begin && isspace(*(substr_end-1))) { + substr_end--; + substr_len--; + } + } + + /* Copy the substring to the output list of strings */ + if (skip_empty == false || substr_len > 0) { + list = talloc_realloc(tmp_ctx, list, char*, num_strings + 2); + if (list == NULL) { + ret = ENOMEM; + goto done; + } + + /* empty string is stored for substr_len == 0 */ + list[num_strings] = talloc_strndup(list, substr_begin, substr_len); + if (list[num_strings] == NULL) { + ret = ENOMEM; + goto done; + } + num_strings++; + } + + } while (*sep_pos != '\0'); + + if (list == NULL) { + /* No allocations were done, make space for the NULL */ + list = talloc(tmp_ctx, char *); + if (list == NULL) { + ret = ENOMEM; + goto done; + } + } + list[num_strings] = NULL; + + if (size) { + *size = num_strings; + } + + *_list = talloc_steal(mem_ctx, list); + ret = EOK; +done: + talloc_free(tmp_ctx); + return ret; +} -- 2.9.3