diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/env_merge.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/env_merge.c')
-rw-r--r-- | src/libstddjb/env_merge.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstddjb/env_merge.c b/src/libstddjb/env_merge.c new file mode 100644 index 0000000..2a0e5bd --- /dev/null +++ b/src/libstddjb/env_merge.c @@ -0,0 +1,25 @@ +/* ISC license. */ + +#include <errno.h> +#include <skalibs/bytestr.h> +#include <skalibs/env.h> + +unsigned int env_merge (char const **v, unsigned int vmax, char const *const *envp, unsigned int envlen, char const *modifs, unsigned int modiflen) +{ + unsigned int n = byte_count(modifs, modiflen, '\0') ; + unsigned int vlen = envlen ; + register unsigned int i = 0 ; + if (envlen + n + 1 > vmax) return (errno = ENAMETOOLONG, 0) ; + for (; i < envlen ; i++) v[i] = envp[i] ; + for (i = 0 ; i < modiflen ; i += str_len(modifs + i) + 1) + { + unsigned int split = str_chr(modifs + i, '=') ; + register unsigned int j = 0 ; + for (; j < vlen ; j++) + if (!byte_diff(modifs + i, split, v[j]) && (v[j][split] == '=')) break ; + if (j < vlen) v[j] = v[--vlen] ; + if (modifs[i + split]) v[vlen++] = modifs + i ; + } + v[vlen++] = 0 ; + return vlen ; +} |