* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Copyright (c) 1987 Oliver Laumann
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* Noteworthy contributors to screen's design and implementation:
* Wayne Davison (davison@borland.com)
* Patrick Wolfe (pat@kai.com, kailand!pat)
* Bart Schaefer (schaefer@cse.ogi.edu)
* Nathan Glasser (nathan@brokaw.lcs.mit.edu)
* Larry W. Virden (lwv27%cas.BITNET@CUNYVM.CUNY.Edu)
* Howard Chu (hyc@hanauma.jpl.nasa.gov)
* Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
* Markku Jarvinen (mta@{cc,cs,ee}.tut.fi)
* Marc Boucher (marc@CAM.ORG)
****************************************************************
* putenv -- put value into environment
* Usage: i = putenv (string)
* where string is of the form <name>=<value>.
* If "value" is 0, then "name" will be deleted from the environment.
* Putenv returns 0 normally, -1 on error (not enough core for malloc).
* Putenv may need to add a new name into the environment, or to
* associate a value longer than the current value with a particular
* name. So, to make life simpler, putenv() copies your entire
* environment into the heap (i.e. malloc()) from the stack
* (i.e. where it resides when your process is initiated) the first
* 3-Sep-91 Michael Schroeder (mlschroe). Modified to behave as
* 16-Aug-91 Tim MacKenzie (tym) at Monash University. Modified for
* use in screen (iScreen) (ignores final int parameter)
* 14-Oct-85 Michael Mauldin (mlm) at Carnegie-Mellon University
* Ripped out of CMU lib for Rob-O-Matic portability
* 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
* Created for VAX. Too bad Bell Labs didn't provide this. It's
* unfortunate that you have to copy the whole environment onto the
* heap, but the bookkeeping-and-not-so-much-copying approach turns
* out to be much hairier. So, I decided to do the simple thing,
* copying the entire environment onto the heap the first time you
* call putenv(), then doing realloc() uniformly later on.
#define EXTRASIZE 5 /* increment to add to env. size */
char *index(), *malloc(), *realloc();
static int envsize
= -1; /* current size of environment */
extern char **environ
; /* the global which is your env. */
static int findenv(); /* look for a name in the env. */
static int newenv(); /* copy env. from stack to heap */
static int moreenv(); /* incr. size of env. */
return; /* Already here */
environ
[i
] = environ
[i
+1];
{ /* first time putenv called */
if (newenv() < 0) /* copy env. to heap */
i
= findenv(string
); /* look for name in environment */
{ /* name must be added */
for (i
= 0; environ
[i
]; i
++);
p
= malloc(strlen(string
) + 1);
if (p
== 0) /* not enough core */
environ
[i
+ 1] = 0; /* new end of env. */
{ /* name already in env. */
p
= realloc(environ
[i
], strlen(string
) + 1);
sprintf(p
, "%s", string
); /* copy into env. */
register char *namechar
, *envchar
;
for (i
= 0; environ
[i
] && !found
; i
++)
while (*namechar
&& *namechar
!= '=' && (*namechar
== *envchar
))
found
= ((*namechar
== '\0' || *namechar
== '=') && *envchar
== '=');
return (found
? i
- 1 : -1);
register char **env
, *elem
;
for (i
= 0; environ
[i
]; i
++)
esize
= i
+ EXTRASIZE
+ 1;
env
= (char **)malloc(esize
* sizeof (elem
));
for (i
= 0; environ
[i
]; i
++)
elem
= malloc(strlen(environ
[i
]) + 1);
strcpy(elem
, environ
[i
]);
esize
= envsize
+ EXTRASIZE
;
env
= (char **)realloc(environ
, esize
* sizeof (*env
));