Remove _sys_putenv cruft and just rename the function itself.
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997 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 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
27 # include <stdlib.h>
28 #endif
29 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
30 # include <string.h>
31 #endif
32 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
33 # include <unistd.h>
34 #endif
35
36 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
37 # define strchr index
38 #endif
39 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
40 # define memcpy(d,s,n) bcopy ((s), (d), (n))
41 #endif
42
43 #if HAVE_GNU_LD
44 # define environ __environ
45 #else
46 extern char **environ;
47 #endif
48
49
50 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
51 int
52 rpl_putenv (string)
53      const char *string;
54 {
55   const char *const name_end = strchr (string, '=');
56   register size_t size;
57   register char **ep;
58
59   if (name_end == NULL)
60     {
61       /* Remove the variable from the environment.  */
62       size = strlen (string);
63       for (ep = environ; *ep != NULL; ++ep)
64         if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
65           {
66             while (ep[1] != NULL)
67               {
68                 ep[0] = ep[1];
69                 ++ep;
70               }
71             *ep = NULL;
72             return 0;
73           }
74     }
75
76   size = 0;
77   for (ep = environ; *ep != NULL; ++ep)
78     if (!strncmp (*ep, string, name_end - string) &&
79         (*ep)[name_end - string] == '=')
80       break;
81     else
82       ++size;
83
84   if (*ep == NULL)
85     {
86       static char **last_environ = NULL;
87       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
88       if (new_environ == NULL)
89         return -1;
90       (void) memcpy ((void *) new_environ, (void *) environ,
91                      size * sizeof (char *));
92       new_environ[size] = (char *) string;
93       new_environ[size + 1] = NULL;
94       if (last_environ != NULL)
95         free ((void *) last_environ);
96       last_environ = new_environ;
97       environ = new_environ;
98     }
99   else
100     *ep = (char *) string;
101
102   return 0;
103 }