summaryrefslogtreecommitdiffstats
path: root/lib/util/strv.c
blob: 83d84d92528bdef7a30855bf304df517fd63450c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * String Vector functions modeled after glibc argv_* functions
 *
 * Copyright Volker Lendecke <vl@samba.org> 2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include "strv.h"
#include "talloc.h"
#include <string.h>

static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
			size_t srclen)
{
	size_t dstlen = talloc_array_length(*dst);
	size_t newlen = dstlen + srclen;
	char *new_dst;

	if ((newlen < srclen) || (newlen < dstlen)) {
		return ERANGE;
	}

	new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
	if (new_dst == NULL) {
		return ENOMEM;
	}
	memcpy(&new_dst[dstlen], src, srclen);

	*dst = new_dst;
	return 0;
}

int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
{
	return _strv_append(mem_ctx, strv, string, strlen(string)+1);
}

int strv_addn(TALLOC_CTX *mem_ctx, char **strv, const char *string, size_t n)
{
        char t[n+1];

        memcpy(t, string, n);
        t[n] = '\0';
        return _strv_append(mem_ctx, strv, t, n+1);
}

int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
{
	return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
}

static bool strv_valid_entry(const char *strv, size_t strv_len,
			     const char *entry, size_t *entry_len)
{
	if (strv_len == 0) {
		return false;
	}
	if (strv[strv_len-1] != '\0') {
		return false;
	}

	if (entry < strv) {
		return false;
	}
	if (entry >= (strv+strv_len)) {
		return false;
	}

	if (entry_len != NULL) {
		*entry_len = strlen(entry);
	}

	return true;
}

const char *strv_len_next(const char *strv, size_t strv_len,
			  const char *entry)
{
	size_t entry_len;

	if (entry == NULL) {
		if (strv_valid_entry(strv, strv_len, strv, NULL)) {
			return strv;
		}
		return NULL;
	}

	if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) {
		return NULL;
	}

	entry += entry_len+1;

	if (entry >= (strv + strv_len)) {
		return NULL;
	}
	return entry;
}

char *strv_next(char *strv, const char *entry)
{
	size_t len = talloc_array_length(strv);
	const char *result;

	result = strv_len_next(strv, len, entry);
	return discard_const_p(char, result);
}

size_t strv_count(char *strv)
{
	char *entry;
	size_t count = 0;

	for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
		count += 1;
	}

	return count;
}

char *strv_find(char *strv, const char *entry)
{
	char *e = NULL;

	while ((e = strv_next(strv, e)) != NULL) {
		if (strcmp(e, entry) == 0) {
			return e;
		}
	}

	return NULL;
}

void strv_delete(char **strv, char *entry)
{
	size_t len = talloc_array_length(*strv);
	size_t entry_len;

	if (entry == NULL) {
		return;
	}

	if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
		return;
	}
	entry_len += 1;

	memmove(entry, entry+entry_len,
		len - entry_len - (entry - *strv));

	*strv = talloc_realloc(NULL, *strv, char, len - entry_len);
}

char * const *strv_to_env(TALLOC_CTX *mem_ctx, char *strv)
{
       char **data;
       char *next = NULL;
       size_t i;
       size_t count = strv_count(strv);

       if (strv == NULL) {
               return NULL;
       }

       data = talloc_array(mem_ctx, char *, count + 1);

       if (data == NULL) {
               return NULL;
       }

       for(i = 0; i < count; i++) {
               next = strv_next(strv, next);
               data[i] = next;
       }
       data[count] = NULL;

       return data;
}