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/.
33 #ifdef HAVE_CRT_EXTERNS_H
34 #include <crt_externs.h> /* for _NSGetEnviron */
40 #include "glib-private.h"
42 #include "gmessages.h"
43 #include "gstrfuncs.h"
48 /* Environ array functions {{{1 */
50 g_environ_find (gchar **envp,
51 const gchar *variable)
58 len = strlen (variable);
60 for (i = 0; envp[i]; i++)
62 if (strncmp (envp[i], variable, len) == 0 &&
72 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
73 * list (eg, as returned from g_get_environ()), or %NULL
74 * for an empty environment list
75 * @variable: the environment variable to get, in the GLib file name
78 * Returns the value of the environment variable @variable in the
79 * provided list @envp.
81 * Return value: the value of the environment variable, or %NULL if
82 * the environment variable is not set in @envp. The returned
83 * string is owned by @envp, and will be freed if @variable is
89 g_environ_getenv (gchar **envp,
90 const gchar *variable)
94 g_return_val_if_fail (variable != NULL, NULL);
96 index = g_environ_find (envp, variable);
98 return envp[index] + strlen (variable) + 1;
105 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an
106 * environment list that can be freed using g_strfreev() (e.g., as
107 * returned from g_get_environ()), or %NULL for an empty
109 * @variable: the environment variable to set, must not contain '='
110 * @value: the value for to set the variable to
111 * @overwrite: whether to change the variable if it already exists
113 * Sets the environment variable @variable in the provided list
116 * Return value: (array zero-terminated=1) (transfer full): the
117 * updated environment list. Free it using g_strfreev().
122 g_environ_setenv (gchar **envp,
123 const gchar *variable,
129 g_return_val_if_fail (variable != NULL, NULL);
130 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
131 g_return_val_if_fail (value != NULL, NULL);
133 index = g_environ_find (envp, variable);
138 g_free (envp[index]);
139 envp[index] = g_strdup_printf ("%s=%s", variable, value);
146 length = envp ? g_strv_length (envp) : 0;
147 envp = g_renew (gchar *, envp, length + 2);
148 envp[length] = g_strdup_printf ("%s=%s", variable, value);
149 envp[length + 1] = NULL;
156 g_environ_unsetenv_internal (gchar **envp,
157 const gchar *variable,
163 len = strlen (variable);
165 /* Note that we remove *all* environment entries for
166 * the variable name, not just the first.
171 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
191 * g_environ_unsetenv:
192 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
193 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
194 * or %NULL for an empty environment list
195 * @variable: the environment variable to remove, must not contain '='
197 * Removes the environment variable @variable from the provided
200 * Return value: (array zero-terminated=1) (transfer full): the
201 * updated environment list. Free it using g_strfreev().
206 g_environ_unsetenv (gchar **envp,
207 const gchar *variable)
209 g_return_val_if_fail (variable != NULL, NULL);
210 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
215 return g_environ_unsetenv_internal (envp, variable, TRUE);
218 /* UNIX implemention {{{1 */
223 * @variable: the environment variable to get, in the GLib file name
226 * Returns the value of an environment variable.
228 * The name and value are in the GLib file name encoding. On UNIX,
229 * this means the actual bytes which might or might not be in some
230 * consistent character set and encoding. On Windows, it is in UTF-8.
231 * On Windows, in case the environment variable's value contains
232 * references to other environment variables, they are expanded.
234 * Return value: the value of the environment variable, or %NULL if
235 * the environment variable is not found. The returned string
236 * may be overwritten by the next call to g_getenv(), g_setenv()
240 g_getenv (const gchar *variable)
242 g_return_val_if_fail (variable != NULL, NULL);
244 return getenv (variable);
249 * @variable: the environment variable to set, must not contain '='.
250 * @value: the value for to set the variable to.
251 * @overwrite: whether to change the variable if it already exists.
253 * Sets an environment variable. Both the variable's name and value
254 * should be in the GLib file name encoding. On UNIX, this means that
255 * they can be arbitrary byte strings. On Windows, they should be in
258 * Note that on some systems, when variables are overwritten, the memory
259 * used for the previous variables and its value isn't reclaimed.
262 * Environment variable handling in UNIX is not thread-safe, and your
263 * program may crash if one thread calls g_setenv() while another
264 * thread is calling getenv(). (And note that many functions, such as
265 * gettext(), call getenv() internally.) This function is only safe to
266 * use at the very start of your program, before creating any other
267 * threads (or creating objects that create worker threads of their
270 * If you need to set up the environment for a child process, you can
271 * use g_get_environ() to get an environment array, modify that with
272 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
273 * array directly to execvpe(), g_spawn_async(), or the like.
276 * Returns: %FALSE if the environment variable couldn't be set.
281 g_setenv (const gchar *variable,
290 g_return_val_if_fail (variable != NULL, FALSE);
291 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
292 g_return_val_if_fail (value != NULL, FALSE);
295 result = setenv (variable, value, overwrite);
297 if (!overwrite && getenv (variable) != NULL)
300 /* This results in a leak when you overwrite existing
301 * settings. It would be fairly easy to fix this by keeping
302 * our own parallel array or hash table.
304 string = g_strconcat (variable, "=", value, NULL);
305 result = putenv (string);
310 #ifdef HAVE__NSGETENVIRON
311 #define environ (*_NSGetEnviron())
313 /* According to the Single Unix Specification, environ is not
314 * in any system header, although unistd.h often declares it.
316 extern char **environ;
321 * @variable: the environment variable to remove, must not contain '='
323 * Removes an environment variable from the environment.
325 * Note that on some systems, when variables are overwritten, the
326 * memory used for the previous variables and its value isn't reclaimed.
329 * Environment variable handling in UNIX is not thread-safe, and your
330 * program may crash if one thread calls g_unsetenv() while another
331 * thread is calling getenv(). (And note that many functions, such as
332 * gettext(), call getenv() internally.) This function is only safe
333 * to use at the very start of your program, before creating any other
334 * threads (or creating objects that create worker threads of their
337 * If you need to set up the environment for a child process, you can
338 * use g_get_environ() to get an environment array, modify that with
339 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
340 * array directly to execvpe(), g_spawn_async(), or the like.
346 g_unsetenv (const gchar *variable)
348 g_return_if_fail (variable != NULL);
349 g_return_if_fail (strchr (variable, '=') == NULL);
353 #else /* !HAVE_UNSETENV */
354 /* Mess directly with the environ array.
355 * This seems to be the only portable way to do this.
357 g_environ_unsetenv_internal (environ, variable, FALSE);
358 #endif /* !HAVE_UNSETENV */
364 * Gets the names of all variables set in the environment.
366 * Programs that want to be portable to Windows should typically use
367 * this function and g_getenv() instead of using the environ array
368 * from the C library directly. On Windows, the strings in the environ
369 * array are in system codepage encoding, while in most of the typical
370 * use cases for environment variables in GLib-using programs you want
371 * the UTF-8 encoding that this function and g_getenv() provide.
373 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
374 * list of strings which must be freed with g_strfreev().
384 len = g_strv_length (environ);
385 result = g_new0 (gchar *, len + 1);
388 for (i = 0; i < len; i++)
390 eq = strchr (environ[i], '=');
392 result[j++] = g_strndup (environ[i], eq - environ[i]);
403 * Gets the list of environment variables for the current process.
405 * The list is %NULL terminated and each item in the list is of the
408 * This is equivalent to direct access to the 'environ' global variable,
411 * The return value is freshly allocated and it should be freed with
412 * g_strfreev() when it is no longer needed.
414 * Returns: (array zero-terminated=1) (transfer full): the list of
415 * environment variables
422 return g_strdupv (environ);
425 /* Win32 implementation {{{1 */
426 #else /* G_OS_WIN32 */
429 g_getenv (const gchar *variable)
433 wchar_t dummy[2], *wname, *wvalue;
436 g_return_val_if_fail (variable != NULL, NULL);
437 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
439 /* On Windows NT, it is relatively typical that environment
440 * variables contain references to other environment variables. If
441 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
442 * environment variables would be stored in the Registry as
443 * REG_EXPAND_SZ type values, and would then get automatically
444 * expanded before a program sees them. But there is broken software
445 * that stores environment variables as REG_SZ values even if they
446 * contain references to other environment variables.)
449 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
451 len = GetEnvironmentVariableW (wname, dummy, 2);
456 if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
459 quark = g_quark_from_static_string ("");
460 return g_quark_to_string (quark);
465 wvalue = g_new (wchar_t, len);
467 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
474 if (wcschr (wvalue, L'%') != NULL)
476 wchar_t *tem = wvalue;
478 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
482 wvalue = g_new (wchar_t, len);
484 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
494 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
499 quark = g_quark_from_string (value);
502 return g_quark_to_string (quark);
506 g_setenv (const gchar *variable,
511 wchar_t *wname, *wvalue, *wassignment;
514 g_return_val_if_fail (variable != NULL, FALSE);
515 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
516 g_return_val_if_fail (value != NULL, FALSE);
517 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
518 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
520 if (!overwrite && g_getenv (variable) != NULL)
523 /* We want to (if possible) set both the environment variable copy
524 * kept by the C runtime and the one kept by the system.
526 * We can't use only the C runtime's putenv or _wputenv() as that
527 * won't work for arbitrary Unicode strings in a "non-Unicode" app
528 * (with main() and not wmain()). In a "main()" app the C runtime
529 * initializes the C runtime's environment table by converting the
530 * real (wide char) environment variables to system codepage, thus
531 * breaking those that aren't representable in the system codepage.
533 * As the C runtime's putenv() will also set the system copy, we do
534 * the putenv() first, then call SetEnvironmentValueW ourselves.
537 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
538 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
539 tem = g_strconcat (variable, "=", value, NULL);
540 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
543 _wputenv (wassignment);
544 g_free (wassignment);
546 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
555 g_unsetenv (const gchar *variable)
557 wchar_t *wname, *wassignment;
560 g_return_if_fail (variable != NULL);
561 g_return_if_fail (strchr (variable, '=') == NULL);
562 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
564 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
565 tem = g_strconcat (variable, "=", NULL);
566 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
569 _wputenv (wassignment);
570 g_free (wassignment);
572 SetEnvironmentVariableW (wname, NULL);
584 p = (wchar_t *) GetEnvironmentStringsW ();
594 result = g_new0 (gchar *, len + 1);
600 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
601 if (result[j] != NULL)
603 eq = strchr (result[j], '=');
604 if (eq && eq > result[j])
615 FreeEnvironmentStringsW (p);
627 strings = GetEnvironmentStringsW ();
628 for (n = 0, i = 0; strings[n]; i++)
629 n += wcslen (strings + n) + 1;
631 result = g_new (char *, i + 1);
632 for (n = 0, i = 0; strings[n]; i++)
634 result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
635 n += wcslen (strings + n) + 1;
637 FreeEnvironmentStringsW (strings);
643 /* Win32 binary compatibility versions {{{1 */
649 g_getenv (const gchar *variable)
651 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
652 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
656 g_free (utf8_variable);
659 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
660 quark = g_quark_from_string (value);
663 return g_quark_to_string (quark);
669 g_setenv (const gchar *variable,
673 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
674 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
675 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
677 g_free (utf8_variable);
686 g_unsetenv (const gchar *variable)
688 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
690 g_unsetenv_utf8 (utf8_variable);
692 g_free (utf8_variable);
697 #endif /* G_OS_WIN32 */
700 /* vim: set foldmethod=marker: */