diff options
author | Ray Strode <rstrode@redhat.com> | 2016-08-09 10:20:22 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-02-20 23:32:53 -0500 |
commit | 4bed076c5f79ce26451ea3d73950d895f630f9a7 (patch) | |
tree | 2dfddadab7401f9ac55bc73cbe52d6fd5349677a /src | |
parent | 51e76f7cd137bd57c035f96f447c08991226999d (diff) |
basic: add replace_env_n function
It's like replace_env, but lets you pass in a substring.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/env-util.c | 6 | ||||
-rw-r--r-- | src/basic/env-util.h | 6 |
2 files changed, 9 insertions, 3 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c index f370854673..1b955ff1d5 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -519,7 +519,7 @@ char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const cha return e; } -char *replace_env(const char *format, char **env, unsigned flags) { +char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) { enum { WORD, CURLY, @@ -530,10 +530,11 @@ char *replace_env(const char *format, char **env, unsigned flags) { const char *e, *word = format; char *k; _cleanup_free_ char *r = NULL; + size_t i; assert(format); - for (e = format; *e; e ++) { + for (e = format, i = 0; *e && i < n; e ++, i ++) { switch (state) { @@ -614,6 +615,7 @@ char *replace_env(const char *format, char **env, unsigned flags) { r = k; word = e--; + i--; state = WORD; } break; diff --git a/src/basic/env-util.h b/src/basic/env-util.h index 03bbc6af00..43a1371f5e 100644 --- a/src/basic/env-util.h +++ b/src/basic/env-util.h @@ -34,9 +34,13 @@ enum { REPLACE_ENV_ALLOW_BRACELESS = 2u, }; -char *replace_env(const char *format, char **env, unsigned flags); +char *replace_env_n(const char *format, size_t n, char **env, unsigned flags); char **replace_env_argv(char **argv, char **env); +static inline char *replace_env(const char *format, char **env, unsigned flags) { + return replace_env_n(format, strlen(format), env, flags); +} + bool strv_env_is_valid(char **e); #define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL) char **strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata); |