Add __mmap64 and mmap64 aliases to mmap.
[glibc/history.git] / sysdeps / generic / setenv.c
blobd4c5c87b15fdfc984359323622e9a7fcb113620e
1 /* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <errno.h>
24 #if !_LIBC
25 # if !defined(errno) && !defined(HAVE_ERRNO_DECL)
26 extern int errno;
27 # endif
28 # define __set_errno(ev) ((errno) = (ev))
29 #endif
31 #if _LIBC || HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #if _LIBC || HAVE_STRING_H
35 # include <string.h>
36 #endif
37 #if _LIBC || HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
41 #if !_LIBC
42 # define __environ environ
43 # ifndef HAVE_ENVIRON_DECL
44 extern char **environ;
45 # endif
46 #endif
48 #if _LIBC
49 /* This lock protects against simultaneous modifications of `environ'. */
50 # include <bits/libc-lock.h>
51 __libc_lock_define_initialized (static, envlock)
52 # define LOCK __libc_lock_lock (envlock)
53 # define UNLOCK __libc_lock_unlock (envlock)
54 #else
55 # define LOCK
56 # define UNLOCK
57 #endif
59 /* In the GNU C library we must keep the namespace clean. */
60 #ifdef _LIBC
61 # define clearenv __clearenv
62 #endif
65 /* If this variable is not a null pointer we allocated the current
66 environment. */
67 static char **last_environ;
70 int
71 setenv (name, value, replace)
72 const char *name;
73 const char *value;
74 int replace;
76 register char **ep;
77 register size_t size;
78 const size_t namelen = strlen (name);
79 const size_t vallen = strlen (value) + 1;
81 LOCK;
83 size = 0;
84 if (__environ != NULL)
85 for (ep = __environ; *ep != NULL; ++ep)
86 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
87 break;
88 else
89 ++size;
91 if (__environ == NULL || *ep == NULL)
93 char **new_environ;
95 if (__environ == last_environ && __environ != NULL)
96 /* We allocated this space; we can extend it. */
97 new_environ = (char **) realloc (last_environ,
98 (size + 2) * sizeof (char *));
99 else
100 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
102 if (new_environ == NULL)
104 UNLOCK;
105 return -1;
108 new_environ[size] = malloc (namelen + 1 + vallen);
109 if (new_environ[size] == NULL)
111 free ((char *) new_environ);
112 __set_errno (ENOMEM);
113 UNLOCK;
114 return -1;
117 if (__environ != last_environ)
118 memcpy ((char *) new_environ, (char *) __environ,
119 size * sizeof (char *));
121 #ifdef _LIBC
123 char *tmp = __mempcpy (new_environ[size], name, namelen);
124 *tmp++ = '=';
125 __mempcpy (tmp, value, vallen);
127 #else
128 memcpy (new_environ[size], name, namelen);
129 new_environ[size][namelen] = '=';
130 memcpy (&new_environ[size][namelen + 1], value, vallen);
131 #endif
133 new_environ[size + 1] = NULL;
135 last_environ = __environ = new_environ;
137 else if (replace)
139 size_t len = strlen (*ep);
140 if (len + 1 < namelen + 1 + vallen)
142 /* The existing string is too short; malloc a new one. */
143 char *new = malloc (namelen + 1 + vallen);
144 if (new == NULL)
146 UNLOCK;
147 return -1;
149 *ep = new;
150 #ifdef _LIBC
151 *((char *) __mempcpy (*ep, name, namelen)) = '=';
152 #else
153 memcpy (*ep, name, namelen);
154 (*ep)[namelen] = '=';
155 #endif
157 memcpy (&(*ep)[namelen + 1], value, vallen);
160 UNLOCK;
162 return 0;
165 void
166 unsetenv (name)
167 const char *name;
169 const size_t len = strlen (name);
170 char **ep;
172 LOCK;
174 for (ep = __environ; *ep; ++ep)
175 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
177 /* Found it. Remove this pointer by moving later ones back. */
178 char **dp = ep;
180 dp[0] = dp[1];
181 while (*dp++);
182 /* Continue the loop in case NAME appears again. */
185 UNLOCK;
188 /* The `clearenv' was planned to be added to POSIX.1 but probably
189 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
190 for Fortran 77) requires this function. */
192 clearenv ()
194 LOCK;
196 if (__environ == last_environ && __environ != NULL)
198 /* We allocated this environment so we can free it. */
199 free (__environ);
200 last_environ = NULL;
203 /* Clear the environment pointer removes the whole environment. */
204 __environ = NULL;
206 UNLOCK;
208 return 0;
210 #ifdef _LIBC
211 # undef clearenv
212 weak_alias (__clearenv, clearenv)
213 #endif