coreutils/lib/putenv.c

109 lines
2.9 KiB
C
Raw Normal View History

/* Copyright (C) 1991, 1994, 1997 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
1992-11-01 13:44:29 +08:00
#include <errno.h>
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
/* Disable the definition of putenv to rpl_putenv (from config.h) in this
file. Otherwise, we'd get conflicting prototypes for rpl_putenv on
systems like Irix 5.3. */
#undef putenv
#if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
1996-07-15 11:56:06 +08:00
# include <stdlib.h>
1994-10-01 12:48:44 +08:00
#endif
#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
#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
#if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
1996-07-15 11:56:06 +08:00
# define strchr index
#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
#if HAVE_GNU_LD
1996-07-15 11:56:06 +08:00
# define environ __environ
#else
1994-10-01 12:48:44 +08:00
extern char **environ;
#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
rpl_putenv (string)
1994-10-01 12:48:44 +08:00
const char *string;
1992-11-01 13:44:29 +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;
(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)
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;
}