1 /* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C
4 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 /* Define-away any (possibly conflicting) prototype of putenv.
27 Many systems omit the `const' attribute on the argument. */
28 #define putenv _sys_putenv
30 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
33 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
36 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
42 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
45 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
46 # define memcpy(d,s,n) bcopy ((s), (d), (n))
50 # define environ __environ
52 extern char **environ;
56 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
61 const char *const name_end = strchr (string, '=');
67 /* Remove the variable from the environment. */
68 size = strlen (string);
69 for (ep = environ; *ep != NULL; ++ep)
70 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
83 for (ep = environ; *ep != NULL; ++ep)
84 if (!strncmp (*ep, string, name_end - string) &&
85 (*ep)[name_end - string] == '=')
92 static char **last_environ = NULL;
93 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
94 if (new_environ == NULL)
96 (void) memcpy ((void *) new_environ, (void *) environ,
97 size * sizeof (char *));
98 new_environ[size] = (char *) string;
99 new_environ[size + 1] = NULL;
100 if (last_environ != NULL)
101 free ((void *) last_environ);
102 last_environ = new_environ;
103 environ = new_environ;
106 *ep = (char *) string;