sssd  2.2.3
About: SSSD provides a set of daemons to manage access to remote directories and authentication mechanisms such as LDAP, Kerberos or FreeIPA. It provides also an NSS and PAM interface toward the system.
  Fossies Dox: sssd-2.2.3.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

util_ext.c
Go to the documentation of this file.
1 /*
2  SSSD helper calls - can be used by libraries for external use as well
3 
4  Authors:
5  Simo Sorce <ssorce@redhat.com>
6 
7  Copyright (C) 2017 Red Hat
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include <talloc.h>
24 #include <stdbool.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <strings.h>
29 
30 #define EOK 0
31 
32 int split_on_separator(TALLOC_CTX *mem_ctx, const char *str,
33  const char sep, bool trim, bool skip_empty,
34  char ***_list, int *size)
35 {
36  int ret;
37  const char *substr_end = str;
38  const char *substr_begin = str;
39  const char *sep_pos = NULL;
40  size_t substr_len;
41  char **list = NULL;
42  int num_strings = 0;
43  TALLOC_CTX *tmp_ctx = NULL;
44 
45  if (str == NULL || *str == '\0' || _list == NULL) {
46  return EINVAL;
47  }
48 
49  tmp_ctx = talloc_new(NULL);
50  if (tmp_ctx == NULL) {
51  return ENOMEM;
52  }
53 
54  do {
55  substr_len = 0;
56 
57  /* If this is not the first substring, then move from the separator. */
58  if (sep_pos != NULL) {
59  substr_end = sep_pos + 1;
60  substr_begin = sep_pos + 1;
61  }
62 
63  /* Find end of the first substring */
64  while (*substr_end != sep && *substr_end != '\0') {
65  substr_end++;
66  substr_len++;
67  }
68 
69  sep_pos = substr_end;
70 
71  if (trim) {
72  /* Trim leading whitespace */
73  while (isspace(*substr_begin) && substr_begin < substr_end) {
74  substr_begin++;
75  substr_len--;
76  }
77 
78  /* Trim trailing whitespace */
79  while (substr_end - 1 > substr_begin && isspace(*(substr_end-1))) {
80  substr_end--;
81  substr_len--;
82  }
83  }
84 
85  /* Copy the substring to the output list of strings */
86  if (skip_empty == false || substr_len > 0) {
87  list = talloc_realloc(tmp_ctx, list, char*, num_strings + 2);
88  if (list == NULL) {
89  ret = ENOMEM;
90  goto done;
91  }
92 
93  /* empty string is stored for substr_len == 0 */
94  list[num_strings] = talloc_strndup(list, substr_begin, substr_len);
95  if (list[num_strings] == NULL) {
96  ret = ENOMEM;
97  goto done;
98  }
99  num_strings++;
100  }
101 
102  } while (*sep_pos != '\0');
103 
104  if (list == NULL) {
105  /* No allocations were done, make space for the NULL */
106  list = talloc(tmp_ctx, char *);
107  if (list == NULL) {
108  ret = ENOMEM;
109  goto done;
110  }
111  }
112  list[num_strings] = NULL;
113 
114  if (size) {
115  *size = num_strings;
116  }
117 
118  *_list = talloc_steal(mem_ctx, list);
119  ret = EOK;
120 done:
121  talloc_free(tmp_ctx);
122  return ret;
123 }
124 
125 bool string_in_list(const char *string, char **list, bool case_sensitive)
126 {
127  size_t c;
128  int(*compare)(const char *s1, const char *s2);
129 
130  if (string == NULL || list == NULL || *list == NULL) {
131  return false;
132  }
133 
134  compare = case_sensitive ? strcmp : strcasecmp;
135 
136  for (c = 0; list[c] != NULL; c++) {
137  if (compare(string, list[c]) == 0) {
138  return true;
139  }
140  }
141 
142  return false;
143 }
EOK
#define EOK
Definition: util_ext.c:30
split_on_separator
int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, const char sep, bool trim, bool skip_empty, char ***_list, int *size)
Definition: util_ext.c:32
NULL
#define NULL
Definition: util.h:67
ret
errno_t ret
Definition: sbus_errors.c:31
string_in_list
bool string_in_list(const char *string, char **list, bool case_sensitive)
Definition: util_ext.c:125