basic/env-util: add variant of strv_env_replace() that does strdup internally

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-02-15 20:43:41 +01:00
parent c39e36203f
commit aaf057c4bb
6 changed files with 35 additions and 37 deletions
+19 -3
View File
@@ -370,9 +370,8 @@ int strv_env_replace(char ***l, char *p) {
assert(p);
/* Replace first occurrence of the env var or add a new one in the string list. Drop other occurrences. Edits
* in-place. Does not copy p. p must be a valid key=value assignment.
*/
/* Replace first occurrence of the env var or add a new one in the string list. Drop other
* occurrences. Edits in-place. Does not copy p. p must be a valid key=value assignment. */
t = strchr(p, '=');
if (!t)
@@ -395,6 +394,23 @@ int strv_env_replace(char ***l, char *p) {
return 1;
}
int strv_env_replace_strdup(char ***l, const char *assignment) {
int r;
/* Like strv_env_replace(), but copies the argument. */
_cleanup_free_ char *p = strdup(assignment);
if (!p)
return -ENOMEM;
r = strv_env_replace(l, p);
if (r < 0)
return r;
TAKE_PTR(p);
return r;
}
char **strv_env_set(char **x, const char *p) {
_cleanup_strv_free_ char **ret = NULL;
size_t n, m;