1 /* RCS $Id: putenv.c,v 1.1.1.1 2000-09-22 15:33:35 hr Exp $
4 -- My own putenv for BSD like systems.
7 -- This originally came from MKS, but I rewrote it to fix a bug with
8 -- replacing existing strings, probably never happened but the code
9 -- was wrong nonetheless.
12 -- Dennis Vadura, dvadura@dmake.wticorp.com
15 -- http://dmake.wticorp.com/
18 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
20 -- This program is NOT free software; you can redistribute it and/or
21 -- modify it under the terms of the Software License Agreement Provided
22 -- in the file <distribution-root>/readme/license.txt.
25 -- Use cvs log to obtain detailed change logs.
34 Take a string of the form NAME=value and stick it into the environment.
35 We do this by allocating a new set of pointers if we have to add a new
36 string and by replacing an existing pointer if the value replaces the value
37 of an existing string. */
40 extern char **environ
; /* The current environment. */
41 static char **ourenv
= NULL
; /* A new environment */
46 /* First search the current environment and see if we can replace a
48 for( p
=environ
; *p
; p
++ ) {
49 register char *s
= str
;
51 for( q
= *p
; *q
&& *s
&& *s
== *q
; q
++, s
++ )
54 return(0); /* replaced it so go away */
58 /* Ok, can't replace a string so need to grow the environment. */
59 size
= p
- environ
+ 2; /* size of new environment */
60 /* size of old is size-1 */
62 /* It's the first time, so allocate a new environment since we don't know
63 * where the old one is comming from. */
64 if( ourenv
== NULL
) {
65 if( (ourenv
= (char **) malloc( sizeof(char *)*size
)) == NULL
)
68 memcpy( (char *)ourenv
, (char *)environ
, (size
-2)*sizeof(char *) );
70 else if( (ourenv
= (char **)realloc( ourenv
, size
*sizeof(char *))) == NULL
)
73 ourenv
[--size
] = NULL
;