* This code is derived from software copyrighted by the Free Software
* Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
* Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
static char sccsid
[] = "@(#)environ.c 6.3 (Berkeley) 5/8/91";
/* environ.c -- library for manipulating environments for GNU.
Copyright (C) 1986, 1989 Free Software Foundation, Inc.
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; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
/* Return a new environment object. */
register struct environ
*e
;
e
= (struct environ
*) xmalloc (sizeof (struct environ
));
e
->vector
= (char **) xmalloc ((e
->allocated
+ 1) * sizeof (char *));
/* Free an environment and all the strings in it. */
register struct environ
*e
;
register char **vector
= e
->vector
;
/* Copy the environment given to this process into E.
Also copies all the strings in it, so we can be sure
that all strings in these environments are safe to free. */
register struct environ
*e
;
for (i
= 0; environ
[i
]; i
++);
e
->allocated
= max (i
, e
->allocated
+ 10);
e
->vector
= (char **) xrealloc (e
->vector
,
(e
->allocated
+ 1) * sizeof (char *));
bcopy (environ
, e
->vector
, (i
+ 1) * sizeof (char *));
register int len
= strlen (e
->vector
[i
]) + 1;
register char *new = (char *) xmalloc (len
);
bcopy (e
->vector
[i
], new, len
);
/* Return the vector of environment E.
This is used to get something to pass to execve. */
/* Return the value in environment E of variable VAR. */
register int len
= strlen (var
);
register char **vector
= e
->vector
;
for (; s
= *vector
; vector
++)
if (!strncmp (s
, var
, len
)
/* Store the value in E of VAR as VALUE. */
set_in_environ (e
, var
, value
)
register int len
= strlen (var
);
register char **vector
= e
->vector
;
for (i
= 0; s
= vector
[i
]; i
++)
if (!strncmp (s
, var
, len
)
vector
= (char **) xrealloc (vector
,
(e
->allocated
+ 1) * sizeof (char *));
s
= (char *) xmalloc (len
+ strlen (value
) + 2);
/* Remove the setting for variable VAR from environment E. */
unset_in_environ (e
, var
)
register int len
= strlen (var
);
register char **vector
= e
->vector
;
for (; s
= *vector
; vector
++)
if (!strncmp (s
, var
, len
)
bcopy (vector
+ 1, vector
,
(e
->allocated
- (vector
- e
->vector
)) * sizeof (char *));
e
->vector
[e
->allocated
- 1] = 0;