indent cpp-directives
[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 Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <errno.h>
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Define-away any (possibly conflicting) prototype of putenv.
28    Many systems omit the `const' attribute on the argument.  */
29 #define putenv _sys_putenv
30
31 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
32 # include <stdlib.h>
33 #endif
34 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
35 # include <string.h>
36 #endif
37 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
40
41 #undef putenv
42
43 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
44 # define strchr index
45 #endif
46 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
47 # define memcpy(d,s,n) bcopy ((s), (d), (n))
48 #endif
49
50 #if HAVE_GNU_LD
51 # define environ __environ
52 #else
53 extern char **environ;
54 #endif
55
56
57 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
58 int
59 putenv (string)
60      const char *string;
61 {
62   const char *const name_end = strchr (string, '=');
63   register size_t size;
64   register char **ep;
65
66   if (name_end == NULL)
67     {
68       /* Remove the variable from the environment.  */
69       size = strlen (string);
70       for (ep = environ; *ep != NULL; ++ep)
71         if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
72           {
73             while (ep[1] != NULL)
74               {
75                 ep[0] = ep[1];
76                 ++ep;
77               }
78             *ep = NULL;
79             return 0;
80           }
81     }
82
83   size = 0;
84   for (ep = environ; *ep != NULL; ++ep)
85     if (!strncmp (*ep, string, name_end - string) &&
86         (*ep)[name_end - string] == '=')
87       break;
88     else
89       ++size;
90
91   if (*ep == NULL)
92     {
93       static char **last_environ = NULL;
94       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
95       if (new_environ == NULL)
96         return -1;
97       (void) memcpy ((void *) new_environ, (void *) environ,
98                      size * sizeof (char *));
99       new_environ[size] = (char *) string;
100       new_environ[size + 1] = NULL;
101       if (last_environ != NULL)
102         free ((void *) last_environ);
103       last_environ = new_environ;
104       environ = new_environ;
105     }
106   else
107     *ep = (char *) string;
108
109   return 0;
110 }