2 * __put_env.c - common code for putenv() and setenv()
10 /* Initialized to zero, meaning "not malloc'd" */
11 static size_t __environ_size;
13 /* str should be a duplicated version of the input string;
14 len is the length of the key including the = sign */
15 int __put_env(char *str, size_t len, int overwrite)
21 n = 1; /* Include space for final NULL */
22 for ( p = environ ; (q = *p) ; p++ ) {
24 if ( !strncmp(q,str,len) ) {
28 *p = str; /* Possible memory leak... */
33 /* Need to extend the environment */
34 if ( n < __environ_size ) {
39 if ( __environ_size ) {
40 newenv = realloc(environ, (__environ_size << 1)*sizeof(char *));
46 /* Make a reasonable guess how much more space we need */
47 size_t newsize = n+32;
48 newenv = malloc(newsize*sizeof(char *));
52 memcpy(newenv, environ, n*sizeof(char *));
53 __environ_size = newsize;
55 newenv[n-1] = str; /* Old NULL position */