aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/argz/envz_merge.c
blob: 3be7c1fbcab7daae185b8d2227594d933d64e53f (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright (C) 2002 by  Red Hat, Incorporated. All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * is freely granted, provided that this notice is preserved.
 */

#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <argz.h>
#include <envz.h>

error_t
envz_merge (char **envz,
       size_t *envz_len,
       const char *envz2,
       size_t envz2_len,
       int override)
{
  char *entry = NULL;
  char *name_str = NULL;
  char *val_str = NULL;
  char *name_iter = NULL;
  int retval = 0;

  while((entry = argz_next((char *)envz2, envz2_len, entry)) && !retval)
    {
      if (!override)
        {
          name_str = strdup (entry);
          name_iter = strchr(name_str, '=');
          if(name_iter)
            *name_iter = '\0';

          if(!envz_entry(*envz, *envz_len, name_str))
            {
              retval = argz_add(envz, envz_len, entry);
            }
          free(name_str);
        }
      else
        {
          name_str = strdup (entry);
          name_iter = strchr(name_str, '=');
          if(name_iter)
            {
              *name_iter = '\0';
              val_str = name_iter + 1;
            }
          else
            {
              val_str = NULL;
            }

          retval = envz_add(envz, envz_len, name_str, val_str);
          free(name_str);
        }
    }
  return retval;
}