3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (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., 59 Temple Place, Suite 330, Boston,
24 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
27 #include <linux/linux_string.h>
34 #include <linux/string.h>
38 * Iterate through the whole list calling the callback for each found element.
39 * "attr_list" takes the form:
40 * attributes = [^,:\s]*
41 * entry = name[:attributes]
44 int env_attr_walk(const char *attr_list,
45 int (*callback)(const char *name, const char *attributes))
47 const char *entry, *entry_end;
48 char *name, *attributes;
56 char *entry_cpy = NULL;
58 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
59 /* check if this is the last entry in the list */
60 if (entry_end == NULL) {
61 int entry_len = strlen(entry);
65 * allocate memory to copy the entry into since
66 * we will need to inject '\0' chars and squash
67 * white-space before calling the callback
69 entry_cpy = malloc(entry_len + 1);
71 /* copy the rest of the list */
72 strcpy(entry_cpy, entry);
77 int entry_len = entry_end - entry;
81 * allocate memory to copy the entry into since
82 * we will need to inject '\0' chars and squash
83 * white-space before calling the callback
85 entry_cpy = malloc(entry_len + 1);
87 /* copy just this entry and null term */
88 strncpy(entry_cpy, entry, entry_len);
89 entry_cpy[entry_len] = '\0';
95 /* check if there is anything to process (e.g. not ",,,") */
96 if (entry_cpy != NULL) {
97 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
98 /* check if there is a ':' */
99 if (attributes != NULL) {
100 /* replace the ':' with '\0' to term name */
101 *attributes++ = '\0';
102 /* remove white-space from attributes */
103 attributes = strim(attributes);
105 /* remove white-space from name */
106 name = strim(entry_cpy);
108 /* only call the callback if there is a name */
109 if (strlen(name) != 0) {
112 retval = callback(name, attributes);
121 entry = entry_end + 1;
122 } while (entry_end != NULL);
128 * Search for the last matching string in another string with the option to
129 * start looking at a certain point (i.e. ignore anything beyond that point).
131 static char *reverse_strstr(const char *searched, const char *search_for,
132 const char *searched_start)
136 if (*search_for == '\0')
137 return (char *)searched;
140 char *match = strstr(searched, search_for);
143 * Stop looking if no new match is found or looking past the
144 * searched_start pointer
146 if (match == NULL || (searched_start != NULL &&
147 match + strlen(search_for) > searched_start))
151 searched = match + 1;
158 * Retrieve the attributes string associated with a single name in the list
159 * There is no protection on attributes being too small for the value
161 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
163 const char *entry = NULL;
172 entry = reverse_strstr(attr_list, name, NULL);
173 while (entry != NULL) {
174 const char *prevch = entry - 1;
175 const char *nextch = entry + strlen(name);
178 while (*prevch == ' ')
180 while (*nextch == ' ')
183 /* check for an exact match */
184 if ((entry == attr_list ||
185 *prevch == ENV_ATTR_LIST_DELIM) &&
186 (*nextch == ENV_ATTR_SEP ||
187 *nextch == ENV_ATTR_LIST_DELIM ||
191 entry = reverse_strstr(attr_list, name, entry);
197 entry += strlen(name);
199 while (*entry == ' ')
201 if (*entry != ENV_ATTR_SEP)
205 static const char delims[] = {
206 ENV_ATTR_LIST_DELIM, ' ', '\0'};
208 /* skip the attr sep */
211 while (*entry == ' ')
214 delim = strpbrk(entry, delims);
219 memcpy(attributes, entry, len);
221 attributes[len] = '\0';
227 /* not found in list */