auth:tests: Only enable torture_gnutls_aes_128_cfb() on GnuTLS >= 3.6.11
[Samba.git] / lib / util / strv.c
blob83d84d92528bdef7a30855bf304df517fd63450c
1 /*
2 * String Vector functions modeled after glibc argv_* functions
4 * Copyright Volker Lendecke <vl@samba.org> 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "strv.h"
22 #include "talloc.h"
23 #include <string.h>
25 static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
26 size_t srclen)
28 size_t dstlen = talloc_array_length(*dst);
29 size_t newlen = dstlen + srclen;
30 char *new_dst;
32 if ((newlen < srclen) || (newlen < dstlen)) {
33 return ERANGE;
36 new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
37 if (new_dst == NULL) {
38 return ENOMEM;
40 memcpy(&new_dst[dstlen], src, srclen);
42 *dst = new_dst;
43 return 0;
46 int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
48 return _strv_append(mem_ctx, strv, string, strlen(string)+1);
51 int strv_addn(TALLOC_CTX *mem_ctx, char **strv, const char *string, size_t n)
53 char t[n+1];
55 memcpy(t, string, n);
56 t[n] = '\0';
57 return _strv_append(mem_ctx, strv, t, n+1);
60 int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
62 return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
65 static bool strv_valid_entry(const char *strv, size_t strv_len,
66 const char *entry, size_t *entry_len)
68 if (strv_len == 0) {
69 return false;
71 if (strv[strv_len-1] != '\0') {
72 return false;
75 if (entry < strv) {
76 return false;
78 if (entry >= (strv+strv_len)) {
79 return false;
82 if (entry_len != NULL) {
83 *entry_len = strlen(entry);
86 return true;
89 const char *strv_len_next(const char *strv, size_t strv_len,
90 const char *entry)
92 size_t entry_len;
94 if (entry == NULL) {
95 if (strv_valid_entry(strv, strv_len, strv, NULL)) {
96 return strv;
98 return NULL;
101 if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) {
102 return NULL;
105 entry += entry_len+1;
107 if (entry >= (strv + strv_len)) {
108 return NULL;
110 return entry;
113 char *strv_next(char *strv, const char *entry)
115 size_t len = talloc_array_length(strv);
116 const char *result;
118 result = strv_len_next(strv, len, entry);
119 return discard_const_p(char, result);
122 size_t strv_count(char *strv)
124 char *entry;
125 size_t count = 0;
127 for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
128 count += 1;
131 return count;
134 char *strv_find(char *strv, const char *entry)
136 char *e = NULL;
138 while ((e = strv_next(strv, e)) != NULL) {
139 if (strcmp(e, entry) == 0) {
140 return e;
144 return NULL;
147 void strv_delete(char **strv, char *entry)
149 size_t len = talloc_array_length(*strv);
150 size_t entry_len;
152 if (entry == NULL) {
153 return;
156 if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
157 return;
159 entry_len += 1;
161 memmove(entry, entry+entry_len,
162 len - entry_len - (entry - *strv));
164 *strv = talloc_realloc(NULL, *strv, char, len - entry_len);
167 char * const *strv_to_env(TALLOC_CTX *mem_ctx, char *strv)
169 char **data;
170 char *next = NULL;
171 size_t i;
172 size_t count = strv_count(strv);
174 if (strv == NULL) {
175 return NULL;
178 data = talloc_array(mem_ctx, char *, count + 1);
180 if (data == NULL) {
181 return NULL;
184 for(i = 0; i < count; i++) {
185 next = strv_next(strv, next);
186 data[i] = next;
188 data[count] = NULL;
190 return data;