merge with 1.10n1
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <sys/types.h>
22 #include <errno.h>
23 #ifndef errno
24 extern int errno;
25 #endif
26
27 /* Don't include stdlib.h for non-GNU C libraries because some of them
28    contain conflicting prototypes for getopt.
29    This needs to come after some library #include
30    to get __GNU_LIBRARY__ defined.  */
31 #ifdef  __GNU_LIBRARY__
32 #include <stdlib.h>
33 #else
34 char *malloc ();
35 #endif  /* GNU C library.  */
36
37 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
38 #include <string.h>
39 #else
40 #include <strings.h>
41 #ifndef strchr
42 #define strchr index
43 #endif
44 #ifndef memcpy
45 #define memcpy(d, s, n) bcopy((s), (d), (n))
46 #endif
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #ifndef NULL
54 #define NULL 0
55 #endif
56
57 extern char **environ;
58
59 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
60 int
61 putenv (string)
62      const char *string;
63 {
64   char *name_end = strchr (string, '=');
65   register size_t size;
66   register char **ep;
67
68   if (name_end == NULL)
69     {
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] == '=')
74           {
75             while (ep[1] != NULL)
76               {
77                 ep[0] = ep[1];
78                 ++ep;
79               }
80             *ep = NULL;
81             return 0;
82           }
83     }
84
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;
92
93   if (*ep == NULL)
94     {
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       memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
100       new_environ[size] = (char *) string;
101       new_environ[size + 1] = NULL;
102       if (last_environ != NULL)
103         free ((char *) last_environ);
104       last_environ = new_environ;
105       environ = new_environ;
106     }
107   else
108     *ep = (char *) string;
109
110   return 0;
111 }