1 /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2  * This file is part of the Linux-8086 C library and is distributed
3  * under the GNU Library General Public License.
4  */
5 #include <string.h>
6 #include <stdlib.h>
7 #include <malloc.h>
8 
9 extern char ** environ;
10 #define ADD_NUM 4
11 
12 int
putenv(var)13 putenv(var)
14 char * var;
15 {
16 static char ** mall_env = 0;
17 static int extras = 0;
18    char **p, **d;
19    char * r;
20    int len;
21 
22    r = strchr(var, '=');
23    if( r == 0 )  len = strlen(var);
24    else          len = r-var;
25 
26    for(p=environ; *p; p++)
27    {
28       if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' )
29       {
30          while( p[0] = p[1] ) p++;
31          extras++;
32          break;
33       }
34    }
35    if( r == 0 ) return 0;
36    if( extras <= 0 )	/* Need more space */
37    {
38       d = malloc((p-environ+1+ADD_NUM)*sizeof(char*));
39       if( d == 0 ) return -1;
40 
41       memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*));
42       p = d + (p-environ);
43       extras=ADD_NUM;
44 
45       if( mall_env ) free(mall_env);
46       environ = d;
47       mall_env = d;
48    }
49    *p++ = var;
50    *p = '\0';
51    extras--;
52 
53    return 0;
54 }
55 
56 
57