1 /* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011 Free Software Foundation,
3 This file based on setenv.c in the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
21 * This file was borrowed on 2013-08-08 from:
22 * https://raw.github.com/mirrors/gcc/master/libiberty/setenv.c
27 @deftypefn Supplemental int setenv (const char *@var{name}, @
28 const char *@var{value}, int @var{overwrite})
29 @deftypefnx Supplemental void unsetenv (const char *@var{name})
31 @code{setenv} adds @var{name} to the environment with value
32 @var{value}. If the name was already present in the environment,
33 the new value will be stored only if @var{overwrite} is nonzero.
34 The companion @code{unsetenv} function removes @var{name} from the
35 environment. This implementation is not safe for multithreaded code.
47 #include <sys/types.h> /* For `size_t' */
48 #include <stdio.h> /* For `NULL' */
51 #if !defined(errno) && !defined(HAVE_DECL_ERRNO)
54 #define __set_errno(ev) ((errno) = (ev))
66 #define __environ environ
67 #ifndef HAVE_DECL_ENVIRON
68 extern char **environ
;
71 /* LOCK and UNLOCK are defined as no-ops. This makes the tig
72 * implementation MT-Unsafe. */
76 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
78 /* If this variable is not a null pointer we allocated the current
80 static char **last_environ
;
84 compat_setenv (const char *name
, const char *value
, int replace
)
86 register char **ep
= 0;
88 const size_t namelen
= strlen (name
);
89 const size_t vallen
= strlen (value
) + 1;
94 if (__environ
!= NULL
)
96 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
97 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
103 if (__environ
== NULL
|| *ep
== NULL
)
106 if (__environ
== last_environ
&& __environ
!= NULL
)
107 /* We allocated this space; we can extend it. */
108 new_environ
= (char **) realloc (last_environ
,
109 (size
+ 2) * sizeof (char *));
111 new_environ
= (char **) malloc ((size
+ 2) * sizeof (char *));
113 if (new_environ
== NULL
)
119 new_environ
[size
] = (char *) malloc (namelen
+ 1 + vallen
);
120 if (new_environ
[size
] == NULL
)
122 free ((char *) new_environ
);
123 __set_errno (ENOMEM
);
128 if (__environ
!= last_environ
)
129 memcpy ((char *) new_environ
, (char *) __environ
,
130 size
* sizeof (char *));
132 memcpy (new_environ
[size
], name
, namelen
);
133 new_environ
[size
][namelen
] = '=';
134 memcpy (&new_environ
[size
][namelen
+ 1], value
, vallen
);
136 new_environ
[size
+ 1] = NULL
;
138 last_environ
= __environ
= new_environ
;
142 size_t len
= strlen (*ep
);
143 if (len
+ 1 < namelen
+ 1 + vallen
)
145 /* The existing string is too short; malloc a new one. */
146 char *new_string
= (char *) malloc (namelen
+ 1 + vallen
);
147 if (new_string
== NULL
)
154 memcpy (*ep
, name
, namelen
);
155 (*ep
)[namelen
] = '=';
156 memcpy (&(*ep
)[namelen
+ 1], value
, vallen
);
166 compat_unsetenv (const char *name
)
168 const size_t len
= strlen (name
);
173 for (ep
= __environ
; *ep
; ++ep
)
174 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
176 /* Found it. Remove this pointer by moving later ones back. */
181 /* Continue the loop in case NAME appears again. */