summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorSumit Bose <sbose@redhat.com>2012-11-13 21:20:32 +0100
committerJakub Hrozek <jhrozek@redhat.com>2012-11-14 10:42:34 +0100
commit032d0980dfe5a27a5954f44f9d519e03fc7d1ced (patch)
treeb78b6ea906b1833acc64153a6e6d4d6c3b24cfb8 /src/util
parent5fe7390f295321f6a13afddf2023aae45a32aad2 (diff)
downloadsssd-032d0980dfe5a27a5954f44f9d519e03fc7d1ced.tar.gz
sssd-032d0980dfe5a27a5954f44f9d519e03fc7d1ced.tar.xz
sssd-032d0980dfe5a27a5954f44f9d519e03fc7d1ced.zip
Add string_in_list() and add_string_to_list() with tests
string_in_list() and add_string_to_list() are two utilities for NULL terminated strings arrays. add_string_to_list() adds a new string to an existing list or creates a new one with the strings as only item if there is not list. string_in_list() checks if a given string is in the list. It can be used case sensitive or in-sensitive.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.c63
-rw-r--r--src/util/util.h6
2 files changed, 69 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c
index b812ef1b1..18df0e847 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -634,3 +634,66 @@ remove_ipv6_brackets(char *ipv6addr)
return EOK;
}
+
+errno_t add_string_to_list(TALLOC_CTX *mem_ctx, const char *string,
+ char ***list_p)
+{
+ size_t c;
+ char **old_list = NULL;
+ char **new_list = NULL;
+
+ if (string == NULL || list_p == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE, ("Missing string or list.\n"));
+ return EINVAL;
+ }
+
+ old_list = *list_p;
+
+ if (old_list == NULL) {
+ /* If the input is a NULL list a new one is created with the new
+ * string and the terminating NULL element. */
+ c = 0;
+ new_list = talloc_array(mem_ctx, char *, 2);
+ } else {
+ for (c = 0; old_list[c] != NULL; c++);
+ new_list = talloc_realloc(mem_ctx, old_list, char *, c + 1);
+ }
+
+ if (new_list == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE, ("talloc_array/talloc_realloc failed.\n"));
+ return ENOMEM;
+ }
+
+ new_list[c] = talloc_strdup(new_list, string);
+ if (new_list[c] == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE, ("talloc_strdup failed.\n"));
+ talloc_free(new_list);
+ return ENOMEM;
+ }
+
+ new_list[c + 1] = NULL;
+
+ *list_p = new_list;
+
+ return EOK;
+}
+
+bool string_in_list(const char *string, char **list, bool case_sensitive)
+{
+ size_t c;
+ int(*compare)(const char *s1, const char *s2);
+
+ if (string == NULL || list == NULL || *list == NULL) {
+ return false;
+ }
+
+ compare = case_sensitive ? strcmp : strcasecmp;
+
+ for (c = 0; list[c] != NULL; c++) {
+ if (compare(string, list[c]) == 0) {
+ return true;
+ }
+ }
+
+ return false;
+}
diff --git a/src/util/util.h b/src/util/util.h
index b6ecfc2c9..56e7e0bed 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -535,6 +535,12 @@ sss_escape_ip_address(TALLOC_CTX *mem_ctx, int family, const char *addr);
errno_t
remove_ipv6_brackets(char *ipv6addr);
+
+errno_t add_string_to_list(TALLOC_CTX *mem_ctx, const char *string,
+ char ***list_p);
+
+bool string_in_list(const char *string, char **list, bool case_sensitive);
+
/* from sss_tc_utf8.c */
char *
sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s);