3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 * SPDX-License-Identifier: GPL-2.0+
8 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
11 #include <linux/linux_string.h>
18 #include <linux/string.h>
22 * Iterate through the whole list calling the callback for each found element.
23 * "attr_list" takes the form:
24 * attributes = [^,:\s]*
25 * entry = name[:attributes]
28 int env_attr_walk(const char *attr_list,
29 int (*callback)(const char *name, const char *attributes, void *priv),
32 const char *entry, *entry_end;
33 char *name, *attributes;
41 char *entry_cpy = NULL;
43 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
44 /* check if this is the last entry in the list */
45 if (entry_end == NULL) {
46 int entry_len = strlen(entry);
50 * allocate memory to copy the entry into since
51 * we will need to inject '\0' chars and squash
52 * white-space before calling the callback
54 entry_cpy = malloc(entry_len + 1);
56 /* copy the rest of the list */
57 strcpy(entry_cpy, entry);
62 int entry_len = entry_end - entry;
66 * allocate memory to copy the entry into since
67 * we will need to inject '\0' chars and squash
68 * white-space before calling the callback
70 entry_cpy = malloc(entry_len + 1);
72 /* copy just this entry and null term */
73 strncpy(entry_cpy, entry, entry_len);
74 entry_cpy[entry_len] = '\0';
80 /* check if there is anything to process (e.g. not ",,,") */
81 if (entry_cpy != NULL) {
82 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
83 /* check if there is a ':' */
84 if (attributes != NULL) {
85 /* replace the ':' with '\0' to term name */
87 /* remove white-space from attributes */
88 attributes = strim(attributes);
90 /* remove white-space from name */
91 name = strim(entry_cpy);
93 /* only call the callback if there is a name */
94 if (strlen(name) != 0) {
97 retval = callback(name, attributes, priv);
106 entry = entry_end + 1;
107 } while (entry_end != NULL);
113 * Search for the last exactly matching name in an attribute list
115 static int reverse_name_search(const char *searched, const char *search_for,
119 const char *cur_searched = searched;
124 if (*search_for == '\0') {
127 return strlen(searched);
131 const char *match = strstr(cur_searched, search_for);
135 /* Stop looking if no new match is found */
140 nextch = match + strlen(search_for);
143 while (*prevch == ' ' && prevch >= searched)
145 while (*nextch == ' ')
148 /* Start looking past the current match so last is found */
149 cur_searched = match + 1;
150 /* Check for an exact match */
151 if (match != searched &&
152 *prevch != ENV_ATTR_LIST_DELIM &&
153 prevch != searched - 1)
155 if (*nextch != ENV_ATTR_SEP &&
156 *nextch != ENV_ATTR_LIST_DELIM &&
162 result_size = strlen(search_for);
169 * Retrieve the attributes string associated with a single name in the list
170 * There is no protection on attributes being too small for the value
172 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
174 const char *entry = NULL;
184 entry_len = reverse_name_search(attr_list, name, &entry);
191 while (*entry == ' ')
193 if (*entry != ENV_ATTR_SEP)
197 static const char delims[] = {
198 ENV_ATTR_LIST_DELIM, ' ', '\0'};
200 /* skip the attr sep */
203 while (*entry == ' ')
206 delim = strpbrk(entry, delims);
211 memcpy(attributes, entry, len);
213 attributes[len] = '\0';
219 /* not found in list */