*** empty log message ***
[coreutils.git] / lib / putenv.c
blob0720de5232d9108126593305cb2102aac043e370
1 /* Copyright (C) 1991, 1994, 1997, 1998 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
9 later version.
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. */
20 #include <errno.h>
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <sys/types.h>
28 /* Don't include stdlib.h because some (e.g., Solaris 2.7) declare putenv
29 with a non-const argument. That would conflict with the declaration of
30 rpl_putenv below (due to the #define putenv rpl_putenv from config.h). */
32 char *malloc ();
33 void free ();
35 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
36 # include <string.h>
37 #endif
38 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
39 # include <unistd.h>
40 #endif
42 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
43 # define strchr index
44 #endif
45 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
46 # define memcpy(d,s,n) bcopy ((s), (d), (n))
47 #endif
49 #if HAVE_GNU_LD
50 # define environ __environ
51 #else
52 extern char **environ;
53 #endif
55 #ifndef NULL
56 # define NULL 0
57 #endif
60 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
61 int
62 rpl_putenv (const char *string)
64 const char *const name_end = strchr (string, '=');
65 register size_t size;
66 register char **ep;
68 if (name_end == NULL)
70 /* Remove the variable from the environment. */
71 size = strlen (string);
72 for (ep = environ; *ep != NULL; ++ep)
73 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
75 while (ep[1] != NULL)
77 ep[0] = ep[1];
78 ++ep;
80 *ep = NULL;
81 return 0;
85 size = 0;
86 for (ep = environ; *ep != NULL; ++ep)
87 if (!strncmp (*ep, string, name_end - string) &&
88 (*ep)[name_end - string] == '=')
89 break;
90 else
91 ++size;
93 if (*ep == NULL)
95 static char **last_environ = NULL;
96 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
97 if (new_environ == NULL)
98 return -1;
99 (void) memcpy ((void *) new_environ, (void *) environ,
100 size * sizeof (char *));
101 new_environ[size] = (char *) string;
102 new_environ[size + 1] = NULL;
103 if (last_environ != NULL)
104 free ((void *) last_environ);
105 last_environ = new_environ;
106 environ = new_environ;
108 else
109 *ep = (char *) string;
111 return 0;