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
))
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 matching string in another string with the option to
113 * start looking at a certain point (i.e. ignore anything beyond that point).
115 static char *reverse_strstr(const char *searched
, const char *search_for
,
116 const char *searched_start
)
120 if (*search_for
== '\0')
121 return (char *)searched
;
124 char *match
= strstr(searched
, search_for
);
127 * Stop looking if no new match is found or looking past the
128 * searched_start pointer
130 if (match
== NULL
|| (searched_start
!= NULL
&&
131 match
+ strlen(search_for
) > searched_start
))
135 searched
= match
+ 1;
142 * Retrieve the attributes string associated with a single name in the list
143 * There is no protection on attributes being too small for the value
145 int env_attr_lookup(const char *attr_list
, const char *name
, char *attributes
)
147 const char *entry
= NULL
;
156 entry
= reverse_strstr(attr_list
, name
, NULL
);
157 while (entry
!= NULL
) {
158 const char *prevch
= entry
- 1;
159 const char *nextch
= entry
+ strlen(name
);
162 while (*prevch
== ' ')
164 while (*nextch
== ' ')
167 /* check for an exact match */
168 if ((entry
== attr_list
||
169 *prevch
== ENV_ATTR_LIST_DELIM
) &&
170 (*nextch
== ENV_ATTR_SEP
||
171 *nextch
== ENV_ATTR_LIST_DELIM
||
175 entry
= reverse_strstr(attr_list
, name
, entry
);
181 entry
+= strlen(name
);
183 while (*entry
== ' ')
185 if (*entry
!= ENV_ATTR_SEP
)
189 static const char delims
[] = {
190 ENV_ATTR_LIST_DELIM
, ' ', '\0'};
192 /* skip the attr sep */
195 while (*entry
== ' ')
198 delim
= strpbrk(entry
, delims
);
203 memcpy(attributes
, entry
, len
);
205 attributes
[len
] = '\0';
211 /* not found in list */