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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h> /* for _NSGetEnviron */
38 #include "glib-private.h"
40 #include "gmessages.h"
41 #include "gstrfuncs.h"
46 /* Environ array functions {{{1 */
48 g_environ_find (gchar **envp,
49 const gchar *variable)
56 len = strlen (variable);
58 for (i = 0; envp[i]; i++)
60 if (strncmp (envp[i], variable, len) == 0 &&
70 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
71 * list (eg, as returned from g_get_environ()), or %NULL
72 * for an empty environment list
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 * Returns: the value of the environment variable, or %NULL if
80 * the environment variable is not set in @envp. The returned
81 * string is owned by @envp, and will be freed if @variable is
87 g_environ_getenv (gchar **envp,
88 const gchar *variable)
92 g_return_val_if_fail (variable != NULL, NULL);
94 index = g_environ_find (envp, variable);
96 return envp[index] + strlen (variable) + 1;
103 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an
104 * environment list that can be freed using g_strfreev() (e.g., as
105 * returned from g_get_environ()), or %NULL for an empty
107 * @variable: the environment variable to set, must not contain '='
108 * @value: the value for to set the variable to
109 * @overwrite: whether to change the variable if it already exists
111 * Sets the environment variable @variable in the provided list
114 * Returns: (array zero-terminated=1) (transfer full): the
115 * updated environment list. Free it using g_strfreev().
120 g_environ_setenv (gchar **envp,
121 const gchar *variable,
127 g_return_val_if_fail (variable != NULL, NULL);
128 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
129 g_return_val_if_fail (value != NULL, NULL);
131 index = g_environ_find (envp, variable);
136 g_free (envp[index]);
137 envp[index] = g_strdup_printf ("%s=%s", variable, value);
144 length = envp ? g_strv_length (envp) : 0;
145 envp = g_renew (gchar *, envp, length + 2);
146 envp[length] = g_strdup_printf ("%s=%s", variable, value);
147 envp[length + 1] = NULL;
154 g_environ_unsetenv_internal (gchar **envp,
155 const gchar *variable,
161 len = strlen (variable);
163 /* Note that we remove *all* environment entries for
164 * the variable name, not just the first.
169 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
189 * g_environ_unsetenv:
190 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
191 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
192 * or %NULL for an empty environment list
193 * @variable: the environment variable to remove, must not contain '='
195 * Removes the environment variable @variable from the provided
198 * Returns: (array zero-terminated=1) (transfer full): the
199 * updated environment list. Free it using g_strfreev().
204 g_environ_unsetenv (gchar **envp,
205 const gchar *variable)
207 g_return_val_if_fail (variable != NULL, NULL);
208 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
213 return g_environ_unsetenv_internal (envp, variable, TRUE);
216 /* UNIX implemention {{{1 */
221 * @variable: the environment variable to get, in the GLib file name
224 * Returns the value of an environment variable.
226 * The name and value are in the GLib file name encoding. On UNIX,
227 * this means the actual bytes which might or might not be in some
228 * consistent character set and encoding. On Windows, it is in UTF-8.
229 * On Windows, in case the environment variable's value contains
230 * references to other environment variables, they are expanded.
232 * Returns: the value of the environment variable, or %NULL if
233 * the environment variable is not found. The returned string
234 * may be overwritten by the next call to g_getenv(), g_setenv()
238 g_getenv (const gchar *variable)
240 g_return_val_if_fail (variable != NULL, NULL);
242 return getenv (variable);
247 * @variable: the environment variable to set, must not contain '='.
248 * @value: the value for to set the variable to.
249 * @overwrite: whether to change the variable if it already exists.
251 * Sets an environment variable. Both the variable's name and value
252 * should be in the GLib file name encoding. On UNIX, this means that
253 * they can be arbitrary byte strings. On Windows, they should be in
256 * Note that on some systems, when variables are overwritten, the memory
257 * used for the previous variables and its value isn't reclaimed.
259 * You should be mindful of the fact that environment variable handling
260 * in UNIX is not thread-safe, and your program may crash if one thread
261 * calls g_setenv() while another thread is calling getenv(). (And note
262 * that many functions, such as gettext(), call getenv() internally.)
263 * This function is only safe to use at the very start of your program,
264 * before creating any other threads (or creating objects that create
265 * worker threads of their own).
267 * If you need to set up the environment for a child process, you can
268 * use g_get_environ() to get an environment array, modify that with
269 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
270 * array directly to execvpe(), g_spawn_async(), or the like.
272 * Returns: %FALSE if the environment variable couldn't be set.
277 g_setenv (const gchar *variable,
286 g_return_val_if_fail (variable != NULL, FALSE);
287 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
288 g_return_val_if_fail (value != NULL, FALSE);
291 result = setenv (variable, value, overwrite);
293 if (!overwrite && getenv (variable) != NULL)
296 /* This results in a leak when you overwrite existing
297 * settings. It would be fairly easy to fix this by keeping
298 * our own parallel array or hash table.
300 string = g_strconcat (variable, "=", value, NULL);
301 result = putenv (string);
306 #ifdef HAVE__NSGETENVIRON
307 #define environ (*_NSGetEnviron())
309 /* According to the Single Unix Specification, environ is not
310 * in any system header, although unistd.h often declares it.
312 extern char **environ;
317 * @variable: the environment variable to remove, must not contain '='
319 * Removes an environment variable from the environment.
321 * Note that on some systems, when variables are overwritten, the
322 * memory used for the previous variables and its value isn't reclaimed.
324 * You should be mindful of the fact that environment variable handling
325 * in UNIX is not thread-safe, and your program may crash if one thread
326 * calls g_unsetenv() while another thread is calling getenv(). (And note
327 * that many functions, such as gettext(), call getenv() internally.) This
328 * function is only safe to use at the very start of your program, before
329 * creating any other threads (or creating objects that create worker
330 * threads of their own).
332 * If you need to set up the environment for a child process, you can
333 * use g_get_environ() to get an environment array, modify that with
334 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
335 * array directly to execvpe(), g_spawn_async(), or the like.
340 g_unsetenv (const gchar *variable)
342 g_return_if_fail (variable != NULL);
343 g_return_if_fail (strchr (variable, '=') == NULL);
347 #else /* !HAVE_UNSETENV */
348 /* Mess directly with the environ array.
349 * This seems to be the only portable way to do this.
351 g_environ_unsetenv_internal (environ, variable, FALSE);
352 #endif /* !HAVE_UNSETENV */
358 * Gets the names of all variables set in the environment.
360 * Programs that want to be portable to Windows should typically use
361 * this function and g_getenv() instead of using the environ array
362 * from the C library directly. On Windows, the strings in the environ
363 * array are in system codepage encoding, while in most of the typical
364 * use cases for environment variables in GLib-using programs you want
365 * the UTF-8 encoding that this function and g_getenv() provide.
367 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
368 * list of strings which must be freed with g_strfreev().
378 len = g_strv_length (environ);
379 result = g_new0 (gchar *, len + 1);
382 for (i = 0; i < len; i++)
384 eq = strchr (environ[i], '=');
386 result[j++] = g_strndup (environ[i], eq - environ[i]);
397 * Gets the list of environment variables for the current process.
399 * The list is %NULL terminated and each item in the list is of the
402 * This is equivalent to direct access to the 'environ' global variable,
405 * The return value is freshly allocated and it should be freed with
406 * g_strfreev() when it is no longer needed.
408 * Returns: (array zero-terminated=1) (transfer full): the list of
409 * environment variables
416 return g_strdupv (environ);
419 /* Win32 implementation {{{1 */
420 #else /* G_OS_WIN32 */
423 g_getenv (const gchar *variable)
427 wchar_t dummy[2], *wname, *wvalue;
430 g_return_val_if_fail (variable != NULL, NULL);
431 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
433 /* On Windows NT, it is relatively typical that environment
434 * variables contain references to other environment variables. If
435 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
436 * environment variables would be stored in the Registry as
437 * REG_EXPAND_SZ type values, and would then get automatically
438 * expanded before a program sees them. But there is broken software
439 * that stores environment variables as REG_SZ values even if they
440 * contain references to other environment variables.)
443 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
445 len = GetEnvironmentVariableW (wname, dummy, 2);
450 if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
453 quark = g_quark_from_static_string ("");
454 return g_quark_to_string (quark);
459 wvalue = g_new (wchar_t, len);
461 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
468 if (wcschr (wvalue, L'%') != NULL)
470 wchar_t *tem = wvalue;
472 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
476 wvalue = g_new (wchar_t, len);
478 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
488 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
493 quark = g_quark_from_string (value);
496 return g_quark_to_string (quark);
500 g_setenv (const gchar *variable,
505 wchar_t *wname, *wvalue, *wassignment;
508 g_return_val_if_fail (variable != NULL, FALSE);
509 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
510 g_return_val_if_fail (value != NULL, FALSE);
511 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
512 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
514 if (!overwrite && g_getenv (variable) != NULL)
517 /* We want to (if possible) set both the environment variable copy
518 * kept by the C runtime and the one kept by the system.
520 * We can't use only the C runtime's putenv or _wputenv() as that
521 * won't work for arbitrary Unicode strings in a "non-Unicode" app
522 * (with main() and not wmain()). In a "main()" app the C runtime
523 * initializes the C runtime's environment table by converting the
524 * real (wide char) environment variables to system codepage, thus
525 * breaking those that aren't representable in the system codepage.
527 * As the C runtime's putenv() will also set the system copy, we do
528 * the putenv() first, then call SetEnvironmentValueW ourselves.
531 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
532 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
533 tem = g_strconcat (variable, "=", value, NULL);
534 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
537 _wputenv (wassignment);
538 g_free (wassignment);
540 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
549 g_unsetenv (const gchar *variable)
551 wchar_t *wname, *wassignment;
554 g_return_if_fail (variable != NULL);
555 g_return_if_fail (strchr (variable, '=') == NULL);
556 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
558 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
559 tem = g_strconcat (variable, "=", NULL);
560 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
563 _wputenv (wassignment);
564 g_free (wassignment);
566 SetEnvironmentVariableW (wname, NULL);
578 p = (wchar_t *) GetEnvironmentStringsW ();
588 result = g_new0 (gchar *, len + 1);
594 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
595 if (result[j] != NULL)
597 eq = strchr (result[j], '=');
598 if (eq && eq > result[j])
609 FreeEnvironmentStringsW (p);
621 strings = GetEnvironmentStringsW ();
622 for (n = 0, i = 0; strings[n]; i++)
623 n += wcslen (strings + n) + 1;
625 result = g_new (char *, i + 1);
626 for (n = 0, i = 0; strings[n]; i++)
628 result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
629 n += wcslen (strings + n) + 1;
631 FreeEnvironmentStringsW (strings);
637 /* Win32 binary compatibility versions {{{1 */
643 g_getenv (const gchar *variable)
645 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
646 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
650 g_free (utf8_variable);
653 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
654 quark = g_quark_from_string (value);
657 return g_quark_to_string (quark);
663 g_setenv (const gchar *variable,
667 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
668 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
669 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
671 g_free (utf8_variable);
680 g_unsetenv (const gchar *variable)
682 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
684 g_unsetenv_utf8 (utf8_variable);
686 g_free (utf8_variable);
691 #endif /* G_OS_WIN32 */
694 /* vim: set foldmethod=marker: */