* lisp/emacs-lisp/lisp-mnt.el (lm-section-end): Always end before the
[emacs.git] / lib / putenv.c
blob3c0f7ead3303bf0bd14ef334bcdd039f0647dc5c
1 /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2012 Free Software
2 Foundation, Inc.
4 NOTE: The canonical source of this file is maintained with the GNU C
5 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
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 3 of the License, or any
10 later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 /* Specification. */
23 #include <stdlib.h>
25 #include <stddef.h>
27 /* Include errno.h *after* sys/types.h to work around header problems
28 on AIX 3.2.5. */
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(ev) ((errno) = (ev))
32 #endif
34 #include <string.h>
35 #include <unistd.h>
37 #if _LIBC
38 # if HAVE_GNU_LD
39 # define environ __environ
40 # else
41 extern char **environ;
42 # endif
43 #endif
45 #if _LIBC
46 /* This lock protects against simultaneous modifications of 'environ'. */
47 # include <bits/libc-lock.h>
48 __libc_lock_define_initialized (static, envlock)
49 # define LOCK __libc_lock_lock (envlock)
50 # define UNLOCK __libc_lock_unlock (envlock)
51 #else
52 # define LOCK
53 # define UNLOCK
54 #endif
56 static int
57 _unsetenv (const char *name)
59 size_t len;
60 char **ep;
62 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
64 __set_errno (EINVAL);
65 return -1;
68 len = strlen (name);
70 LOCK;
72 ep = environ;
73 while (*ep != NULL)
74 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
76 /* Found it. Remove this pointer by moving later ones back. */
77 char **dp = ep;
80 dp[0] = dp[1];
81 while (*dp++);
82 /* Continue the loop in case NAME appears again. */
84 else
85 ++ep;
87 UNLOCK;
89 return 0;
93 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
94 If STRING contains no '=', then remove STRING from the environment. */
95 int
96 putenv (char *string)
98 const char *const name_end = strchr (string, '=');
99 register size_t size;
100 register char **ep;
102 if (name_end == NULL)
104 /* Remove the variable from the environment. */
105 return _unsetenv (string);
108 size = 0;
109 for (ep = environ; *ep != NULL; ++ep)
110 if (!strncmp (*ep, string, name_end - string) &&
111 (*ep)[name_end - string] == '=')
112 break;
113 else
114 ++size;
116 if (*ep == NULL)
118 static char **last_environ = NULL;
119 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
120 if (new_environ == NULL)
121 return -1;
122 (void) memcpy ((void *) new_environ, (void *) environ,
123 size * sizeof (char *));
124 new_environ[size] = (char *) string;
125 new_environ[size + 1] = NULL;
126 free (last_environ);
127 last_environ = new_environ;
128 environ = new_environ;
130 else
131 *ep = string;
133 return 0;