1 /* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
4 NOTE: The canonical source of this file is maintained with the GNU C Library.
5 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
30 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
33 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
37 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
40 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
41 #define memcpy(d,s,n) bcopy ((s), (d), (n))
45 #define environ __environ
47 extern char **environ;
51 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
56 const char *const name_end = strchr (string, '=');
62 /* Remove the variable from the environment. */
63 size = strlen (string);
64 for (ep = environ; *ep != NULL; ++ep)
65 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
78 for (ep = environ; *ep != NULL; ++ep)
79 if (!strncmp (*ep, string, name_end - string) &&
80 (*ep)[name_end - string] == '=')
87 static char **last_environ = NULL;
88 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
89 if (new_environ == NULL)
91 (void) memcpy ((void *) new_environ, (void *) environ,
92 size * sizeof (char *));
93 new_environ[size] = (char *) string;
94 new_environ[size + 1] = NULL;
95 if (last_environ != NULL)
96 free ((void *) last_environ);
97 last_environ = new_environ;
98 environ = new_environ;
101 *ep = (char *) string;