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))
31 const char *entry, *entry_end;
32 char *name, *attributes;
40 char *entry_cpy = NULL;
42 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
43 /* check if this is the last entry in the list */
44 if (entry_end == NULL) {
45 int entry_len = strlen(entry);
49 * allocate memory to copy the entry into since
50 * we will need to inject '\0' chars and squash
51 * white-space before calling the callback
53 entry_cpy = malloc(entry_len + 1);
55 /* copy the rest of the list */
56 strcpy(entry_cpy, entry);
61 int entry_len = entry_end - 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 just this entry and null term */
72 strncpy(entry_cpy, entry, entry_len);
73 entry_cpy[entry_len] = '\0';
79 /* check if there is anything to process (e.g. not ",,,") */
80 if (entry_cpy != NULL) {
81 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
82 /* check if there is a ':' */
83 if (attributes != NULL) {
84 /* replace the ':' with '\0' to term name */
86 /* remove white-space from attributes */
87 attributes = strim(attributes);
89 /* remove white-space from name */
90 name = strim(entry_cpy);
92 /* only call the callback if there is a name */
93 if (strlen(name) != 0) {
96 retval = callback(name, attributes);
105 entry = entry_end + 1;
106 } while (entry_end != NULL);
112 * Search for the last exactly matching name in an attribute list
114 static int reverse_name_search(const char *searched, const char *search_for,
118 const char *cur_searched = searched;
123 if (*search_for == '\0') {
126 return strlen(searched);
130 const char *match = strstr(cur_searched, search_for);
134 /* Stop looking if no new match is found */
139 nextch = match + strlen(search_for);
142 while (*prevch == ' ' && prevch >= searched)
144 while (*nextch == ' ')
147 /* Start looking past the current match so last is found */
148 cur_searched = match + 1;
149 /* Check for an exact match */
150 if (match != searched &&
151 *prevch != ENV_ATTR_LIST_DELIM &&
152 prevch != searched - 1)
154 if (*nextch != ENV_ATTR_SEP &&
155 *nextch != ENV_ATTR_LIST_DELIM &&
161 result_size = strlen(search_for);
168 * Retrieve the attributes string associated with a single name in the list
169 * There is no protection on attributes being too small for the value
171 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
173 const char *entry = NULL;
183 entry_len = reverse_name_search(attr_list, name, &entry);
190 while (*entry == ' ')
192 if (*entry != ENV_ATTR_SEP)
196 static const char delims[] = {
197 ENV_ATTR_LIST_DELIM, ' ', '\0'};
199 /* skip the attr sep */
202 while (*entry == ' ')
205 delim = strpbrk(entry, delims);
210 memcpy(attributes, entry, len);
212 attributes[len] = '\0';
218 /* not found in list */