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,
27 #include <linux/string.h>
31 * Iterate through the whole list calling the callback for each found element.
32 * "attr_list" takes the form:
33 * attributes = [^,:\s]*
34 * entry = name[:attributes]
37 int env_attr_walk(const char *attr_list,
38 int (*callback)(const char *name, const char *attributes))
40 const char *entry, *entry_end;
41 char *name, *attributes;
49 char *entry_cpy = NULL;
51 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
52 /* check if this is the last entry in the list */
53 if (entry_end == NULL) {
54 int entry_len = strlen(entry);
58 * allocate memory to copy the entry into since
59 * we will need to inject '\0' chars and squash
60 * white-space before calling the callback
62 entry_cpy = malloc(entry_len + 1);
64 /* copy the rest of the list */
65 strcpy(entry_cpy, entry);
70 int entry_len = entry_end - entry;
74 * allocate memory to copy the entry into since
75 * we will need to inject '\0' chars and squash
76 * white-space before calling the callback
78 entry_cpy = malloc(entry_len + 1);
80 /* copy just this entry and null term */
81 strncpy(entry_cpy, entry, entry_len);
82 entry_cpy[entry_len] = '\0';
88 /* check if there is anything to process (e.g. not ",,,") */
89 if (entry_cpy != NULL) {
90 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
91 /* check if there is a ':' */
92 if (attributes != NULL) {
93 /* replace the ':' with '\0' to term name */
95 /* remove white-space from attributes */
96 attributes = strim(attributes);
98 /* remove white-space from name */
99 name = strim(entry_cpy);
101 /* only call the callback if there is a name */
102 if (strlen(name) != 0) {
105 retval = callback(name, attributes);
114 entry = entry_end + 1;
115 } while (entry_end != NULL);
121 * Search for the last matching string in another string with the option to
122 * start looking at a certain point (i.e. ignore anything beyond that point).
124 static char *reverse_strstr(const char *searched, const char *search_for,
125 const char *searched_start)
129 if (*search_for == '\0')
130 return (char *)searched;
133 char *match = strstr(searched, search_for);
136 * Stop looking if no new match is found or looking past the
137 * searched_start pointer
139 if (match == NULL || (searched_start != NULL &&
140 match + strlen(search_for) > searched_start))
144 searched = match + 1;
151 * Retrieve the attributes string associated with a single name in the list
152 * There is no protection on attributes being too small for the value
154 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
156 const char *entry = NULL;
165 entry = reverse_strstr(attr_list, name, NULL);
166 while (entry != NULL) {
167 const char *prevch = entry - 1;
168 const char *nextch = entry + strlen(name);
171 while (*prevch == ' ')
173 while (*nextch == ' ')
176 /* check for an exact match */
177 if ((entry == attr_list ||
178 *prevch == ENV_ATTR_LIST_DELIM) &&
179 (*nextch == ENV_ATTR_SEP ||
180 *nextch == ENV_ATTR_LIST_DELIM ||
184 entry = reverse_strstr(attr_list, name, entry);
190 entry += strlen(name);
192 while (*entry == ' ')
194 if (*entry != ENV_ATTR_SEP)
198 static const char delims[] = {
199 ENV_ATTR_LIST_DELIM, ' ', '\0'};
201 /* skip the attr sep */
204 while (*entry == ' ')
207 delim = strpbrk(entry, delims);
212 memcpy(attributes, entry, len);
214 attributes[len] = '\0';
220 /* not found in list */