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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 #ifdef HAVE_CRT_EXTERNS_H
37 #include <crt_externs.h> /* for _NSGetEnviron */
43 #include "glib-private.h"
45 #include "gmessages.h"
46 #include "gstrfuncs.h"
51 /* Environ array functions {{{1 */
53 g_environ_find (gchar **envp,
54 const gchar *variable)
61 len = strlen (variable);
63 for (i = 0; envp[i]; i++)
65 if (strncmp (envp[i], variable, len) == 0 &&
75 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
76 * list (eg, as returned from g_get_environ()), or %NULL
77 * for an empty environment list
78 * @variable: the environment variable to get, in the GLib file name
81 * Returns the value of the environment variable @variable in the
82 * provided list @envp.
84 * The name and value are in the GLib file name encoding.
85 * On UNIX, this means the actual bytes which might or might not
86 * be in some consistent character set and encoding. On Windows,
87 * it is in UTF-8. On Windows, in case the environment variable's
88 * value contains references to other environment variables, they
91 * Return value: 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: (allow-none) (array zero-terminated=1) (transfer full): an environment
116 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
117 * for an empty environment list
118 * @variable: the environment variable to set, must not contain '='
119 * @value: the value for to set the variable to
120 * @overwrite: whether to change the variable if it already exists
122 * Sets the environment variable @variable in the provided list
125 * Both the variable's name and value should be in the GLib
126 * file name encoding. On UNIX, this means that they can be
127 * arbitrary byte strings. On Windows, they should be in UTF-8.
129 * Return value: (array zero-terminated=1) (transfer full): the
130 * updated environment list. Free it using g_strfreev().
135 g_environ_setenv (gchar **envp,
136 const gchar *variable,
142 g_return_val_if_fail (variable != NULL, NULL);
143 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
145 index = g_environ_find (envp, variable);
150 g_free (envp[index]);
151 envp[index] = g_strdup_printf ("%s=%s", variable, value);
158 length = envp ? g_strv_length (envp) : 0;
159 envp = g_renew (gchar *, envp, length + 2);
160 envp[length] = g_strdup_printf ("%s=%s", variable, value);
161 envp[length + 1] = NULL;
168 g_environ_unsetenv_internal (gchar **envp,
169 const gchar *variable,
175 len = strlen (variable);
177 /* Note that we remove *all* environment entries for
178 * the variable name, not just the first.
183 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
203 * g_environ_unsetenv:
204 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
205 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
206 * or %NULL for an empty environment list
207 * @variable: the environment variable to remove, must not contain '='
209 * Removes the environment variable @variable from the provided
212 * Return value: (array zero-terminated=1) (transfer full): the
213 * 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 implemention {{{1 */
235 * @variable: the environment variable to get, in the GLib file name
238 * Returns the value of an environment variable.
240 * The name and value are in the GLib file name encoding. On UNIX,
241 * this means the actual bytes which might or might not be in some
242 * consistent character set and encoding. On Windows, it is in UTF-8.
243 * On Windows, in case the environment variable's value contains
244 * references to other environment variables, they are expanded.
246 * Return value: the value of the environment variable, or %NULL if
247 * the environment variable is not found. The returned string
248 * may be overwritten by the next call to g_getenv(), g_setenv()
252 g_getenv (const gchar *variable)
254 g_return_val_if_fail (variable != NULL, NULL);
256 return getenv (variable);
261 * @variable: the environment variable to set, must not contain '='.
262 * @value: the value for to set the variable to.
263 * @overwrite: whether to change the variable if it already exists.
265 * Sets an environment variable. Both the variable's name and value
266 * should be in the GLib file name encoding. On UNIX, this means that
267 * they can be arbitrary byte strings. On Windows, they should be in
270 * Note that on some systems, when variables are overwritten, the memory
271 * used for the previous variables and its value isn't reclaimed.
274 * Environment variable handling in UNIX is not thread-safe, and your
275 * program may crash if one thread calls g_setenv() while another
276 * thread is calling getenv(). (And note that many functions, such as
277 * gettext(), call getenv() internally.) This function is only safe to
278 * use at the very start of your program, before creating any other
279 * threads (or creating objects that create worker threads of their
282 * If you need to set up the environment for a child process, you can
283 * use g_get_environ() to get an environment array, modify that with
284 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
285 * array directly to execvpe(), g_spawn_async(), or the like.
288 * Returns: %FALSE if the environment variable couldn't be set.
293 g_setenv (const gchar *variable,
302 g_return_val_if_fail (variable != NULL, FALSE);
303 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
306 result = setenv (variable, value, overwrite);
308 if (!overwrite && getenv (variable) != NULL)
311 /* This results in a leak when you overwrite existing
312 * settings. It would be fairly easy to fix this by keeping
313 * our own parallel array or hash table.
315 string = g_strconcat (variable, "=", value, NULL);
316 result = putenv (string);
321 #ifdef HAVE__NSGETENVIRON
322 #define environ (*_NSGetEnviron())
324 /* According to the Single Unix Specification, environ is not
325 * in any system header, although unistd.h often declares it.
327 extern char **environ;
332 * @variable: the environment variable to remove, must not contain '='
334 * Removes an environment variable from the environment.
336 * Note that on some systems, when variables are overwritten, the
337 * memory used for the previous variables and its value isn't reclaimed.
340 * Environment variable handling in UNIX is not thread-safe, and your
341 * program may crash if one thread calls g_unsetenv() while another
342 * thread is calling getenv(). (And note that many functions, such as
343 * gettext(), call getenv() internally.) This function is only safe
344 * to use at the very start of your program, before creating any other
345 * threads (or creating objects that create worker threads of their
348 * If you need to set up the environment for a child process, you can
349 * use g_get_environ() to get an environment array, modify that with
350 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
351 * array directly to execvpe(), g_spawn_async(), or the like.
357 g_unsetenv (const gchar *variable)
360 g_return_if_fail (variable != NULL);
361 g_return_if_fail (strchr (variable, '=') == NULL);
364 #else /* !HAVE_UNSETENV */
365 g_return_if_fail (variable != NULL);
366 g_return_if_fail (strchr (variable, '=') == NULL);
368 /* Mess directly with the environ array.
369 * This seems to be the only portable way to do this.
371 g_environ_unsetenv_internal (environ, variable, FALSE);
372 #endif /* !HAVE_UNSETENV */
378 * Gets the names of all variables set in the environment.
380 * Programs that want to be portable to Windows should typically use
381 * this function and g_getenv() instead of using the environ array
382 * from the C library directly. On Windows, the strings in the environ
383 * array are in system codepage encoding, while in most of the typical
384 * use cases for environment variables in GLib-using programs you want
385 * the UTF-8 encoding that this function and g_getenv() provide.
387 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
388 * list of strings which must be freed with g_strfreev().
398 len = g_strv_length (environ);
399 result = g_new0 (gchar *, len + 1);
402 for (i = 0; i < len; i++)
404 eq = strchr (environ[i], '=');
406 result[j++] = g_strndup (environ[i], eq - environ[i]);
417 * Gets the list of environment variables for the current process.
419 * The list is %NULL terminated and each item in the list is of the
422 * This is equivalent to direct access to the 'environ' global variable,
425 * The return value is freshly allocated and it should be freed with
426 * g_strfreev() when it is no longer needed.
428 * Returns: (array zero-terminated=1) (transfer full): the list of
429 * environment variables
436 return g_strdupv (environ);
439 /* Win32 implementation {{{1 */
440 #else /* G_OS_WIN32 */
443 g_getenv (const gchar *variable)
447 wchar_t dummy[2], *wname, *wvalue;
450 g_return_val_if_fail (variable != NULL, NULL);
451 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
453 /* On Windows NT, it is relatively typical that environment
454 * variables contain references to other environment variables. If
455 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
456 * environment variables would be stored in the Registry as
457 * REG_EXPAND_SZ type values, and would then get automatically
458 * expanded before a program sees them. But there is broken software
459 * that stores environment variables as REG_SZ values even if they
460 * contain references to other environment variables.)
463 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
465 len = GetEnvironmentVariableW (wname, dummy, 2);
470 if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
473 quark = g_quark_from_static_string ("");
474 return g_quark_to_string (quark);
479 wvalue = g_new (wchar_t, len);
481 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
488 if (wcschr (wvalue, L'%') != NULL)
490 wchar_t *tem = wvalue;
492 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
496 wvalue = g_new (wchar_t, len);
498 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
508 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
513 quark = g_quark_from_string (value);
516 return g_quark_to_string (quark);
520 g_setenv (const gchar *variable,
525 wchar_t *wname, *wvalue, *wassignment;
528 g_return_val_if_fail (variable != NULL, FALSE);
529 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
530 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
531 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
533 if (!overwrite && g_getenv (variable) != NULL)
536 /* We want to (if possible) set both the environment variable copy
537 * kept by the C runtime and the one kept by the system.
539 * We can't use only the C runtime's putenv or _wputenv() as that
540 * won't work for arbitrary Unicode strings in a "non-Unicode" app
541 * (with main() and not wmain()). In a "main()" app the C runtime
542 * initializes the C runtime's environment table by converting the
543 * real (wide char) environment variables to system codepage, thus
544 * breaking those that aren't representable in the system codepage.
546 * As the C runtime's putenv() will also set the system copy, we do
547 * the putenv() first, then call SetEnvironmentValueW ourselves.
550 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
551 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
552 tem = g_strconcat (variable, "=", value, NULL);
553 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
556 _wputenv (wassignment);
557 g_free (wassignment);
559 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
568 g_unsetenv (const gchar *variable)
570 wchar_t *wname, *wassignment;
573 g_return_if_fail (variable != NULL);
574 g_return_if_fail (strchr (variable, '=') == NULL);
575 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
577 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
578 tem = g_strconcat (variable, "=", NULL);
579 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
582 _wputenv (wassignment);
583 g_free (wassignment);
585 SetEnvironmentVariableW (wname, NULL);
597 p = (wchar_t *) GetEnvironmentStringsW ();
607 result = g_new0 (gchar *, len + 1);
613 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
614 if (result[j] != NULL)
616 eq = strchr (result[j], '=');
617 if (eq && eq > result[j])
628 FreeEnvironmentStringsW (p);
640 strings = GetEnvironmentStringsW ();
641 for (n = 0, i = 0; strings[n]; i++)
642 n += wcslen (strings + n) + 1;
644 result = g_new (char *, i + 1);
645 for (n = 0, i = 0; strings[n]; i++)
647 result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
648 n += wcslen (strings + n) + 1;
650 FreeEnvironmentStringsW (strings);
656 /* Win32 binary compatibility versions {{{1 */
662 g_getenv (const gchar *variable)
664 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
665 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
669 g_free (utf8_variable);
672 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
673 quark = g_quark_from_string (value);
676 return g_quark_to_string (quark);
682 g_setenv (const gchar *variable,
686 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
687 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
688 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
690 g_free (utf8_variable);
699 g_unsetenv (const gchar *variable)
701 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
703 g_unsetenv_utf8 (utf8_variable);
705 g_free (utf8_variable);
710 #endif /* G_OS_WIN32 */
713 /* vim: set foldmethod=marker: */