summaryrefslogtreecommitdiff
path: root/src/libstddjb/env_merge.c
blob: 2a0e5bdae98e3b02641d9286a44e3cbb3df5ff08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 ;
}