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 Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 /* Define-away any (possibly conflicting) prototype of putenv.
28 Many systems omit the `const' attribute on the argument. */
29 #define putenv _sys_putenv
31 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
34 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
37 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
43 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
46 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
47 # define memcpy(d,s,n) bcopy ((s), (d), (n))
51 # define environ __environ
52 #elif defined (__APPLE__)
53 # include <crt_externs.h>
54 # define environ (*_NSGetEnviron())
56 extern char **environ;
60 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
65 const char *const name_end = strchr (string, '=');
71 /* Remove the variable from the environment. */
72 size = strlen (string);
73 for (ep = environ; *ep != NULL; ++ep)
74 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
87 for (ep = environ; *ep != NULL; ++ep)
88 if (!strncmp (*ep, string, name_end - string) &&
89 (*ep)[name_end - string] == '=')
96 static char **last_environ = NULL;
97 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
98 if (new_environ == NULL)
100 (void) memcpy ((void *) new_environ, (void *) environ,
101 size * sizeof (char *));
102 new_environ[size] = (char *) string;
103 new_environ[size + 1] = NULL;
104 if (last_environ != NULL)
105 free ((void *) last_environ);
106 last_environ = new_environ;
107 environ = new_environ;
110 *ep = (char *) string;