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)
60 len = strlen (variable);
62 for (i = 0; envp[i]; i++)
64 if (strncmp (envp[i], variable, len) == 0 &&
74 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
75 * list (eg, as returned from g_get_environ()), or %NULL
76 * for an empty environment list
77 * @variable: the environment variable to get, in the GLib file name
80 * Returns the value of the environment variable @variable in the
81 * provided list @envp.
83 * The name and value are in the GLib file name encoding.
84 * On UNIX, this means the actual bytes which might or might not
85 * be in some consistent character set and encoding. On Windows,
86 * it is in UTF-8. On Windows, in case the environment variable's
87 * value contains references to other environment variables, they
90 * Return value: the value of the environment variable, or %NULL if
91 * the environment variable is not set in @envp. The returned
92 * string is owned by @envp, and will be freed if @variable is
98 g_environ_getenv (gchar **envp,
99 const gchar *variable)
103 g_return_val_if_fail (variable != NULL, NULL);
105 index = g_environ_find (envp, variable);
107 return envp[index] + strlen (variable) + 1;
114 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
115 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
116 * for an empty environment list
117 * @variable: the environment variable to set, must not contain '='
118 * @value: the value for to set the variable to
119 * @overwrite: whether to change the variable if it already exists
121 * Sets the environment variable @variable in the provided list
124 * Both the variable's name and value should be in the GLib
125 * file name encoding. On UNIX, this means that they can be
126 * arbitrary byte strings. On Windows, they should be in UTF-8.
128 * Return value: (array zero-terminated=1) (transfer full): the
129 * updated environment list. Free it using g_strfreev().
134 g_environ_setenv (gchar **envp,
135 const gchar *variable,
141 g_return_val_if_fail (variable != NULL, NULL);
142 g_return_val_if_fail (strchr (variable, '=') == 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 (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
202 * g_environ_unsetenv:
203 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
204 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
205 * or %NULL for an empty environment list
206 * @variable: the environment variable to remove, must not contain '='
208 * Removes the environment variable @variable from the provided
211 * Return value: (array zero-terminated=1) (transfer full): the
212 * updated environment list. Free it using g_strfreev().
217 g_environ_unsetenv (gchar **envp,
218 const gchar *variable)
220 g_return_val_if_fail (variable != NULL, NULL);
221 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
226 return g_environ_unsetenv_internal (envp, variable, TRUE);
229 /* UNIX implemention {{{1 */
234 * @variable: the environment variable to get, in the GLib file name
237 * Returns the value of an environment variable.
239 * The name and value are in the GLib file name encoding. On UNIX,
240 * this means the actual bytes which might or might not be in some
241 * consistent character set and encoding. On Windows, it is in UTF-8.
242 * On Windows, in case the environment variable's value contains
243 * references to other environment variables, they are expanded.
245 * Return value: 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: the environment variable to set, must not contain '='.
261 * @value: the value for to set the variable to.
262 * @overwrite: whether to change the variable if it already exists.
264 * Sets an environment variable. Both the variable's name and value
265 * should be in the GLib file name encoding. On UNIX, this means that
266 * they can be arbitrary byte strings. On Windows, they should be in
269 * Note that on some systems, when variables are overwritten, the memory
270 * used for the previous variables and its value isn't reclaimed.
273 * Environment variable handling in UNIX is not thread-safe, and your
274 * program may crash if one thread calls g_setenv() while another
275 * thread is calling getenv(). (And note that many functions, such as
276 * gettext(), call getenv() internally.) This function is only safe to
277 * use at the very start of your program, before creating any other
278 * threads (or creating objects that create worker threads of their
281 * If you need to set up the environment for a child process, you can
282 * use g_get_environ() to get an environment array, modify that with
283 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
284 * array directly to execvpe(), g_spawn_async(), or the like.
287 * Returns: %FALSE if the environment variable couldn't be set.
292 g_setenv (const gchar *variable,
301 g_return_val_if_fail (variable != NULL, FALSE);
302 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
305 result = setenv (variable, value, overwrite);
307 if (!overwrite && getenv (variable) != NULL)
310 /* This results in a leak when you overwrite existing
311 * settings. It would be fairly easy to fix this by keeping
312 * our own parallel array or hash table.
314 string = g_strconcat (variable, "=", value, NULL);
315 result = putenv (string);
320 #ifdef HAVE__NSGETENVIRON
321 #define environ (*_NSGetEnviron())
323 /* According to the Single Unix Specification, environ is not
324 * in any system header, although unistd.h often declares it.
326 extern char **environ;
331 * @variable: the environment variable to remove, must not contain '='
333 * Removes an environment variable from the environment.
335 * Note that on some systems, when variables are overwritten, the
336 * memory used for the previous variables and its value isn't reclaimed.
339 * Environment variable handling in UNIX is not thread-safe, and your
340 * program may crash if one thread calls g_unsetenv() while another
341 * thread is calling getenv(). (And note that many functions, such as
342 * gettext(), call getenv() internally.) This function is only safe
343 * to use at the very start of your program, before creating any other
344 * threads (or creating objects that create worker threads of their
347 * If you need to set up the environment for a child process, you can
348 * use g_get_environ() to get an environment array, modify that with
349 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
350 * array directly to execvpe(), g_spawn_async(), or the like.
356 g_unsetenv (const gchar *variable)
359 g_return_if_fail (variable != NULL);
360 g_return_if_fail (strchr (variable, '=') == NULL);
363 #else /* !HAVE_UNSETENV */
364 g_return_if_fail (variable != NULL);
365 g_return_if_fail (strchr (variable, '=') == NULL);
367 /* Mess directly with the environ array.
368 * This seems to be the only portable way to do this.
370 g_environ_unsetenv_internal (environ, variable, FALSE);
371 #endif /* !HAVE_UNSETENV */
377 * Gets the names of all variables set in the environment.
379 * Programs that want to be portable to Windows should typically use
380 * this function and g_getenv() instead of using the environ array
381 * from the C library directly. On Windows, the strings in the environ
382 * array are in system codepage encoding, while in most of the typical
383 * use cases for environment variables in GLib-using programs you want
384 * the UTF-8 encoding that this function and g_getenv() provide.
386 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
387 * list of strings which must be freed with g_strfreev().
397 len = g_strv_length (environ);
398 result = g_new0 (gchar *, len + 1);
401 for (i = 0; i < len; i++)
403 eq = strchr (environ[i], '=');
405 result[j++] = g_strndup (environ[i], eq - environ[i]);
416 * Gets the list of environment variables for the current process.
418 * The list is %NULL terminated and each item in the list is of the
421 * This is equivalent to direct access to the 'environ' global variable,
424 * The return value is freshly allocated and it should be freed with
425 * g_strfreev() when it is no longer needed.
427 * Returns: (array zero-terminated=1) (transfer full): the list of
428 * environment variables
435 return g_strdupv (environ);
438 /* Win32 implementation {{{1 */
439 #else /* G_OS_WIN32 */
442 g_getenv (const gchar *variable)
446 wchar_t dummy[2], *wname, *wvalue;
449 g_return_val_if_fail (variable != NULL, NULL);
450 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
452 /* On Windows NT, it is relatively typical that environment
453 * variables contain references to other environment variables. If
454 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
455 * environment variables would be stored in the Registry as
456 * REG_EXPAND_SZ type values, and would then get automatically
457 * expanded before a program sees them. But there is broken software
458 * that stores environment variables as REG_SZ values even if they
459 * contain references to other environment variables.)
462 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
464 len = GetEnvironmentVariableW (wname, dummy, 2);
474 wvalue = g_new (wchar_t, len);
476 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
483 if (wcschr (wvalue, L'%') != NULL)
485 wchar_t *tem = wvalue;
487 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
491 wvalue = g_new (wchar_t, len);
493 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
503 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
508 quark = g_quark_from_string (value);
511 return g_quark_to_string (quark);
515 g_setenv (const gchar *variable,
520 wchar_t *wname, *wvalue, *wassignment;
523 g_return_val_if_fail (variable != NULL, FALSE);
524 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
525 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
526 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
528 if (!overwrite && g_getenv (variable) != NULL)
531 /* We want to (if possible) set both the environment variable copy
532 * kept by the C runtime and the one kept by the system.
534 * We can't use only the C runtime's putenv or _wputenv() as that
535 * won't work for arbitrary Unicode strings in a "non-Unicode" app
536 * (with main() and not wmain()). In a "main()" app the C runtime
537 * initializes the C runtime's environment table by converting the
538 * real (wide char) environment variables to system codepage, thus
539 * breaking those that aren't representable in the system codepage.
541 * As the C runtime's putenv() will also set the system copy, we do
542 * the putenv() first, then call SetEnvironmentValueW ourselves.
545 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
546 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
547 tem = g_strconcat (variable, "=", value, NULL);
548 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
551 _wputenv (wassignment);
552 g_free (wassignment);
554 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
563 g_unsetenv (const gchar *variable)
565 wchar_t *wname, *wassignment;
568 g_return_if_fail (variable != NULL);
569 g_return_if_fail (strchr (variable, '=') == NULL);
570 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
572 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
573 tem = g_strconcat (variable, "=", NULL);
574 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
577 _wputenv (wassignment);
578 g_free (wassignment);
580 SetEnvironmentVariableW (wname, NULL);
592 p = (wchar_t *) GetEnvironmentStringsW ();
602 result = g_new0 (gchar *, len + 1);
608 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
609 if (result[j] != NULL)
611 eq = strchr (result[j], '=');
612 if (eq && eq > result[j])
623 FreeEnvironmentStringsW (p);
635 strings = GetEnvironmentStringsW ();
636 for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
637 result = g_new (char *, n + 1);
638 for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
639 result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
640 FreeEnvironmentStringsW (strings);
646 /* Win32 binary compatibility versions {{{1 */
652 g_getenv (const gchar *variable)
654 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
655 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
659 g_free (utf8_variable);
662 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
663 quark = g_quark_from_string (value);
666 return g_quark_to_string (quark);
672 g_setenv (const gchar *variable,
676 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
677 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
678 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
680 g_free (utf8_variable);
689 g_unsetenv (const gchar *variable)
691 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
693 g_unsetenv_utf8 (utf8_variable);
695 g_free (utf8_variable);
700 #endif /* G_OS_WIN32 */
703 /* vim: set foldmethod=marker: */