2000-04-07 17:24:18 +08:00
|
|
|
/* Copyright (C) 1991, 1994, 1997, 1998, 2000 Free Software Foundation, Inc.
|
1992-11-01 13:44:29 +08:00
|
|
|
|
1997-02-04 11:30:49 +08:00
|
|
|
NOTE: The canonical source of this file is maintained with the GNU C
|
|
|
|
Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
|
1992-11-01 13:44:29 +08:00
|
|
|
|
1997-02-04 11:30:49 +08:00
|
|
|
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 2, or (at your option) any
|
|
|
|
later version.
|
1992-11-01 13:44:29 +08:00
|
|
|
|
1997-02-04 11:30:49 +08:00
|
|
|
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.
|
1992-11-01 13:44:29 +08:00
|
|
|
|
1997-02-04 11:30:49 +08:00
|
|
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1993-10-12 22:49:11 +08:00
|
|
|
|
1997-02-04 11:30:49 +08:00
|
|
|
#if HAVE_CONFIG_H
|
1996-07-15 11:56:06 +08:00
|
|
|
# include <config.h>
|
1994-10-01 12:48:44 +08:00
|
|
|
#endif
|
|
|
|
|
1998-02-03 15:23:13 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-04-07 17:24:18 +08:00
|
|
|
/* Include errno.h *after* sys/types.h to work around header problems
|
|
|
|
on AIX 3.2.5. */
|
|
|
|
#include <errno.h>
|
|
|
|
|
1998-12-14 02:04:11 +08:00
|
|
|
/* Don't include stdlib.h because some (e.g., Solaris 2.7) declare putenv
|
|
|
|
with a non-const argument. That would conflict with the declaration of
|
|
|
|
rpl_putenv below (due to the #define putenv rpl_putenv from config.h). */
|
|
|
|
|
|
|
|
char *malloc ();
|
|
|
|
void free ();
|
1998-12-07 06:18:17 +08:00
|
|
|
|
1994-12-05 05:08:50 +08:00
|
|
|
#if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
|
1996-07-15 11:56:06 +08:00
|
|
|
# include <string.h>
|
1994-10-01 12:48:44 +08:00
|
|
|
#endif
|
1994-12-05 05:08:50 +08:00
|
|
|
#if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
|
1996-07-15 11:56:06 +08:00
|
|
|
# include <unistd.h>
|
1994-10-01 12:48:44 +08:00
|
|
|
#endif
|
1992-11-01 13:44:29 +08:00
|
|
|
|
1994-12-05 05:08:50 +08:00
|
|
|
#if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
|
1996-07-15 11:56:06 +08:00
|
|
|
# define strchr index
|
1994-12-05 05:08:50 +08:00
|
|
|
#endif
|
|
|
|
#if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
|
1996-07-15 11:56:06 +08:00
|
|
|
# define memcpy(d,s,n) bcopy ((s), (d), (n))
|
1992-11-01 13:44:29 +08:00
|
|
|
#endif
|
|
|
|
|
1994-12-05 05:08:50 +08:00
|
|
|
#if HAVE_GNU_LD
|
1996-07-15 11:56:06 +08:00
|
|
|
# define environ __environ
|
1994-12-05 05:08:50 +08:00
|
|
|
#else
|
1994-10-01 12:48:44 +08:00
|
|
|
extern char **environ;
|
1994-12-05 05:08:50 +08:00
|
|
|
#endif
|
|
|
|
|
1998-02-03 15:23:13 +08:00
|
|
|
#ifndef NULL
|
|
|
|
# define NULL 0
|
|
|
|
#endif
|
|
|
|
|
1994-10-01 12:48:44 +08:00
|
|
|
|
1992-11-01 13:44:29 +08:00
|
|
|
/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
|
|
|
|
int
|
1998-12-07 06:18:17 +08:00
|
|
|
rpl_putenv (const char *string)
|
1992-11-01 13:44:29 +08:00
|
|
|
{
|
1994-12-05 05:08:50 +08:00
|
|
|
const char *const name_end = strchr (string, '=');
|
1992-11-01 13:44:29 +08:00
|
|
|
register size_t size;
|
|
|
|
register char **ep;
|
|
|
|
|
|
|
|
if (name_end == NULL)
|
|
|
|
{
|
|
|
|
/* Remove the variable from the environment. */
|
1994-10-01 12:48:44 +08:00
|
|
|
size = strlen (string);
|
|
|
|
for (ep = environ; *ep != NULL; ++ep)
|
|
|
|
if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
|
1992-11-01 13:44:29 +08:00
|
|
|
{
|
|
|
|
while (ep[1] != NULL)
|
|
|
|
{
|
|
|
|
ep[0] = ep[1];
|
|
|
|
++ep;
|
|
|
|
}
|
|
|
|
*ep = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size = 0;
|
1994-10-01 12:48:44 +08:00
|
|
|
for (ep = environ; *ep != NULL; ++ep)
|
|
|
|
if (!strncmp (*ep, string, name_end - string) &&
|
1992-11-01 13:44:29 +08:00
|
|
|
(*ep)[name_end - string] == '=')
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++size;
|
|
|
|
|
|
|
|
if (*ep == NULL)
|
|
|
|
{
|
|
|
|
static char **last_environ = NULL;
|
1994-10-01 12:48:44 +08:00
|
|
|
char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
|
1992-11-01 13:44:29 +08:00
|
|
|
if (new_environ == NULL)
|
|
|
|
return -1;
|
1994-12-05 05:08:50 +08:00
|
|
|
(void) memcpy ((void *) new_environ, (void *) environ,
|
|
|
|
size * sizeof (char *));
|
1992-11-01 13:44:29 +08:00
|
|
|
new_environ[size] = (char *) string;
|
|
|
|
new_environ[size + 1] = NULL;
|
|
|
|
if (last_environ != NULL)
|
1994-12-05 05:08:50 +08:00
|
|
|
free ((void *) last_environ);
|
1992-11-01 13:44:29 +08:00
|
|
|
last_environ = new_environ;
|
1994-10-01 12:48:44 +08:00
|
|
|
environ = new_environ;
|
1992-11-01 13:44:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*ep = (char *) string;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|