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 */
43 #include "glib-private.h"
45 #include "gmessages.h"
46 #include "gstrfuncs.h"
51 /* Environ array functions {{{1 */
53 g_environ_find (gchar **envp,
54 const gchar *variable)
61 len = strlen (variable);
63 for (i = 0; envp[i]; i++)
65 if (strncmp (envp[i], variable, len) == 0 &&
75 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
76 * list (eg, as returned from g_get_environ()), or %NULL
77 * for an empty environment list
78 * @variable: the environment variable to get, in the GLib file name
81 * Returns the value of the environment variable @variable in the
82 * provided list @envp.
84 * Return value: the value of the environment variable, or %NULL if
85 * the environment variable is not set in @envp. The returned
86 * string is owned by @envp, and will be freed if @variable is
92 g_environ_getenv (gchar **envp,
93 const gchar *variable)
97 g_return_val_if_fail (variable != NULL, NULL);
99 index = g_environ_find (envp, variable);
101 return envp[index] + strlen (variable) + 1;
108 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an
109 * environment list that can be freed using g_strfreev() (e.g., as
110 * returned from g_get_environ()), or %NULL for an empty
112 * @variable: the environment variable to set, must not contain '='
113 * @value: the value for to set the variable to
114 * @overwrite: whether to change the variable if it already exists
116 * Sets the environment variable @variable in the provided list
119 * Return value: (array zero-terminated=1) (transfer full): the
120 * updated environment list. Free it using g_strfreev().
125 g_environ_setenv (gchar **envp,
126 const gchar *variable,
132 g_return_val_if_fail (variable != NULL, NULL);
133 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
134 g_return_val_if_fail (value != NULL, NULL);
136 index = g_environ_find (envp, variable);
141 g_free (envp[index]);
142 envp[index] = g_strdup_printf ("%s=%s", variable, value);
149 length = envp ? g_strv_length (envp) : 0;
150 envp = g_renew (gchar *, envp, length + 2);
151 envp[length] = g_strdup_printf ("%s=%s", variable, value);
152 envp[length + 1] = NULL;
159 g_environ_unsetenv_internal (gchar **envp,
160 const gchar *variable,
166 len = strlen (variable);
168 /* Note that we remove *all* environment entries for
169 * the variable name, not just the first.
174 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
194 * g_environ_unsetenv:
195 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
196 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
197 * or %NULL for an empty environment list
198 * @variable: the environment variable to remove, must not contain '='
200 * Removes the environment variable @variable from the provided
203 * Return value: (array zero-terminated=1) (transfer full): the
204 * updated environment list. Free it using g_strfreev().
209 g_environ_unsetenv (gchar **envp,
210 const gchar *variable)
212 g_return_val_if_fail (variable != NULL, NULL);
213 g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
218 return g_environ_unsetenv_internal (envp, variable, TRUE);
221 /* UNIX implemention {{{1 */
226 * @variable: the environment variable to get, in the GLib file name
229 * Returns the value of an environment variable.
231 * The name and value are in the GLib file name encoding. On UNIX,
232 * this means the actual bytes which might or might not be in some
233 * consistent character set and encoding. On Windows, it is in UTF-8.
234 * On Windows, in case the environment variable's value contains
235 * references to other environment variables, they are expanded.
237 * Return value: the value of the environment variable, or %NULL if
238 * the environment variable is not found. The returned string
239 * may be overwritten by the next call to g_getenv(), g_setenv()
243 g_getenv (const gchar *variable)
245 g_return_val_if_fail (variable != NULL, NULL);
247 return getenv (variable);
252 * @variable: the environment variable to set, must not contain '='.
253 * @value: the value for to set the variable to.
254 * @overwrite: whether to change the variable if it already exists.
256 * Sets an environment variable. Both the variable's name and value
257 * should be in the GLib file name encoding. On UNIX, this means that
258 * they can be arbitrary byte strings. On Windows, they should be in
261 * Note that on some systems, when variables are overwritten, the memory
262 * used for the previous variables and its value isn't reclaimed.
265 * Environment variable handling in UNIX is not thread-safe, and your
266 * program may crash if one thread calls g_setenv() while another
267 * thread is calling getenv(). (And note that many functions, such as
268 * gettext(), call getenv() internally.) This function is only safe to
269 * use at the very start of your program, before creating any other
270 * threads (or creating objects that create worker threads of their
273 * If you need to set up the environment for a child process, you can
274 * use g_get_environ() to get an environment array, modify that with
275 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
276 * array directly to execvpe(), g_spawn_async(), or the like.
279 * Returns: %FALSE if the environment variable couldn't be set.
284 g_setenv (const gchar *variable,
293 g_return_val_if_fail (variable != NULL, FALSE);
294 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
295 g_return_val_if_fail (value != NULL, FALSE);
298 result = setenv (variable, value, overwrite);
300 if (!overwrite && getenv (variable) != NULL)
303 /* This results in a leak when you overwrite existing
304 * settings. It would be fairly easy to fix this by keeping
305 * our own parallel array or hash table.
307 string = g_strconcat (variable, "=", value, NULL);
308 result = putenv (string);
313 #ifdef HAVE__NSGETENVIRON
314 #define environ (*_NSGetEnviron())
316 /* According to the Single Unix Specification, environ is not
317 * in any system header, although unistd.h often declares it.
319 extern char **environ;
324 * @variable: the environment variable to remove, must not contain '='
326 * Removes an environment variable from the environment.
328 * Note that on some systems, when variables are overwritten, the
329 * memory used for the previous variables and its value isn't reclaimed.
332 * Environment variable handling in UNIX is not thread-safe, and your
333 * program may crash if one thread calls g_unsetenv() while another
334 * thread is calling getenv(). (And note that many functions, such as
335 * gettext(), call getenv() internally.) This function is only safe
336 * to use at the very start of your program, before creating any other
337 * threads (or creating objects that create worker threads of their
340 * If you need to set up the environment for a child process, you can
341 * use g_get_environ() to get an environment array, modify that with
342 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
343 * array directly to execvpe(), g_spawn_async(), or the like.
349 g_unsetenv (const gchar *variable)
351 g_return_if_fail (variable != NULL);
352 g_return_if_fail (strchr (variable, '=') == NULL);
356 #else /* !HAVE_UNSETENV */
357 /* Mess directly with the environ array.
358 * This seems to be the only portable way to do this.
360 g_environ_unsetenv_internal (environ, variable, FALSE);
361 #endif /* !HAVE_UNSETENV */
367 * Gets the names of all variables set in the environment.
369 * Programs that want to be portable to Windows should typically use
370 * this function and g_getenv() instead of using the environ array
371 * from the C library directly. On Windows, the strings in the environ
372 * array are in system codepage encoding, while in most of the typical
373 * use cases for environment variables in GLib-using programs you want
374 * the UTF-8 encoding that this function and g_getenv() provide.
376 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
377 * list of strings which must be freed with g_strfreev().
387 len = g_strv_length (environ);
388 result = g_new0 (gchar *, len + 1);
391 for (i = 0; i < len; i++)
393 eq = strchr (environ[i], '=');
395 result[j++] = g_strndup (environ[i], eq - environ[i]);
406 * Gets the list of environment variables for the current process.
408 * The list is %NULL terminated and each item in the list is of the
411 * This is equivalent to direct access to the 'environ' global variable,
414 * The return value is freshly allocated and it should be freed with
415 * g_strfreev() when it is no longer needed.
417 * Returns: (array zero-terminated=1) (transfer full): the list of
418 * environment variables
425 return g_strdupv (environ);
428 /* Win32 implementation {{{1 */
429 #else /* G_OS_WIN32 */
432 g_getenv (const gchar *variable)
436 wchar_t dummy[2], *wname, *wvalue;
439 g_return_val_if_fail (variable != NULL, NULL);
440 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
442 /* On Windows NT, it is relatively typical that environment
443 * variables contain references to other environment variables. If
444 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
445 * environment variables would be stored in the Registry as
446 * REG_EXPAND_SZ type values, and would then get automatically
447 * expanded before a program sees them. But there is broken software
448 * that stores environment variables as REG_SZ values even if they
449 * contain references to other environment variables.)
452 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
454 len = GetEnvironmentVariableW (wname, dummy, 2);
459 if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
462 quark = g_quark_from_static_string ("");
463 return g_quark_to_string (quark);
468 wvalue = g_new (wchar_t, len);
470 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
477 if (wcschr (wvalue, L'%') != NULL)
479 wchar_t *tem = wvalue;
481 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
485 wvalue = g_new (wchar_t, len);
487 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
497 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
502 quark = g_quark_from_string (value);
505 return g_quark_to_string (quark);
509 g_setenv (const gchar *variable,
514 wchar_t *wname, *wvalue, *wassignment;
517 g_return_val_if_fail (variable != NULL, FALSE);
518 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
519 g_return_val_if_fail (value != NULL, FALSE);
520 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
521 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
523 if (!overwrite && g_getenv (variable) != NULL)
526 /* We want to (if possible) set both the environment variable copy
527 * kept by the C runtime and the one kept by the system.
529 * We can't use only the C runtime's putenv or _wputenv() as that
530 * won't work for arbitrary Unicode strings in a "non-Unicode" app
531 * (with main() and not wmain()). In a "main()" app the C runtime
532 * initializes the C runtime's environment table by converting the
533 * real (wide char) environment variables to system codepage, thus
534 * breaking those that aren't representable in the system codepage.
536 * As the C runtime's putenv() will also set the system copy, we do
537 * the putenv() first, then call SetEnvironmentValueW ourselves.
540 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
541 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
542 tem = g_strconcat (variable, "=", value, NULL);
543 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
546 _wputenv (wassignment);
547 g_free (wassignment);
549 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
558 g_unsetenv (const gchar *variable)
560 wchar_t *wname, *wassignment;
563 g_return_if_fail (variable != NULL);
564 g_return_if_fail (strchr (variable, '=') == NULL);
565 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
567 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
568 tem = g_strconcat (variable, "=", NULL);
569 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
572 _wputenv (wassignment);
573 g_free (wassignment);
575 SetEnvironmentVariableW (wname, NULL);
587 p = (wchar_t *) GetEnvironmentStringsW ();
597 result = g_new0 (gchar *, len + 1);
603 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
604 if (result[j] != NULL)
606 eq = strchr (result[j], '=');
607 if (eq && eq > result[j])
618 FreeEnvironmentStringsW (p);
630 strings = GetEnvironmentStringsW ();
631 for (n = 0, i = 0; strings[n]; i++)
632 n += wcslen (strings + n) + 1;
634 result = g_new (char *, i + 1);
635 for (n = 0, i = 0; strings[n]; i++)
637 result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
638 n += wcslen (strings + n) + 1;
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: */