1 /* Copyright (C) 2002 by  Red Hat, Incorporated. All rights reserved.
2  *
3  * Permission to use, copy, modify, and distribute this software
4  * is freely granted, provided that this notice is preserved.
5  */
6 
7 #define _DEFAULT_SOURCE
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <argz.h>
13 #include <envz.h>
14 
15 error_t
envz_merge(char ** envz,size_t * envz_len,const char * envz2,size_t envz2_len,int override)16 envz_merge (char **envz,
17        size_t *envz_len,
18        const char *envz2,
19        size_t envz2_len,
20        int override)
21 {
22   char *entry = NULL;
23   char *name_str = NULL;
24   char *val_str = NULL;
25   char *name_iter = NULL;
26   int retval = 0;
27 
28   while((entry = argz_next((char *)envz2, envz2_len, entry)) && !retval)
29     {
30       if (!override)
31         {
32           name_str = strdup (entry);
33           name_iter = strchr(name_str, '=');
34           if(name_iter)
35             *name_iter = '\0';
36 
37           if(!envz_entry(*envz, *envz_len, name_str))
38             {
39               retval = argz_add(envz, envz_len, entry);
40             }
41           free(name_str);
42         }
43       else
44         {
45           name_str = strdup (entry);
46           name_iter = strchr(name_str, '=');
47           if(name_iter)
48             {
49               *name_iter = '\0';
50               val_str = name_iter + 1;
51             }
52           else
53             {
54               val_str = NULL;
55             }
56 
57           retval = envz_add(envz, envz_len, name_str, val_str);
58           free(name_str);
59         }
60     }
61   return retval;
62 }
63