From 8c1ab65c0f548d20b7f177bdb736daaf603340e1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 15:54:38 +0200 Subject: Adding upstream version 0.0~git20221206.8b7148f. Signed-off-by: Daniel Baumann --- libc-top-half/musl/src/env/setenv.c | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 libc-top-half/musl/src/env/setenv.c (limited to 'libc-top-half/musl/src/env/setenv.c') 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 +#include +#include + +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); +} -- cgit v1.2.3