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 #include <errno.h>
8 #include <sys/types.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <argz.h>
12 #include <envz.h>
13 
14 error_t
envz_add(char ** envz,size_t * envz_len,const char * name,const char * value)15 envz_add (char **envz,
16        size_t *envz_len,
17        const char *name,
18        const char *value)
19 {
20   char *concat = NULL;
21   int name_len = 0;
22   int val_len = 0;
23   int retval = 0;
24 
25   envz_remove(envz, envz_len, name);
26 
27   if (value)
28     {
29       name_len = strlen(name);
30       val_len = strlen(value);
31       if(!(concat = (char *) malloc(name_len + val_len + 2)))
32         return ENOMEM;
33 
34       memcpy(concat, name, name_len);
35       concat[name_len] = '=';
36       memcpy(concat + name_len + 1, value, val_len + 1);
37 
38       retval = argz_add(envz, envz_len, concat);
39       free(concat);
40     }
41   else
42     {
43       retval = argz_add(envz, envz_len, name);
44     }
45   return retval;
46 }
47