1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 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
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #define __environ environ
30 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
32 DEFUN(putenv, (string), CONST char *string)
34 CONST char *CONST name_end = strchr(string, '=');
40 /* Remove the variable from the environment. */
41 size = strlen(string);
42 for (ep = __environ; *ep != NULL; ++ep)
43 if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
56 for (ep = __environ; *ep != NULL; ++ep)
57 if (!strncmp(*ep, string, name_end - string) &&
58 (*ep)[name_end - string] == '=')
65 static char **last_environ = NULL;
66 char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
67 if (new_environ == NULL)
69 (void) memcpy((PTR) new_environ, (PTR) __environ, size * sizeof(char *));
70 new_environ[size] = (char *) string;
71 new_environ[size + 1] = NULL;
72 if (last_environ != NULL)
73 free((PTR) last_environ);
74 last_environ = new_environ;
75 __environ = new_environ;
78 *ep = (char *) string;