Move source files to src and include folder
[tig.git] / compat / setenv.c
blobf5b6543a052b3f0e1fa944ef62982e20c9b14233
1 /* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011 Free Software Foundation,
2 Inc.
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.
37 @end deftypefn
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #include "compat.h"
47 #include <sys/types.h> /* For `size_t' */
48 #include <stdio.h> /* For `NULL' */
50 #include <errno.h>
51 #if !defined(errno) && !defined(HAVE_DECL_ERRNO)
52 extern int errno;
53 #endif
54 #define __set_errno(ev) ((errno) = (ev))
56 #ifdef HAVE_STDLIB_H
57 # include <stdlib.h>
58 #endif
59 #ifdef HAVE_STRING_H
60 # include <string.h>
61 #endif
62 #ifdef HAVE_UNISTD_H
63 # include <unistd.h>
64 #endif
66 #define __environ environ
67 #ifndef HAVE_DECL_ENVIRON
68 extern char **environ;
69 #endif
71 /* LOCK and UNLOCK are defined as no-ops. This makes the tig
72 * implementation MT-Unsafe. */
73 #define LOCK
74 #define UNLOCK
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
79 environment. */
80 static char **last_environ;
83 int
84 compat_setenv (const char *name, const char *value, int replace)
86 register char **ep = 0;
87 register size_t size;
88 const size_t namelen = strlen (name);
89 const size_t vallen = strlen (value) + 1;
91 LOCK;
93 size = 0;
94 if (__environ != NULL)
96 for (ep = __environ; *ep != NULL; ++ep)
97 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
98 break;
99 else
100 ++size;
103 if (__environ == NULL || *ep == NULL)
105 char **new_environ;
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 *));
110 else
111 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
113 if (new_environ == NULL)
115 UNLOCK;
116 return -1;
119 new_environ[size] = (char *) malloc (namelen + 1 + vallen);
120 if (new_environ[size] == NULL)
122 free ((char *) new_environ);
123 __set_errno (ENOMEM);
124 UNLOCK;
125 return -1;
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;
140 else if (replace)
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)
149 UNLOCK;
150 return -1;
152 *ep = new_string;
154 memcpy (*ep, name, namelen);
155 (*ep)[namelen] = '=';
156 memcpy (&(*ep)[namelen + 1], value, vallen);
159 UNLOCK;
161 return 0;
164 #if 0
165 void
166 compat_unsetenv (const char *name)
168 const size_t len = strlen (name);
169 char **ep;
171 LOCK;
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. */
177 char **dp = ep;
179 dp[0] = dp[1];
180 while (*dp++);
181 /* Continue the loop in case NAME appears again. */
184 UNLOCK;
186 #endif