summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSumit Bose <sbose@redhat.com>2017-02-08 14:28:28 +0100
committerSumit Bose <sbose@redhat.com>2017-02-13 17:09:06 +0100
commita900fb16256367af2daceec381e1908ecf17c15e (patch)
treed4cde2a6651e58250b9b8820ee092a9b7bf43b9b /src
parenta5ecc93abb01cece628fdef04ebad43bba267419 (diff)
split_on_separator: move to a separate file
To be able to include split_on_separator() without additional dependencies (only talloc), it is moved into a separate file.
Diffstat (limited to 'src')
-rw-r--r--src/util/util.c93
-rw-r--r--src/util/util_ext.c121
2 files changed, 121 insertions, 93 deletions
diff --git a/src/util/util.c b/src/util/util.c
index 885f67e66..52a7868c0 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 000000000..fceb8c873
--- /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 <ssorce@redhat.com>
+
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include <talloc.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <ctype.h>
+
+#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;
+}