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