dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0002-split_on_separator-move-to-a-separate-file.patch

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