1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h> /* for _NSGetEnviron */
38 #include "glib-private.h"
40 #include "gmessages.h"
41 #include "gstrfuncs.h"
45 #include "gthreadprivate.h"
47 /* Environ array functions {{{1 */
49 g_environ_matches (const gchar *env, const gchar *variable, gsize len)
52 /* TODO handle Unicode environment variable names */
53 /* Like filesystem paths, environment variables are case-insensitive. */
54 return g_ascii_strncasecmp (env, variable, len) == 0 && env[len] == '=';
56 return strncmp (env, variable, len) == 0 && env[len] == '=';
61 g_environ_find (gchar **envp,
62 const gchar *variable)
70 len = strlen (variable);
72 for (i = 0; envp[i]; i++)
74 if (g_environ_matches (envp[i], variable, len))
83 * @envp: (nullable) (array zero-terminated=1) (transfer none) (element-type filename):
84 * an environment list (eg, as returned from g_get_environ()), or %NULL
85 * for an empty environment list
86 * @variable: (type filename): the environment variable to get
88 * Returns the value of the environment variable @variable in the
89 * provided list @envp.
91 * Returns: (type filename): the value of the environment variable, or %NULL if
92 * the environment variable is not set in @envp. The returned
93 * string is owned by @envp, and will be freed if @variable is
99 g_environ_getenv (gchar **envp,
100 const gchar *variable)
104 g_return_val_if_fail (variable != NULL, NULL);
106 index = g_environ_find (envp, variable);
108 return envp[index] + strlen (variable) + 1;
115 * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
116 * an environment list that can be freed using g_strfreev() (e.g., as
117 * returned from g_get_environ()), or %NULL for an empty
119 * @variable: (type filename): the environment variable to set, must not
121 * @value: (type filename): the value for to set the variable to
122 * @overwrite: whether to change the variable if it already exists
124 * Sets the environment variable @variable in the provided list
127 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
128 * the updated environment list. Free it using g_strfreev().
133 g_environ_setenv (gchar **envp,
134 const gchar *variable,
140 g_return_val_if_fail (variable != NULL, NULL);
141 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
142 g_return_val_if_fail (value != NULL, NULL);
144 index = g_environ_find (envp, variable);
149 g_free (envp[index]);
150 envp[index] = g_strdup_printf ("%s=%s", variable, value);
157 length = envp ? g_strv_length (envp) : 0;
158 envp = g_renew (gchar *, envp, length + 2);
159 envp[length] = g_strdup_printf ("%s=%s", variable, value);
160 envp[length + 1] = NULL;
167 g_environ_unsetenv_internal (gchar **envp,
168 const gchar *variable,
174 len = strlen (variable);
176 /* Note that we remove *all* environment entries for
177 * the variable name, not just the first.
182 if (!g_environ_matches (*e, variable, len))
202 * g_environ_unsetenv:
203 * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
204 * an environment list that can be freed using g_strfreev() (e.g., as
205 * returned from g_get_environ()), or %NULL for an empty environment list
206 * @variable: (type filename): the environment variable to remove, must not
209 * Removes the environment variable @variable from the provided
212 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
213 * the updated environment list. Free it using g_strfreev().
218 g_environ_unsetenv (gchar **envp,
219 const gchar *variable)
221 g_return_val_if_fail (variable != NULL, NULL);
222 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
227 return g_environ_unsetenv_internal (envp, variable, TRUE);
230 /* UNIX implementation {{{1 */
235 * @variable: (type filename): the environment variable to get
237 * Returns the value of an environment variable.
239 * On UNIX, the name and value are byte strings which might or might not
240 * be in some consistent character set and encoding. On Windows, they are
242 * On Windows, in case the environment variable's value contains
243 * references to other environment variables, they are expanded.
245 * Returns: (type filename): the value of the environment variable, or %NULL if
246 * the environment variable is not found. The returned string
247 * may be overwritten by the next call to g_getenv(), g_setenv()
251 g_getenv (const gchar *variable)
253 g_return_val_if_fail (variable != NULL, NULL);
255 return getenv (variable);
260 * @variable: (type filename): the environment variable to set, must not
262 * @value: (type filename): the value for to set the variable to.
263 * @overwrite: whether to change the variable if it already exists.
265 * Sets an environment variable. On UNIX, both the variable's name and
266 * value can be arbitrary byte strings, except that the variable's name
267 * cannot contain '='. On Windows, they should be in UTF-8.
269 * Note that on some systems, when variables are overwritten, the memory
270 * used for the previous variables and its value isn't reclaimed.
272 * You should be mindful of the fact that environment variable handling
273 * in UNIX is not thread-safe, and your program may crash if one thread
274 * calls g_setenv() while another thread is calling getenv(). (And note
275 * that many functions, such as gettext(), call getenv() internally.)
276 * This function is only safe to use at the very start of your program,
277 * before creating any other threads (or creating objects that create
278 * worker threads of their own).
280 * If you need to set up the environment for a child process, you can
281 * use g_get_environ() to get an environment array, modify that with
282 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
283 * array directly to execvpe(), g_spawn_async(), or the like.
285 * Returns: %FALSE if the environment variable couldn't be set.
290 g_setenv (const gchar *variable,
299 g_return_val_if_fail (variable != NULL, FALSE);
300 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
301 g_return_val_if_fail (value != NULL, FALSE);
303 #ifndef G_DISABLE_CHECKS
304 /* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
305 * See https://gitlab.gnome.org/GNOME/glib/issues/715 */
306 if (g_thread_n_created () > 0)
307 g_debug ("setenv()/putenv() are not thread-safe and should not be used after threads are created");
311 result = setenv (variable, value, overwrite);
313 if (!overwrite && getenv (variable) != NULL)
316 /* This results in a leak when you overwrite existing
317 * settings. It would be fairly easy to fix this by keeping
318 * our own parallel array or hash table.
320 string = g_strconcat (variable, "=", value, NULL);
321 result = putenv (string);
326 #ifdef HAVE__NSGETENVIRON
327 #define environ (*_NSGetEnviron())
329 /* According to the Single Unix Specification, environ is not
330 * in any system header, although unistd.h often declares it.
332 extern char **environ;
337 * @variable: (type filename): the environment variable to remove, must
340 * Removes an environment variable from the environment.
342 * Note that on some systems, when variables are overwritten, the
343 * memory used for the previous variables and its value isn't reclaimed.
345 * You should be mindful of the fact that environment variable handling
346 * in UNIX is not thread-safe, and your program may crash if one thread
347 * calls g_unsetenv() while another thread is calling getenv(). (And note
348 * that many functions, such as gettext(), call getenv() internally.) This
349 * function is only safe to use at the very start of your program, before
350 * creating any other threads (or creating objects that create worker
351 * threads of their own).
353 * If you need to set up the environment for a child process, you can
354 * use g_get_environ() to get an environment array, modify that with
355 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
356 * array directly to execvpe(), g_spawn_async(), or the like.
361 g_unsetenv (const gchar *variable)
363 g_return_if_fail (variable != NULL);
364 g_return_if_fail (strchr (variable, '=') == NULL);
366 #ifndef G_DISABLE_CHECKS
367 /* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
368 * See https://gitlab.gnome.org/GNOME/glib/issues/715 */
369 if (g_thread_n_created () > 0)
370 g_debug ("unsetenv() is not thread-safe and should not be used after threads are created");
375 #else /* !HAVE_UNSETENV */
376 /* Mess directly with the environ array.
377 * This seems to be the only portable way to do this.
379 g_environ_unsetenv_internal (environ, variable, FALSE);
380 #endif /* !HAVE_UNSETENV */
386 * Gets the names of all variables set in the environment.
388 * Programs that want to be portable to Windows should typically use
389 * this function and g_getenv() instead of using the environ array
390 * from the C library directly. On Windows, the strings in the environ
391 * array are in system codepage encoding, while in most of the typical
392 * use cases for environment variables in GLib-using programs you want
393 * the UTF-8 encoding that this function and g_getenv() provide.
395 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
396 * a %NULL-terminated list of strings which must be freed with
407 len = g_strv_length (environ);
408 result = g_new0 (gchar *, len + 1);
411 for (i = 0; i < len; i++)
413 eq = strchr (environ[i], '=');
415 result[j++] = g_strndup (environ[i], eq - environ[i]);
426 * Gets the list of environment variables for the current process.
428 * The list is %NULL terminated and each item in the list is of the
431 * This is equivalent to direct access to the 'environ' global variable,
434 * The return value is freshly allocated and it should be freed with
435 * g_strfreev() when it is no longer needed.
437 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
438 * the list of environment variables
445 return g_strdupv (environ);
448 /* Win32 implementation {{{1 */
449 #else /* G_OS_WIN32 */
452 g_getenv (const gchar *variable)
456 wchar_t dummy[2], *wname, *wvalue;
459 g_return_val_if_fail (variable != NULL, NULL);
460 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
462 /* On Windows NT, it is relatively typical that environment
463 * variables contain references to other environment variables. If
464 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
465 * environment variables would be stored in the Registry as
466 * REG_EXPAND_SZ type values, and would then get automatically
467 * expanded before a program sees them. But there is broken software
468 * that stores environment variables as REG_SZ values even if they
469 * contain references to other environment variables.)
472 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
474 len = GetEnvironmentVariableW (wname, dummy, 2);
479 if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
482 quark = g_quark_from_static_string ("");
483 return g_quark_to_string (quark);
488 wvalue = g_new (wchar_t, len);
490 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
497 if (wcschr (wvalue, L'%') != NULL)
499 wchar_t *tem = wvalue;
501 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
505 wvalue = g_new (wchar_t, len);
507 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
517 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
522 quark = g_quark_from_string (value);
525 return g_quark_to_string (quark);
529 g_setenv (const gchar *variable,
534 wchar_t *wname, *wvalue, *wassignment;
537 g_return_val_if_fail (variable != NULL, FALSE);
538 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
539 g_return_val_if_fail (value != NULL, FALSE);
540 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
541 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
543 if (!overwrite && g_getenv (variable) != NULL)
546 /* We want to (if possible) set both the environment variable copy
547 * kept by the C runtime and the one kept by the system.
549 * We can't use only the C runtime's putenv or _wputenv() as that
550 * won't work for arbitrary Unicode strings in a "non-Unicode" app
551 * (with main() and not wmain()). In a "main()" app the C runtime
552 * initializes the C runtime's environment table by converting the
553 * real (wide char) environment variables to system codepage, thus
554 * breaking those that aren't representable in the system codepage.
556 * As the C runtime's putenv() will also set the system copy, we do
557 * the putenv() first, then call SetEnvironmentValueW ourselves.
560 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
561 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
562 tem = g_strconcat (variable, "=", value, NULL);
563 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
566 _wputenv (wassignment);
567 g_free (wassignment);
569 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
578 g_unsetenv (const gchar *variable)
580 wchar_t *wname, *wassignment;
583 g_return_if_fail (variable != NULL);
584 g_return_if_fail (strchr (variable, '=') == NULL);
585 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
587 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
588 tem = g_strconcat (variable, "=", NULL);
589 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
592 _wputenv (wassignment);
593 g_free (wassignment);
595 SetEnvironmentVariableW (wname, NULL);
607 p = (wchar_t *) GetEnvironmentStringsW ();
617 result = g_new0 (gchar *, len + 1);
623 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
624 if (result[j] != NULL)
626 eq = strchr (result[j], '=');
627 if (eq && eq > result[j])
638 FreeEnvironmentStringsW (p);
650 strings = GetEnvironmentStringsW ();
651 for (n = 0, i = 0; strings[n]; i++)
652 n += wcslen (strings + n) + 1;
654 result = g_new (char *, i + 1);
655 for (n = 0, i = 0; strings[n]; i++)
657 result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
658 n += wcslen (strings + n) + 1;
660 FreeEnvironmentStringsW (strings);
666 #endif /* G_OS_WIN32 */
670 /* Binary compatibility versions. Not for newly compiled code. */
672 _GLIB_EXTERN const gchar *g_getenv_utf8 (const gchar *variable);
673 _GLIB_EXTERN gboolean g_setenv_utf8 (const gchar *variable,
676 _GLIB_EXTERN void g_unsetenv_utf8 (const gchar *variable);
679 g_getenv_utf8 (const gchar *variable)
681 return g_getenv (variable);
685 g_setenv_utf8 (const gchar *variable,
689 return g_setenv (variable, value, overwrite);
693 g_unsetenv_utf8 (const gchar *variable)
695 g_unsetenv (variable);
701 /* vim: set foldmethod=marker: */