diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
commit | 8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch) | |
tree | df55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/env/setenv.c | |
parent | Initial commit. (diff) | |
download | wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.tar.xz wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.zip |
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | libc-top-half/musl/src/env/setenv.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/env/setenv.c b/libc-top-half/musl/src/env/setenv.c new file mode 100644 index 0000000..c5226b6 --- /dev/null +++ b/libc-top-half/musl/src/env/setenv.c @@ -0,0 +1,42 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +void __env_rm_add(char *old, char *new) +{ + static char **env_alloced; + static size_t env_alloced_n; + for (size_t i=0; i < env_alloced_n; i++) + if (env_alloced[i] == old) { + env_alloced[i] = new; + free(old); + return; + } else if (!env_alloced[i] && new) { + env_alloced[i] = new; + new = 0; + } + if (!new) return; + char **t = realloc(env_alloced, sizeof *t * (env_alloced_n+1)); + if (!t) return; + (env_alloced = t)[env_alloced_n++] = new; +} + +int setenv(const char *var, const char *value, int overwrite) +{ + char *s; + size_t l1, l2; + + if (!var || !(l1 = __strchrnul(var, '=') - var) || var[l1]) { + errno = EINVAL; + return -1; + } + if (!overwrite && getenv(var)) return 0; + + l2 = strlen(value); + s = malloc(l1+l2+2); + if (!s) return -1; + memcpy(s, var, l1); + s[l1] = '='; + memcpy(s+l1+1, value, l2+1); + return __putenv(s, l1, s); +} |