1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, see
18 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 ****************************************************************
25 * putenv -- put value into environment
27 * Usage: i = putenv (string)
31 * where string is of the form <name>=<value>.
32 * If "value" is 0, then "name" will be deleted from the environment.
33 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
35 * Putenv may need to add a new name into the environment, or to
36 * associate a value longer than the current value with a particular
37 * name. So, to make life simpler, putenv() copies your entire
38 * environment into the heap (i.e. malloc()) from the stack
39 * (i.e. where it resides when your process is initiated) the first
43 * 3-Sep-91 Michael Schroeder (mlschroe). Modified to behave as
45 * 16-Aug-91 Tim MacKenzie (tym) at Monash University. Modified for
46 * use in screen (iScreen) (ignores final int parameter)
47 * 14-Oct-85 Michael Mauldin (mlm) at Carnegie-Mellon University
48 * Ripped out of CMU lib for Rob-O-Matic portability
49 * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
50 * Created for VAX. Too bad Bell Labs didn't provide this. It's
51 * unfortunate that you have to copy the whole environment onto the
52 * heap, but the bookkeeping-and-not-so-much-copying approach turns
53 * out to be much hairier. So, I decided to do the simple thing,
54 * copying the entire environment onto the heap the first time you
55 * call putenv(), then doing realloc() uniformly later on.
68 char *malloc
__P((int));
69 char *realloc
__P((char *, int));
70 void free
__P((char *));
71 int sprintf
__P((char *, char *, ...));
73 #define EXTRASIZE 5 /* increment to add to env. size */
75 static int envsize
= -1; /* current size of environment */
76 extern char **environ
; /* the global which is your env. */
78 static int findenv
__P((char *)); /* look for a name in the env. */
79 static int newenv
__P((void)); /* copy env. from stack to heap */
80 static int moreenv
__P((void)); /* incr. size of env. */
89 { /* first time putenv called */
90 if (newenv() < 0) /* copy env. to heap */
95 return 0; /* Already here */
100 for (; environ
[i
]; i
++)
101 environ
[i
] = environ
[i
+1];
102 return 0; /* Already here */
113 { /* first time putenv called */
114 if (newenv() < 0) /* copy env. to heap */
118 i
= findenv(string
); /* look for name in environment */
121 { /* name must be added */
122 for (i
= 0; environ
[i
]; i
++);
123 if (i
>= (envsize
- 1))
124 { /* need new slot */
128 p
= malloc(strlen(string
) + 1);
129 if (p
== 0) /* not enough core */
131 environ
[i
+ 1] = 0; /* new end of env. */
134 { /* name already in env. */
135 p
= realloc(environ
[i
], strlen(string
) + 1);
139 sprintf(p
, "%s", string
); /* copy into env. */
149 register char *namechar
, *envchar
;
150 register int i
, found
;
153 for (i
= 0; environ
[i
] && !found
; i
++)
155 envchar
= environ
[i
];
157 while (*namechar
&& *namechar
!= '=' && (*namechar
== *envchar
))
162 found
= ((*namechar
== '\0' || *namechar
== '=') && *envchar
== '=');
164 return found
? i
- 1 : -1;
170 register char **env
, *elem
;
171 register int i
, esize
;
173 for (i
= 0; environ
[i
]; i
++)
175 esize
= i
+ EXTRASIZE
+ 1;
176 env
= (char **)malloc(esize
* sizeof (elem
));
180 for (i
= 0; environ
[i
]; i
++)
182 elem
= malloc(strlen(environ
[i
]) + 1);
186 strcpy(elem
, environ
[i
]);
201 esize
= envsize
+ EXTRASIZE
;
202 env
= (char **)realloc((char *)environ
, esize
* sizeof (*env
));
210 #endif /* NEEDPUTENV */