1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
30 * putenv -- put value into environment
32 * Usage: i = putenv (string)
36 * where string is of the form <name>=<value>.
37 * If "value" is 0, then "name" will be deleted from the environment.
38 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
40 * Putenv may need to add a new name into the environment, or to
41 * associate a value longer than the current value with a particular
42 * name. So, to make life simpler, putenv() copies your entire
43 * environment into the heap (i.e. malloc()) from the stack
44 * (i.e. where it resides when your process is initiated) the first
48 * 3-Sep-91 Michael Schroeder (mlschroe). Modified to behave as
50 * 16-Aug-91 Tim MacKenzie (tym) at Monash University. Modified for
51 * use in screen (iScreen) (ignores final int parameter)
52 * 14-Oct-85 Michael Mauldin (mlm) at Carnegie-Mellon University
53 * Ripped out of CMU lib for Rob-O-Matic portability
54 * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
55 * Created for VAX. Too bad Bell Labs didn't provide this. It's
56 * unfortunate that you have to copy the whole environment onto the
57 * heap, but the bookkeeping-and-not-so-much-copying approach turns
58 * out to be much hairier. So, I decided to do the simple thing,
59 * copying the entire environment onto the heap the first time you
60 * call putenv(), then doing realloc() uniformly later on.
73 char *malloc
__P((int));
74 char *realloc
__P((char *, int));
75 void free
__P((char *));
76 int sprintf
__P((char *, char *, ...));
78 #define EXTRASIZE 5 /* increment to add to env. size */
80 static int envsize
= -1; /* current size of environment */
81 extern char **environ
; /* the global which is your env. */
83 static int findenv
__P((char *)); /* look for a name in the env. */
84 static int newenv
__P((void)); /* copy env. from stack to heap */
85 static int moreenv
__P((void)); /* incr. size of env. */
94 { /* first time putenv called */
95 if (newenv() < 0) /* copy env. to heap */
100 return 0; /* Already here */
105 for (; environ
[i
]; i
++)
106 environ
[i
] = environ
[i
+1];
107 return 0; /* Already here */
118 { /* first time putenv called */
119 if (newenv() < 0) /* copy env. to heap */
123 i
= findenv(string
); /* look for name in environment */
126 { /* name must be added */
127 for (i
= 0; environ
[i
]; i
++);
128 if (i
>= (envsize
- 1))
129 { /* need new slot */
133 p
= malloc(strlen(string
) + 1);
134 if (p
== 0) /* not enough core */
136 environ
[i
+ 1] = 0; /* new end of env. */
139 { /* name already in env. */
140 p
= realloc(environ
[i
], strlen(string
) + 1);
144 sprintf(p
, "%s", string
); /* copy into env. */
154 register char *namechar
, *envchar
;
155 register int i
, found
;
158 for (i
= 0; environ
[i
] && !found
; i
++)
160 envchar
= environ
[i
];
162 while (*namechar
&& *namechar
!= '=' && (*namechar
== *envchar
))
167 found
= ((*namechar
== '\0' || *namechar
== '=') && *envchar
== '=');
169 return found
? i
- 1 : -1;
175 register char **env
, *elem
;
176 register int i
, esize
;
178 for (i
= 0; environ
[i
]; i
++)
180 esize
= i
+ EXTRASIZE
+ 1;
181 env
= (char **)malloc(esize
* sizeof (elem
));
185 for (i
= 0; environ
[i
]; i
++)
187 elem
= malloc(strlen(environ
[i
]) + 1);
191 strcpy(elem
, environ
[i
]);
206 esize
= envsize
+ EXTRASIZE
;
207 env
= (char **)realloc((char *)environ
, esize
* sizeof (*env
));
215 #endif /* NEEDPUTENV */