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_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
|
---|
52 | {
|
---|
53 | return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
|
---|
54 | }
|
---|
55 |
|
---|
56 | static bool strv_valid_entry(const char *strv, const char *entry,
|
---|
57 | size_t *strv_len, size_t *entry_len)
|
---|
58 | {
|
---|
59 | size_t len;
|
---|
60 |
|
---|
61 | len = talloc_array_length(strv);
|
---|
62 | if (len == 0) {
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 | if (strv[len-1] != '\0') {
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (entry < strv) {
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | if (entry >= (strv+len)) {
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | *strv_len = len;
|
---|
77 | *entry_len = strlen(entry);
|
---|
78 |
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | char *strv_next(char *strv, const char *entry)
|
---|
83 | {
|
---|
84 | size_t len, entry_len;
|
---|
85 | char *result;
|
---|
86 |
|
---|
87 | if (entry == NULL) {
|
---|
88 | if (strv_valid_entry(strv, strv, &len, &entry_len)) {
|
---|
89 | return strv;
|
---|
90 | }
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!strv_valid_entry(strv, entry, &len, &entry_len)) {
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 | result = &strv[entry - strv]; /* avoid const problems with this stmt */
|
---|
98 | result += entry_len + 1;
|
---|
99 |
|
---|
100 | if (result >= (strv + len)) {
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | size_t strv_count(char *strv)
|
---|
107 | {
|
---|
108 | char *entry;
|
---|
109 | size_t count = 0;
|
---|
110 |
|
---|
111 | for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
|
---|
112 | count += 1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return count;
|
---|
116 | }
|
---|
117 |
|
---|
118 | char *strv_find(char *strv, const char *entry)
|
---|
119 | {
|
---|
120 | char *e = NULL;
|
---|
121 |
|
---|
122 | while ((e = strv_next(strv, e)) != NULL) {
|
---|
123 | if (strcmp(e, entry) == 0) {
|
---|
124 | return e;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | return NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void strv_delete(char **strv, char *entry)
|
---|
132 | {
|
---|
133 | size_t len, entry_len;
|
---|
134 |
|
---|
135 | if (entry == NULL) {
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (!strv_valid_entry(*strv, entry, &len, &entry_len)) {
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | entry_len += 1;
|
---|
143 |
|
---|
144 | memmove(entry, entry+entry_len,
|
---|
145 | len - entry_len - (entry - *strv));
|
---|
146 |
|
---|
147 | *strv = talloc_realloc(NULL, *strv, char, len - entry_len);
|
---|
148 | }
|
---|