*** empty log message ***
[coreutils.git] / lib / putenv.c
blob58a498fdaa478994071938fb5765c9e2e503e358
1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000 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 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <sys/types.h>
26 /* Include errno.h *after* sys/types.h to work around header problems
27 on AIX 3.2.5. */
28 #include <errno.h>
30 /* Don't include stdlib.h because some (e.g., Solaris 2.7) declare putenv
31 with a non-const argument. That would conflict with the declaration of
32 rpl_putenv below (due to the #define putenv rpl_putenv from config.h). */
34 char *malloc ();
35 void free ();
37 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
38 # include <string.h>
39 #endif
40 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
41 # include <unistd.h>
42 #endif
44 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
45 # define strchr index
46 #endif
47 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
48 # define memcpy(d,s,n) bcopy ((s), (d), (n))
49 #endif
51 #if HAVE_GNU_LD
52 # define environ __environ
53 #else
54 extern char **environ;
55 #endif
57 #ifndef NULL
58 # define NULL 0
59 #endif
62 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
63 int
64 rpl_putenv (const char *string)
66 const char *const name_end = strchr (string, '=');
67 register size_t size;
68 register char **ep;
70 if (name_end == NULL)
72 /* Remove the variable from the environment. */
73 size = strlen (string);
74 for (ep = environ; *ep != NULL; ++ep)
75 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
77 while (ep[1] != NULL)
79 ep[0] = ep[1];
80 ++ep;
82 *ep = NULL;
83 return 0;
87 size = 0;
88 for (ep = environ; *ep != NULL; ++ep)
89 if (!strncmp (*ep, string, name_end - string) &&
90 (*ep)[name_end - string] == '=')
91 break;
92 else
93 ++size;
95 if (*ep == NULL)
97 static char **last_environ = NULL;
98 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
99 if (new_environ == NULL)
100 return -1;
101 (void) memcpy ((void *) new_environ, (void *) environ,
102 size * sizeof (char *));
103 new_environ[size] = (char *) string;
104 new_environ[size + 1] = NULL;
105 if (last_environ != NULL)
106 free ((void *) last_environ);
107 last_environ = new_environ;
108 environ = new_environ;
110 else
111 *ep = (char *) string;
113 return 0;