2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2004
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Martin Pool 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 List of Strings manipulation functions
29 #define S_LIST_ABS 16 /* List Allocation Block Size */
31 char **str_list_make(const char *string, const char *sep)
39 if (!string || !*string)
43 DEBUG(0,("str_list_make: Unable to allocate memory"));
46 if (!sep) sep = LIST_SEP;
52 while (next_token(&str, tok, sep, sizeof(tok))) {
55 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
57 DEBUG(0,("str_list_make: Unable to allocate memory"));
63 memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1)));
66 list[num] = strdup(tok);
68 DEBUG(0,("str_list_make: Unable to allocate memory"));
81 BOOL str_list_copy(char ***dest, const char **src)
96 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
98 DEBUG(0,("str_list_copy: Unable to re-allocate memory"));
103 memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1)));
106 list[num] = strdup(src[num]);
108 DEBUG(0,("str_list_copy: Unable to allocate memory"));
109 str_list_free(&list);
121 Return true if all the elements of the list match exactly.
123 BOOL str_list_compare(char **list1, char **list2)
127 if (!list1 || !list2)
128 return (list1 == list2);
130 for (num = 0; list1[num]; num++) {
133 if (!strcsequal(list1[num], list2[num]))
137 return False; /* if list2 has more elements than list1 fail */
142 void str_list_free(char ***list)
149 for(; *tlist; tlist++)
154 BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
157 ssize_t ls, lp, li, ld, i, d;
166 lp = (ssize_t)strlen(pattern);
167 li = (ssize_t)strlen(insert);
172 ls = (ssize_t)strlen(s);
174 while ((p = strstr(s, pattern))) {
178 t = (char *) malloc(ls +ld +1);
180 DEBUG(0,("str_list_substitute: Unable to allocate memory"));
184 memcpy(t +d +li, p +lp, ls -d -lp +1);
191 for (i = 0; i < li; i++) {
216 #define IPSTR_LIST_SEP ","
219 * Add ip string representation to ipstr list. Used also
220 * as part of @function ipstr_list_make
222 * @param ipstr_list pointer to string containing ip list;
223 * MUST BE already allocated and IS reallocated if necessary
224 * @param ipstr_size pointer to current size of ipstr_list (might be changed
225 * as a result of reallocation)
226 * @param ip IP address which is to be added to list
227 * @return pointer to string appended with new ip and possibly
228 * reallocated to new length
231 char* ipstr_list_add(char** ipstr_list, const struct ipv4_addr *ip)
233 char* new_ipstr = NULL;
235 /* arguments checking */
236 if (!ipstr_list || !ip) return NULL;
238 /* attempt to convert ip to a string and append colon separator to it */
240 asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,sys_inet_ntoa(*ip));
241 SAFE_FREE(*ipstr_list);
243 asprintf(&new_ipstr, "%s", sys_inet_ntoa(*ip));
245 *ipstr_list = new_ipstr;
250 * Allocate and initialise an ipstr list using ip adresses
251 * passed as arguments.
253 * @param ipstr_list pointer to string meant to be allocated and set
254 * @param ip_list array of ip addresses to place in the list
255 * @param ip_count number of addresses stored in ip_list
256 * @return pointer to allocated ip string
259 char* ipstr_list_make(char** ipstr_list, const struct ipv4_addr* ip_list, int ip_count)
263 /* arguments checking */
264 if (!ip_list && !ipstr_list) return 0;
268 /* process ip addresses given as arguments */
269 for (i = 0; i < ip_count; i++)
270 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
272 return (*ipstr_list);
277 * Parse given ip string list into array of ip addresses
278 * (as in_addr structures)
280 * @param ipstr ip string list to be parsed
281 * @param ip_list pointer to array of ip addresses which is
282 * allocated by this function and must be freed by caller
283 * @return number of succesfully parsed addresses
286 int ipstr_list_parse(const char* ipstr_list, struct ipv4_addr** ip_list)
291 if (!ipstr_list || !ip_list) return 0;
293 for (*ip_list = NULL, count = 0;
294 next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN);
297 struct ipv4_addr addr;
299 /* convert single token to ip address */
300 if ( (addr.addr = sys_inet_addr(token_str)) == INADDR_NONE )
303 /* prepare place for another in_addr structure */
304 *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct ipv4_addr));
305 if (!*ip_list) return -1;
307 (*ip_list)[count] = addr;
315 * Safely free ip string list
317 * @param ipstr_list ip string list to be freed
320 void ipstr_list_free(char* ipstr_list)
322 SAFE_FREE(ipstr_list);