int fstab_filter_options(const char *opts, const char *names,
const char **ret_namefound, char **ret_value, char **ret_filtered) {
- const char *name, *n = NULL, *x;
+ const char *name, *namefound = NULL, *x;
_cleanup_strv_free_ char **stor = NULL;
_cleanup_free_ char *v = NULL, **strv = NULL;
+ int r;
assert(names && *names);
if (!opts)
goto answer;
- /* If !value and !filtered, this function is not allowed to fail. */
+ /* If !ret_value and !ret_filtered, this function is not allowed to fail. */
if (!ret_filtered) {
const char *word, *state;
if (!strneq(word, name, strlen(name)))
continue;
- /* we know that the string is NUL
- * terminated, so *x is valid */
+ /* We know that the string is NUL terminated, so *x is valid */
x = word + strlen(name);
if (IN_SET(*x, '\0', '=', ',')) {
- n = name;
+ namefound = name;
if (ret_value) {
- free(v);
- if (IN_SET(*x, '\0', ','))
- v = NULL;
- else {
- assert(*x == '=');
- x++;
- v = strndup(x, l - strlen(name) - 1);
- if (!v)
- return -ENOMEM;
- }
+ bool eq = *x == '=';
+ assert(eq || IN_SET(*x, ',', '\0'));
+
+ r = free_and_strndup(&v,
+ eq ? x + 1 : NULL,
+ eq ? l - strlen(name) - 1 : 0);
+ if (r < 0)
+ return r;
}
}
}
} else {
- char **t, **s;
-
stor = strv_split(opts, ",");
if (!stor)
return -ENOMEM;
if (!strv)
return -ENOMEM;
- for (s = t = strv; *s; s++) {
+ char **t = strv;
+ for (char **s = strv; *s; s++) {
NULSTR_FOREACH(name, names) {
x = startswith(*s, name);
if (x && IN_SET(*x, '\0', '='))
continue;
found:
/* Keep the last occurrence found */
- n = name;
+ namefound = name;
if (ret_value) {
- free(v);
- if (*x == '\0')
- v = NULL;
- else {
- assert(*x == '=');
- x++;
- v = strdup(x);
- if (!v)
- return -ENOMEM;
- }
+ assert(IN_SET(*x, '=', '\0'));
+ r = free_and_strdup(&v, *x == '=' ? x + 1 : NULL);
+ if (r < 0)
+ return r;
}
}
*t = NULL;
answer:
if (ret_namefound)
- *ret_namefound = n;
+ *ret_namefound = namefound;
if (ret_filtered) {
char *f;
if (ret_value)
*ret_value = TAKE_PTR(v);
- return !!n;
+ return !!namefound;
}
int fstab_extract_values(const char *opts, const char *name, char ***values) {