env_merge.c (805B)
1 /* ISC license. */ 2 3 #include <string.h> 4 #include <errno.h> 5 6 #include <skalibs/bytestr.h> 7 #include <skalibs/env.h> 8 9 size_t env_merge (char const **v, size_t vmax, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen) 10 { 11 size_t vlen = envlen ; 12 size_t i = 0 ; 13 if (envlen >= vmax) return 0 ; 14 for (; i < envlen ; i++) v[i] = envp[i] ; 15 for (i = 0 ; i < modiflen ; i += strlen(modifs + i) + 1) 16 { 17 size_t split = str_chr(modifs + i, '=') ; 18 size_t j = 0 ; 19 for (; j < vlen ; j++) 20 if (!strncmp(modifs + i, v[j], split) && (v[j][split] == '=')) break ; 21 if (j < vlen) v[j] = v[--vlen] ; 22 if (modifs[i + split]) 23 { 24 if (vlen >= vmax) return 0 ; 25 v[vlen++] = modifs + i ; 26 } 27 } 28 if (vlen >= vmax) return 0 ; 29 v[vlen++] = 0 ; 30 return vlen ; 31 }