864a3e5cf8ba88636daedf6ddef55c0d102d902e
[sfrench/samba-autobuild/.git] / lib / util / strv.c
1 /*
2  * String Vector functions modeled after glibc argv_* functions
3  *
4  * Copyright Volker Lendecke <vl@samba.org> 2014
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "replace.h"
21 #include "strv.h"
22 #include "talloc.h"
23 #include <string.h>
24
25 static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
26                         size_t srclen)
27 {
28         size_t dstlen = talloc_array_length(*dst);
29         size_t newlen = dstlen + srclen;
30         char *new_dst;
31
32         if ((newlen < srclen) || (newlen < dstlen)) {
33                 return ERANGE;
34         }
35
36         new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
37         if (new_dst == NULL) {
38                 return ENOMEM;
39         }
40         memcpy(&new_dst[dstlen], src, srclen);
41
42         *dst = new_dst;
43         return 0;
44 }
45
46 int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
47 {
48         return _strv_append(mem_ctx, strv, string, strlen(string)+1);
49 }
50
51 int strv_addn(TALLOC_CTX *mem_ctx, char **strv, const char *string, size_t n)
52 {
53         char t[n+1];
54
55         memcpy(t, string, n);
56         t[n] = '\0';
57         return _strv_append(mem_ctx, strv, t, n+1);
58 }
59
60 int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
61 {
62         return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
63 }
64
65 static bool strv_valid_entry(const char *strv, size_t strv_len,
66                              const char *entry, size_t *entry_len)
67 {
68         if (strv_len == 0) {
69                 return false;
70         }
71         if (strv[strv_len-1] != '\0') {
72                 return false;
73         }
74
75         if (entry < strv) {
76                 return false;
77         }
78         if (entry >= (strv+strv_len)) {
79                 return false;
80         }
81
82         *entry_len = strlen(entry);
83
84         return true;
85 }
86
87 char *strv_next(char *strv, const char *entry)
88 {
89         size_t len = talloc_array_length(strv);
90         size_t entry_len;
91         char *result;
92
93         if (entry == NULL) {
94                 if (strv_valid_entry(strv, len, strv, &entry_len)) {
95                         return strv;
96                 }
97                 return NULL;
98         }
99
100         if (!strv_valid_entry(strv, len, entry, &entry_len)) {
101                 return NULL;
102         }
103         result = &strv[entry - strv]; /* avoid const problems with this stmt */
104         result += entry_len + 1;
105
106         if (result >= (strv + len)) {
107                 return NULL;
108         }
109         return result;
110 }
111
112 size_t strv_count(char *strv)
113 {
114         char *entry;
115         size_t count = 0;
116
117         for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
118                 count += 1;
119         }
120
121         return count;
122 }
123
124 char *strv_find(char *strv, const char *entry)
125 {
126         char *e = NULL;
127
128         while ((e = strv_next(strv, e)) != NULL) {
129                 if (strcmp(e, entry) == 0) {
130                         return e;
131                 }
132         }
133
134         return NULL;
135 }
136
137 void strv_delete(char **strv, char *entry)
138 {
139         size_t len = talloc_array_length(*strv);
140         size_t entry_len;
141
142         if (entry == NULL) {
143                 return;
144         }
145
146         if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
147                 return;
148         }
149         entry_len += 1;
150
151         memmove(entry, entry+entry_len,
152                 len - entry_len - (entry - *strv));
153
154         *strv = talloc_realloc(NULL, *strv, char, len - entry_len);
155 }
156
157 char * const *strv_to_env(TALLOC_CTX *mem_ctx, char *strv)
158 {
159        char **data;
160        char *next = NULL;
161        size_t i;
162        size_t count = strv_count(strv);
163
164        if (strv == NULL) {
165                return NULL;
166        }
167
168        data = talloc_array(mem_ctx, char *, count + 1);
169
170        if (data == NULL) {
171                return NULL;
172        }
173
174        for(i = 0; i < count; i++) {
175                next = strv_next(strv, next);
176                data[i] = next;
177        }
178        data[count] = NULL;
179
180        return data;
181 }