1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-18 01:40:06 +02:00
Files
coreutils/lib/putenv.c

111 lines
2.8 KiB
C
Raw Normal View History

/* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
1992-11-01 05:44:29 +00: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 05:44:29 +00: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 05:44:29 +00: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.
You should have received a copy of the GNU General Public License
1996-07-15 03:43:36 +00:00
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 14:49:11 +00:00
1992-11-01 05:44:29 +00:00
#include <errno.h>
#ifdef HAVE_CONFIG_H
1996-07-15 03:56:06 +00:00
# include <config.h>
1994-10-01 04:48:44 +00:00
#endif
/* Define-away any (possibly conflicting) prototype of putenv.
Many systems omit the `const' attribute on the argument. */
#define putenv _sys_putenv
#if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
1996-07-15 03:56:06 +00:00
# include <stdlib.h>
1994-10-01 04:48:44 +00:00
#endif
#if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
1996-07-15 03:56:06 +00:00
# include <string.h>
1994-10-01 04:48:44 +00:00
#endif
#if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
1996-07-15 03:56:06 +00:00
# include <unistd.h>
1994-10-01 04:48:44 +00:00
#endif
1992-11-01 05:44:29 +00:00
#undef putenv
#if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
1996-07-15 03:56:06 +00:00
# define strchr index
#endif
#if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
1996-07-15 03:56:06 +00:00
# define memcpy(d,s,n) bcopy ((s), (d), (n))
1992-11-01 05:44:29 +00:00
#endif
#if HAVE_GNU_LD
1996-07-15 03:56:06 +00:00
# define environ __environ
#else
1994-10-01 04:48:44 +00:00
extern char **environ;
#endif
1994-10-01 04:48:44 +00:00
1992-11-01 05:44:29 +00:00
/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
int
1994-10-01 04:48:44 +00:00
putenv (string)
const char *string;
1992-11-01 05:44:29 +00:00
{
const char *const name_end = strchr (string, '=');
1992-11-01 05:44:29 +00:00
register size_t size;
register char **ep;
if (name_end == NULL)
{
/* Remove the variable from the environment. */
1994-10-01 04:48:44 +00:00
size = strlen (string);
for (ep = environ; *ep != NULL; ++ep)
if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
1992-11-01 05:44:29 +00:00
{
while (ep[1] != NULL)
{
ep[0] = ep[1];
++ep;
}
*ep = NULL;
return 0;
}
}
size = 0;
1994-10-01 04:48:44 +00:00
for (ep = environ; *ep != NULL; ++ep)
if (!strncmp (*ep, string, name_end - string) &&
1992-11-01 05:44:29 +00:00
(*ep)[name_end - string] == '=')
break;
else
++size;
if (*ep == NULL)
{
static char **last_environ = NULL;
1994-10-01 04:48:44 +00:00
char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
1992-11-01 05:44:29 +00:00
if (new_environ == NULL)
return -1;
(void) memcpy ((void *) new_environ, (void *) environ,
size * sizeof (char *));
1992-11-01 05:44:29 +00:00
new_environ[size] = (char *) string;
new_environ[size + 1] = NULL;
if (last_environ != NULL)
free ((void *) last_environ);
1992-11-01 05:44:29 +00:00
last_environ = new_environ;
1994-10-01 04:48:44 +00:00
environ = new_environ;
1992-11-01 05:44:29 +00:00
}
else
*ep = (char *) string;
return 0;
}