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 */
44 #include "gmessages.h"
45 #include "gstrfuncs.h"
50 /* Environ array functions {{{1 */
52 g_environ_find (gchar **envp,
53 const gchar *variable)
57 len = strlen (variable);
59 for (i = 0; envp[i]; i++)
61 if (strncmp (envp[i], variable, len) == 0 &&
71 * @envp: (array zero-terminated=1) (transfer none): an environment
72 * list (eg, as returned from g_get_environ())
73 * @variable: the environment variable to get, in the GLib file name
76 * Returns the value of the environment variable @variable in the
77 * provided list @envp.
79 * The name and value are in the GLib file name encoding.
80 * On UNIX, this means the actual bytes which might or might not
81 * be in some consistent character set and encoding. On Windows,
82 * it is in UTF-8. On Windows, in case the environment variable's
83 * value contains references to other environment variables, they
86 * Return value: the value of the environment variable, or %NULL if
87 * the environment variable is not set in @envp. The returned
88 * string is owned by @envp, and will be freed if @variable is
94 g_environ_getenv (gchar **envp,
95 const gchar *variable)
99 g_return_val_if_fail (envp != NULL, NULL);
100 g_return_val_if_fail (variable != NULL, NULL);
102 index = g_environ_find (envp, variable);
104 return envp[index] + strlen (variable) + 1;
111 * @envp: (array zero-terminated=1) (transfer full): an environment
112 * list (eg, as returned from g_get_environ())
113 * @variable: the environment variable to set, must not contain '='
114 * @value: the value for to set the variable to
115 * @overwrite: whether to change the variable if it already exists
117 * Sets the environment variable @variable in the provided list
120 * Both the variable's name and value should be in the GLib
121 * file name encoding. On UNIX, this means that they can be
122 * arbitrary byte strings. On Windows, they should be in UTF-8.
124 * Return value: (array zero-terminated=1) (transfer full): the
125 * updated environment
130 g_environ_setenv (gchar **envp,
131 const gchar *variable,
137 g_return_val_if_fail (envp != NULL, NULL);
138 g_return_val_if_fail (variable != NULL, NULL);
139 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
141 index = g_environ_find (envp, variable);
146 g_free (envp[index]);
147 envp[index] = g_strdup_printf ("%s=%s", variable, value);
154 length = g_strv_length (envp);
155 envp = g_renew (gchar *, envp, length + 2);
156 envp[length] = g_strdup_printf ("%s=%s", variable, value);
157 envp[length + 1] = NULL;
164 * g_environ_unsetenv:
165 * @envp: (array zero-terminated=1) (transfer full): an environment
166 * list (eg, as returned from g_get_environ())
167 * @variable: the environment variable to remove, must not contain '='
169 * Removes the environment variable @variable from the provided
172 * Return value: (array zero-terminated=1) (transfer full): the
173 * updated environment
178 g_environ_unsetenv (gchar **envp,
179 const gchar *variable)
184 g_return_val_if_fail (envp != NULL, NULL);
185 g_return_val_if_fail (variable != NULL, NULL);
186 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
188 len = strlen (variable);
190 /* Note that we remove *all* environment entries for
191 * the variable name, not just the first.
196 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
211 /* UNIX implemention {{{1 */
216 * @variable: the environment variable to get, in the GLib file name
219 * Returns the value of an environment variable.
221 * The name and value are in the GLib file name encoding. On UNIX,
222 * this means the actual bytes which might or might not be in some
223 * consistent character set and encoding. On Windows, it is in UTF-8.
224 * On Windows, in case the environment variable's value contains
225 * references to other environment variables, they are expanded.
227 * Return value: the value of the environment variable, or %NULL if
228 * the environment variable is not found. The returned string
229 * may be overwritten by the next call to g_getenv(), g_setenv()
233 g_getenv (const gchar *variable)
235 g_return_val_if_fail (variable != NULL, NULL);
237 return getenv (variable);
242 * @variable: the environment variable to set, must not contain '='.
243 * @value: the value for to set the variable to.
244 * @overwrite: whether to change the variable if it already exists.
246 * Sets an environment variable. Both the variable's name and value
247 * should be in the GLib file name encoding. On UNIX, this means that
248 * they can be arbitrary byte strings. On Windows, they should be in
251 * Note that on some systems, when variables are overwritten, the memory
252 * used for the previous variables and its value isn't reclaimed.
255 * Environment variable handling in UNIX is not thread-safe, and your
256 * program may crash if one thread calls g_setenv() while another
257 * thread is calling getenv(). (And note that many functions, such as
258 * gettext(), call getenv() internally.) This function is only safe to
259 * use at the very start of your program, before creating any other
260 * threads (or creating objects that create worker threads of their
263 * If you need to set up the environment for a child process, you can
264 * use g_get_environ() to get an environment array, modify that with
265 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
266 * array directly to execvpe(), g_spawn_async(), or the like.
269 * Returns: %FALSE if the environment variable couldn't be set.
274 g_setenv (const gchar *variable,
283 g_return_val_if_fail (variable != NULL, FALSE);
284 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
287 result = setenv (variable, value, overwrite);
289 if (!overwrite && getenv (variable) != NULL)
292 /* This results in a leak when you overwrite existing
293 * settings. It would be fairly easy to fix this by keeping
294 * our own parallel array or hash table.
296 string = g_strconcat (variable, "=", value, NULL);
297 result = putenv (string);
302 #ifdef HAVE__NSGETENVIRON
303 #define environ (*_NSGetEnviron())
305 /* According to the Single Unix Specification, environ is not
306 * in any system header, although unistd.h often declares it.
308 extern char **environ;
313 * @variable: the environment variable to remove, must not contain '='
315 * Removes an environment variable from the environment.
317 * Note that on some systems, when variables are overwritten, the
318 * memory used for the previous variables and its value isn't reclaimed.
321 * Environment variable handling in UNIX is not thread-safe, and your
322 * program may crash if one thread calls g_unsetenv() while another
323 * thread is calling getenv(). (And note that many functions, such as
324 * gettext(), call getenv() internally.) This function is only safe
325 * to use at the very start of your program, before creating any other
326 * threads (or creating objects that create worker threads of their
329 * If you need to set up the environment for a child process, you can
330 * use g_get_environ() to get an environment array, modify that with
331 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
332 * array directly to execvpe(), g_spawn_async(), or the like.
338 g_unsetenv (const gchar *variable)
341 g_return_if_fail (variable != NULL);
342 g_return_if_fail (strchr (variable, '=') == NULL);
345 #else /* !HAVE_UNSETENV */
346 g_return_if_fail (variable != NULL);
347 g_return_if_fail (strchr (variable, '=') == NULL);
349 /* Mess directly with the environ array.
350 * This seems to be the only portable way to do this.
352 g_environ_unsetenv (environ, variable);
353 #endif /* !HAVE_UNSETENV */
359 * Gets the names of all variables set in the environment.
361 * Programs that want to be portable to Windows should typically use
362 * this function and g_getenv() instead of using the environ array
363 * from the C library directly. On Windows, the strings in the environ
364 * array are in system codepage encoding, while in most of the typical
365 * use cases for environment variables in GLib-using programs you want
366 * the UTF-8 encoding that this function and g_getenv() provide.
368 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
369 * list of strings which must be freed with g_strfreev().
379 len = g_strv_length (environ);
380 result = g_new0 (gchar *, len + 1);
383 for (i = 0; i < len; i++)
385 eq = strchr (environ[i], '=');
387 result[j++] = g_strndup (environ[i], eq - environ[i]);
398 * Gets the list of environment variables for the current process.
400 * The list is %NULL terminated and each item in the list is of the
403 * This is equivalent to direct access to the 'environ' global variable,
406 * The return value is freshly allocated and it should be freed with
407 * g_strfreev() when it is no longer needed.
409 * Returns: (array zero-terminated=1) (transfer full): the list of
410 * environment variables
417 return g_strdupv (environ);
420 /* Win32 implementation {{{1 */
421 #else /* G_OS_WIN32 */
424 g_getenv (const gchar *variable)
428 wchar_t dummy[2], *wname, *wvalue;
431 g_return_val_if_fail (variable != NULL, NULL);
432 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
434 /* On Windows NT, it is relatively typical that environment
435 * variables contain references to other environment variables. If
436 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
437 * environment variables would be stored in the Registry as
438 * REG_EXPAND_SZ type values, and would then get automatically
439 * expanded before a program sees them. But there is broken software
440 * that stores environment variables as REG_SZ values even if they
441 * contain references to other environment variables.)
444 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
446 len = GetEnvironmentVariableW (wname, dummy, 2);
456 wvalue = g_new (wchar_t, len);
458 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
465 if (wcschr (wvalue, L'%') != NULL)
467 wchar_t *tem = wvalue;
469 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
473 wvalue = g_new (wchar_t, len);
475 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
485 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
490 quark = g_quark_from_string (value);
493 return g_quark_to_string (quark);
497 g_setenv (const gchar *variable,
502 wchar_t *wname, *wvalue, *wassignment;
505 g_return_val_if_fail (variable != NULL, FALSE);
506 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
507 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
508 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
510 if (!overwrite && g_getenv (variable) != NULL)
513 /* We want to (if possible) set both the environment variable copy
514 * kept by the C runtime and the one kept by the system.
516 * We can't use only the C runtime's putenv or _wputenv() as that
517 * won't work for arbitrary Unicode strings in a "non-Unicode" app
518 * (with main() and not wmain()). In a "main()" app the C runtime
519 * initializes the C runtime's environment table by converting the
520 * real (wide char) environment variables to system codepage, thus
521 * breaking those that aren't representable in the system codepage.
523 * As the C runtime's putenv() will also set the system copy, we do
524 * the putenv() first, then call SetEnvironmentValueW ourselves.
527 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
528 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
529 tem = g_strconcat (variable, "=", value, NULL);
530 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
533 _wputenv (wassignment);
534 g_free (wassignment);
536 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
545 g_unsetenv (const gchar *variable)
547 wchar_t *wname, *wassignment;
550 g_return_if_fail (variable != NULL);
551 g_return_if_fail (strchr (variable, '=') == NULL);
552 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
554 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
555 tem = g_strconcat (variable, "=", NULL);
556 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
559 _wputenv (wassignment);
560 g_free (wassignment);
562 SetEnvironmentVariableW (wname, NULL);
574 p = (wchar_t *) GetEnvironmentStringsW ();
584 result = g_new0 (gchar *, len + 1);
590 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
591 if (result[j] != NULL)
593 eq = strchr (result[j], '=');
594 if (eq && eq > result[j])
605 FreeEnvironmentStringsW (p);
617 strings = GetEnvironmentStringsW ();
618 for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
619 result = g_new (char *, n + 1);
620 for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
621 result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
622 FreeEnvironmentStringsW (strings);
628 /* Win32 binary compatibility versions {{{1 */
634 g_getenv (const gchar *variable)
636 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
637 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
641 g_free (utf8_variable);
644 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
645 quark = g_quark_from_string (value);
648 return g_quark_to_string (quark);
654 g_setenv (const gchar *variable,
658 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
659 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
660 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
662 g_free (utf8_variable);
671 g_unsetenv (const gchar *variable)
673 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
675 g_unsetenv_utf8 (utf8_variable);
677 g_free (utf8_variable);
682 #endif /* G_OS_WIN32 */
685 /* vim: set foldmethod=marker: */