summaryrefslogtreecommitdiff
path: root/src/libenvexec/env_merge.c
blob: b6281eec722e183d11ea08efa42107718df02103 (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
26
27
28
29
30
31
/* ISC license. */

#include <string.h>
#include <errno.h>

#include <skalibs/bytestr.h>
#include <skalibs/env.h>

size_t env_merge (char const **v, size_t vmax, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen)
{
  size_t vlen = envlen ;
  size_t i = 0 ;
  if (envlen >= vmax) return 0 ;
  for (; i < envlen ; i++) v[i] = envp[i] ;
  for (i = 0 ; i < modiflen ; i += strlen(modifs + i) + 1)
  {
    size_t split = str_chr(modifs + i, '=') ;
    size_t j = 0 ;
    for (; j < vlen ; j++)
      if (!strncmp(modifs + i, v[j], split) && (v[j][split] == '=')) break ;
    if (j < vlen) v[j] = v[--vlen] ;
    if (modifs[i + split])
    {
      if (vlen >= vmax) return 0 ;
      v[vlen++] = modifs + i ;
    }
  }
  if (vlen >= vmax) return 0 ;
  v[vlen++] = 0 ;
  return vlen ;
}