4c161a420d3796c45244771f494029c67e71c4cb
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
2
3
4 NOTE: The canonical source of this file is maintained with the GNU C Library.
5 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include <errno.h>
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
28 #include <stdlib.h>
29 #endif
30 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
31 #include <string.h>
32 #endif
33 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
34 #include <unistd.h>
35 #endif
36
37 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
38 #define strchr index
39 #endif
40 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
41 #define memcpy(d,s,n) bcopy ((s), (d), (n))
42 #endif
43
44 #if HAVE_GNU_LD
45 #define environ __environ
46 #else
47 extern char **environ;
48 #endif
49
50
51 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
52 int
53 putenv (string)
54      const char *string;
55 {
56   const char *const name_end = strchr (string, '=');
57   register size_t size;
58   register char **ep;
59
60   if (name_end == NULL)
61     {
62       /* Remove the variable from the environment.  */
63       size = strlen (string);
64       for (ep = environ; *ep != NULL; ++ep)
65         if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
66           {
67             while (ep[1] != NULL)
68               {
69                 ep[0] = ep[1];
70                 ++ep;
71               }
72             *ep = NULL;
73             return 0;
74           }
75     }
76
77   size = 0;
78   for (ep = environ; *ep != NULL; ++ep)
79     if (!strncmp (*ep, string, name_end - string) &&
80         (*ep)[name_end - string] == '=')
81       break;
82     else
83       ++size;
84
85   if (*ep == NULL)
86     {
87       static char **last_environ = NULL;
88       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
89       if (new_environ == NULL)
90         return -1;
91       (void) memcpy ((void *) new_environ, (void *) environ,
92                      size * sizeof (char *));
93       new_environ[size] = (char *) string;
94       new_environ[size + 1] = NULL;
95       if (last_environ != NULL)
96         free ((void *) last_environ);
97       last_environ = new_environ;
98       environ = new_environ;
99     }
100   else
101     *ep = (char *) string;
102
103   return 0;
104 }