1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Hacked slightly by jjc@jclark.com for groff. */
28 typedef size_t SIZE_T;
29 #else /* not __STDC__ */
32 #endif /* not __STDC__ */
36 #else /* not HAVE_STDLIB_H */
38 #endif /* not HAVE_STDLIB_H */
44 extern char **environ;
46 /* Put STRING, which is of the form `NAME=VALUE', in the environment. */
48 int putenv(const char *string)
50 char *name_end = strchr(string, '=');
56 /* Remove the variable from the environment. */
57 size = strlen(string);
58 for (ep = environ; *ep != NULL; ++ep)
59 if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
72 for (ep = environ; *ep != NULL; ++ep)
73 if (!strncmp(*ep, string, name_end - string)
74 && (*ep)[name_end - string] == '=')
81 static char **last_environ = NULL;
82 char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
83 if (new_environ == NULL)
85 (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *));
86 new_environ[size] = (char *) string;
87 new_environ[size + 1] = NULL;
88 if (last_environ != NULL)
89 free((PTR) last_environ);
90 last_environ = new_environ;
91 environ = new_environ;
94 *ep = (char *) string;