Redefine putenv before including stdlib.h to work
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997, 1998 Free Software Foundation, Inc.
2
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.
5
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.
10
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.
15
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.  */
19
20 #include <errno.h>
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 /* Disable the definition of putenv to rpl_putenv (from config.h) in this
27    file.  Otherwise, we'd get conflicting prototypes for rpl_putenv on
28    systems like Irix 5.3.  */
29 #undef putenv
30
31 #include <sys/types.h>
32
33 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
34 /* Some stdlib.h (e.g., Solaris 2.7) declare putenv with a non-const argument.
35    Since that would conflict with the declaration below, we rename putenv in
36    that incompatible prototype.  */
37 # define putenv vendor_putenv_prototype
38 # include <stdlib.h>
39 # undef putenv
40 #endif
41
42 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
43 # include <string.h>
44 #endif
45 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
46 # include <unistd.h>
47 #endif
48
49 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
50 # define strchr index
51 #endif
52 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
53 # define memcpy(d,s,n) bcopy ((s), (d), (n))
54 #endif
55
56 #if HAVE_GNU_LD
57 # define environ __environ
58 #else
59 extern char **environ;
60 #endif
61
62 #ifndef NULL
63 # define NULL 0
64 #endif
65
66
67 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
68 int
69 rpl_putenv (const char *string)
70 {
71   const char *const name_end = strchr (string, '=');
72   register size_t size;
73   register char **ep;
74
75   if (name_end == NULL)
76     {
77       /* Remove the variable from the environment.  */
78       size = strlen (string);
79       for (ep = environ; *ep != NULL; ++ep)
80         if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
81           {
82             while (ep[1] != NULL)
83               {
84                 ep[0] = ep[1];
85                 ++ep;
86               }
87             *ep = NULL;
88             return 0;
89           }
90     }
91
92   size = 0;
93   for (ep = environ; *ep != NULL; ++ep)
94     if (!strncmp (*ep, string, name_end - string) &&
95         (*ep)[name_end - string] == '=')
96       break;
97     else
98       ++size;
99
100   if (*ep == NULL)
101     {
102       static char **last_environ = NULL;
103       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
104       if (new_environ == NULL)
105         return -1;
106       (void) memcpy ((void *) new_environ, (void *) environ,
107                      size * sizeof (char *));
108       new_environ[size] = (char *) string;
109       new_environ[size + 1] = NULL;
110       if (last_environ != NULL)
111         free ((void *) last_environ);
112       last_environ = new_environ;
113       environ = new_environ;
114     }
115   else
116     *ep = (char *) string;
117
118   return 0;
119 }