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           if (!name_str)
34               return ENOMEM;
35           name_iter = strchr(name_str, '=');
36           if(name_iter)
37             *name_iter = '\0';
38 
39           if(!envz_entry(*envz, *envz_len, name_str))
40             {
41               retval = argz_add(envz, envz_len, entry);
42             }
43           free(name_str);
44         }
45       else
46         {
47           name_str = strdup (entry);
48           if (!name_str)
49               return ENOMEM;
50           name_iter = strchr(name_str, '=');
51           if(name_iter)
52             {
53               *name_iter = '\0';
54               val_str = name_iter + 1;
55             }
56           else
57             {
58               val_str = NULL;
59             }
60 
61           retval = envz_add(envz, envz_len, name_str, val_str);
62           free(name_str);
63         }
64     }
65   return retval;
66 }
67