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] != '=')
208 /* UNIX implemention {{{1 */
213 * @variable: the environment variable to get, in the GLib file name
216 * Returns the value of an environment variable.
218 * The name and value are in the GLib file name encoding. On UNIX,
219 * this means the actual bytes which might or might not be in some
220 * consistent character set and encoding. On Windows, it is in UTF-8.
221 * On Windows, in case the environment variable's value contains
222 * references to other environment variables, they are expanded.
224 * Return value: the value of the environment variable, or %NULL if
225 * the environment variable is not found. The returned string
226 * may be overwritten by the next call to g_getenv(), g_setenv()
230 g_getenv (const gchar *variable)
232 g_return_val_if_fail (variable != NULL, NULL);
234 return getenv (variable);
239 * @variable: the environment variable to set, must not contain '='.
240 * @value: the value for to set the variable to.
241 * @overwrite: whether to change the variable if it already exists.
243 * Sets an environment variable. Both the variable's name and value
244 * should be in the GLib file name encoding. On UNIX, this means that
245 * they can be arbitrary byte strings. On Windows, they should be in
248 * Note that on some systems, when variables are overwritten, the memory
249 * used for the previous variables and its value isn't reclaimed.
252 * Environment variable handling in UNIX is not thread-safe, and your
253 * program may crash if one thread calls g_setenv() while another
254 * thread is calling getenv(). (And note that many functions, such as
255 * gettext(), call getenv() internally.) This function is only safe to
256 * use at the very start of your program, before creating any other
257 * threads (or creating objects that create worker threads of their
260 * If you need to set up the environment for a child process, you can
261 * use g_get_environ() to get an environment array, modify that with
262 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
263 * array directly to execvpe(), g_spawn_async(), or the like.
266 * Returns: %FALSE if the environment variable couldn't be set.
271 g_setenv (const gchar *variable,
280 g_return_val_if_fail (variable != NULL, FALSE);
281 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
284 result = setenv (variable, value, overwrite);
286 if (!overwrite && getenv (variable) != NULL)
289 /* This results in a leak when you overwrite existing
290 * settings. It would be fairly easy to fix this by keeping
291 * our own parallel array or hash table.
293 string = g_strconcat (variable, "=", value, NULL);
294 result = putenv (string);
299 #ifdef HAVE__NSGETENVIRON
300 #define environ (*_NSGetEnviron())
302 /* According to the Single Unix Specification, environ is not
303 * in any system header, although unistd.h often declares it.
305 extern char **environ;
310 * @variable: the environment variable to remove, must not contain '='
312 * Removes an environment variable from the environment.
314 * Note that on some systems, when variables are overwritten, the
315 * memory used for the previous variables and its value isn't reclaimed.
318 * Environment variable handling in UNIX is not thread-safe, and your
319 * program may crash if one thread calls g_unsetenv() while another
320 * thread is calling getenv(). (And note that many functions, such as
321 * gettext(), call getenv() internally.) This function is only safe
322 * to use at the very start of your program, before creating any other
323 * threads (or creating objects that create worker threads of their
326 * If you need to set up the environment for a child process, you can
327 * use g_get_environ() to get an environment array, modify that with
328 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
329 * array directly to execvpe(), g_spawn_async(), or the like.
335 g_unsetenv (const gchar *variable)
338 g_return_if_fail (variable != NULL);
339 g_return_if_fail (strchr (variable, '=') == NULL);
342 #else /* !HAVE_UNSETENV */
343 g_return_if_fail (variable != NULL);
344 g_return_if_fail (strchr (variable, '=') == NULL);
346 /* Mess directly with the environ array.
347 * This seems to be the only portable way to do this.
349 g_environ_unsetenv (environ, variable);
350 #endif /* !HAVE_UNSETENV */
356 * Gets the names of all variables set in the environment.
358 * Programs that want to be portable to Windows should typically use
359 * this function and g_getenv() instead of using the environ array
360 * from the C library directly. On Windows, the strings in the environ
361 * array are in system codepage encoding, while in most of the typical
362 * use cases for environment variables in GLib-using programs you want
363 * the UTF-8 encoding that this function and g_getenv() provide.
365 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
366 * list of strings which must be freed with g_strfreev().
376 len = g_strv_length (environ);
377 result = g_new0 (gchar *, len + 1);
380 for (i = 0; i < len; i++)
382 eq = strchr (environ[i], '=');
384 result[j++] = g_strndup (environ[i], eq - environ[i]);
395 * Gets the list of environment variables for the current process.
397 * The list is %NULL terminated and each item in the list is of the
400 * This is equivalent to direct access to the 'environ' global variable,
403 * The return value is freshly allocated and it should be freed with
404 * g_strfreev() when it is no longer needed.
406 * Returns: (array zero-terminated=1) (transfer full): the list of
407 * environment variables
414 return g_strdupv (environ);
417 /* Win32 implementation {{{1 */
418 #else /* G_OS_WIN32 */
421 g_getenv (const gchar *variable)
425 wchar_t dummy[2], *wname, *wvalue;
428 g_return_val_if_fail (variable != NULL, NULL);
429 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
431 /* On Windows NT, it is relatively typical that environment
432 * variables contain references to other environment variables. If
433 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
434 * environment variables would be stored in the Registry as
435 * REG_EXPAND_SZ type values, and would then get automatically
436 * expanded before a program sees them. But there is broken software
437 * that stores environment variables as REG_SZ values even if they
438 * contain references to other environment variables.)
441 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
443 len = GetEnvironmentVariableW (wname, dummy, 2);
453 wvalue = g_new (wchar_t, len);
455 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
462 if (wcschr (wvalue, L'%') != NULL)
464 wchar_t *tem = wvalue;
466 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
470 wvalue = g_new (wchar_t, len);
472 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
482 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
487 quark = g_quark_from_string (value);
490 return g_quark_to_string (quark);
494 g_setenv (const gchar *variable,
499 wchar_t *wname, *wvalue, *wassignment;
502 g_return_val_if_fail (variable != NULL, FALSE);
503 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
504 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
505 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
507 if (!overwrite && g_getenv (variable) != NULL)
510 /* We want to (if possible) set both the environment variable copy
511 * kept by the C runtime and the one kept by the system.
513 * We can't use only the C runtime's putenv or _wputenv() as that
514 * won't work for arbitrary Unicode strings in a "non-Unicode" app
515 * (with main() and not wmain()). In a "main()" app the C runtime
516 * initializes the C runtime's environment table by converting the
517 * real (wide char) environment variables to system codepage, thus
518 * breaking those that aren't representable in the system codepage.
520 * As the C runtime's putenv() will also set the system copy, we do
521 * the putenv() first, then call SetEnvironmentValueW ourselves.
524 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
525 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
526 tem = g_strconcat (variable, "=", value, NULL);
527 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
530 _wputenv (wassignment);
531 g_free (wassignment);
533 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
542 g_unsetenv (const gchar *variable)
544 wchar_t *wname, *wassignment;
547 g_return_if_fail (variable != NULL);
548 g_return_if_fail (strchr (variable, '=') == NULL);
549 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
551 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
552 tem = g_strconcat (variable, "=", NULL);
553 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
556 _wputenv (wassignment);
557 g_free (wassignment);
559 SetEnvironmentVariableW (wname, NULL);
571 p = (wchar_t *) GetEnvironmentStringsW ();
581 result = g_new0 (gchar *, len + 1);
587 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
588 if (result[j] != NULL)
590 eq = strchr (result[j], '=');
591 if (eq && eq > result[j])
602 FreeEnvironmentStringsW (p);
614 strings = GetEnvironmentStringsW ();
615 for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
616 result = g_new (char *, n + 1);
617 for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
618 result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
619 FreeEnvironmentStringsW (strings);
625 /* Win32 binary compatibility versions {{{1 */
631 g_getenv (const gchar *variable)
633 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
634 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
638 g_free (utf8_variable);
641 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
642 quark = g_quark_from_string (value);
645 return g_quark_to_string (quark);
651 g_setenv (const gchar *variable,
655 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
656 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
657 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
659 g_free (utf8_variable);
668 g_unsetenv (const gchar *variable)
670 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
672 g_unsetenv_utf8 (utf8_variable);
674 g_free (utf8_variable);
679 #endif /* G_OS_WIN32 */
682 /* vim: set foldmethod=marker: */