1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2005-2023, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
38 /* We don't have libiberty, so use malloc. */
39 # define xmalloc(S) malloc (S)
45 #if defined (__MINGW32__)
49 #if defined (__APPLE__) \
50 && !(defined (__arm__) \
51 || defined (__arm64__) \
52 || defined (__IOS_SIMULATOR__))
53 /* On Darwin, _NSGetEnviron must be used for shared libraries; but it is not
54 available on iOS (on device or on simulator). */
55 #include <crt_externs.h>
58 #if defined (__vxworks)
63 /* On VxWorks 6 Real-Time process mode, environ is defined in unistd.h. */
65 #elif defined (VTHREADS)
66 /* VTHREADS mode applies to both VxWorks 653 and VxWorks MILS. The
67 inclusion of vThreadsData.h is necessary to workaround a bug with
68 envLib.h on VxWorks MILS and VxWorks 653. */
69 #include <vThreadsData.h>
82 #include <vms/descrip.h>
88 __gnat_getenv (char *name
, int *len
, char **value
)
90 *value
= getenv (name
);
94 *len
= strlen (*value
);
100 __gnat_setenv (char *name
, char *value
)
102 #if (defined (__vxworks) && (defined (__RTP__) || _WRS_VXWORKS_MAJOR >= 7)) \
103 || defined (__APPLE__)
104 setenv (name
, value
, 1);
107 size_t size
= strlen (name
) + strlen (value
) + 2;
110 expression
= (char *) xmalloc (size
* sizeof (char));
112 sprintf (expression
, "%s=%s", name
, value
);
114 #if defined (__MINGW32__) || defined (__vxworks)
115 /* putenv for Windows and VxWorks 6 kernel modules makes a copy of the
116 expression string, so we need to free it after the call to putenv. */
123 __gnat_environ (void)
125 #if defined (__MINGW32__)
127 #elif defined (__sun__)
128 extern char **_environ
;
130 #elif defined (__APPLE__) \
131 && !(defined (__arm__) \
132 || defined (__arm64__) \
133 || defined (__IOS_SIMULATOR__))
134 return *_NSGetEnviron ();
135 #elif ! (defined (__vxworks))
136 extern char **environ
;
139 #if defined (__RTP__) || defined (VTHREADS)
142 /* For VxWorks kernel modules use envGet to get the task's environment
143 (either the task's private environment if it has one or the global
144 environment otherwise). taskId parameter of 0 refers to the current
145 task (the VxWorks documentation says to use NULL but the compiler
146 complains that taskId is an int rather than a pointer. Internally,
147 VxWorks uses 0 as well). */
153 void __gnat_unsetenv (char *name
)
155 #if defined (__hpux__) || defined (__sun__) \
156 || (defined (__vxworks) && ! defined (__RTP__) \
157 && _WRS_VXWORKS_MAJOR <= 6) \
158 || defined (_AIX) || defined (__Lynx__)
160 /* On Solaris and HP-UX there is no function to clear an environment
161 variable. So we look for the variable in the environ table and delete it
162 by setting the entry to NULL. This can clearly cause some memory leaks
163 but free cannot be used on this context as not all strings in the environ
164 have been allocated using malloc. To avoid this memory leak another
165 method can be used. It consists in forcing the reallocation of all the
166 strings in the environ table using malloc on the first call on the
167 functions related to environment variable management. The disadvantage
168 is that if a program makes a direct call to getenv the return string
169 may be deallocated at some point. */
170 /* Note that on AIX, unsetenv is not supported on 5.1 but it is on 5.3.
171 As we are still supporting AIX 5.1 we cannot use unsetenv */
172 char **env
= __gnat_environ ();
174 size_t size
= strlen (name
);
176 while (env
[index
] != NULL
) {
177 if (strlen (env
[index
]) > size
) {
178 if (strstr (env
[index
], name
) == env
[index
] &&
179 env
[index
][size
] == '=') {
180 #if defined (__vxworks)
181 /* on Vxworks we are sure that the string has been allocated using
185 while (env
[index
] != NULL
) {
186 env
[index
]=env
[index
+ 1];
194 #elif defined (__MINGW32__)
195 /* On Windows platform putenv ("key=") is equivalent to unsetenv (a
196 subsequent call to getenv ("key") will return NULL and not the "\0"
198 size_t size
= strlen (name
) + 2;
200 expression
= (char *) xmalloc (size
* sizeof (char));
202 sprintf (expression
, "%s=", name
);
210 void __gnat_clearenv (void)
212 #if defined (__sun__) \
213 || (defined (__vxworks) && !defined (__RTP__) && _WRS_VXWORKS_MAJOR <= 6) \
214 || defined (__Lynx__) \
215 || defined (__PikeOS__)
216 /* On Solaris, VxWorks kernel pre 7, and Lynx there is no system
217 call to unset a variable or to clear the environment so set all
218 the entries in the environ table to NULL (see comment in
219 __gnat_unsetenv for more explanation). */
220 char **env
= __gnat_environ ();
223 while (env
[index
] != NULL
) {
227 #elif defined (__MINGW32__) || defined (__FreeBSD__) || defined (__APPLE__) \
228 || (defined (__vxworks) && defined (__RTP__) || _WRS_VXWORKS_MAJOR >= 7) \
229 || defined (__CYGWIN__) \
230 || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__rtems__) \
231 || defined (__DragonFly__) || defined (__DJGPP__)
232 /* On Windows, FreeBSD and MacOS there is no function to clean all the
233 environment but there is a "clean" way to unset a variable. So go
234 through the environ table and call __gnat_unsetenv on all entries */
235 char **env
= __gnat_environ ();
238 while (env
[0] != NULL
) {
240 while (env
[0][size
] != '=')
242 /* create a string that contains "name" */
246 expression
= (char *) xmalloc (size
* sizeof (char));
247 strncpy (expression
, env
[0], size
);
248 expression
[size
- 1] = 0;
249 __gnat_unsetenv (expression
);