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 that can be freed using g_strfreev() (e.g., 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 list. Free it using g_strfreev().
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_internal (gchar
**envp
,
165 const gchar
*variable
,
171 len
= strlen (variable
);
173 /* Note that we remove *all* environment entries for
174 * the variable name, not just the first.
179 if (strncmp (*e
, variable
, len
) != 0 || (*e
)[len
] != '=')
199 * g_environ_unsetenv:
200 * @envp: (array zero-terminated=1) (transfer full): an environment
201 * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ())
202 * @variable: the environment variable to remove, must not contain '='
204 * Removes the environment variable @variable from the provided
207 * Return value: (array zero-terminated=1) (transfer full): the
208 * updated environment list. Free it using g_strfreev().
213 g_environ_unsetenv (gchar
**envp
,
214 const gchar
*variable
)
216 g_return_val_if_fail (envp
!= NULL
, NULL
);
217 g_return_val_if_fail (variable
!= NULL
, NULL
);
218 g_return_val_if_fail (strchr (variable
, '=') == NULL
, NULL
);
220 return g_environ_unsetenv_internal (envp
, variable
, TRUE
);
223 /* UNIX implemention {{{1 */
228 * @variable: the environment variable to get, in the GLib file name
231 * Returns the value of an environment variable.
233 * The name and value are in the GLib file name encoding. On UNIX,
234 * this means the actual bytes which might or might not be in some
235 * consistent character set and encoding. On Windows, it is in UTF-8.
236 * On Windows, in case the environment variable's value contains
237 * references to other environment variables, they are expanded.
239 * Return value: the value of the environment variable, or %NULL if
240 * the environment variable is not found. The returned string
241 * may be overwritten by the next call to g_getenv(), g_setenv()
245 g_getenv (const gchar
*variable
)
247 g_return_val_if_fail (variable
!= NULL
, NULL
);
249 return getenv (variable
);
254 * @variable: the environment variable to set, must not contain '='.
255 * @value: the value for to set the variable to.
256 * @overwrite: whether to change the variable if it already exists.
258 * Sets an environment variable. Both the variable's name and value
259 * should be in the GLib file name encoding. On UNIX, this means that
260 * they can be arbitrary byte strings. On Windows, they should be in
263 * Note that on some systems, when variables are overwritten, the memory
264 * used for the previous variables and its value isn't reclaimed.
267 * Environment variable handling in UNIX is not thread-safe, and your
268 * program may crash if one thread calls g_setenv() while another
269 * thread is calling getenv(). (And note that many functions, such as
270 * gettext(), call getenv() internally.) This function is only safe to
271 * use at the very start of your program, before creating any other
272 * threads (or creating objects that create worker threads of their
275 * If you need to set up the environment for a child process, you can
276 * use g_get_environ() to get an environment array, modify that with
277 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
278 * array directly to execvpe(), g_spawn_async(), or the like.
281 * Returns: %FALSE if the environment variable couldn't be set.
286 g_setenv (const gchar
*variable
,
295 g_return_val_if_fail (variable
!= NULL
, FALSE
);
296 g_return_val_if_fail (strchr (variable
, '=') == NULL
, FALSE
);
299 result
= setenv (variable
, value
, overwrite
);
301 if (!overwrite
&& getenv (variable
) != NULL
)
304 /* This results in a leak when you overwrite existing
305 * settings. It would be fairly easy to fix this by keeping
306 * our own parallel array or hash table.
308 string
= g_strconcat (variable
, "=", value
, NULL
);
309 result
= putenv (string
);
314 #ifdef HAVE__NSGETENVIRON
315 #define environ (*_NSGetEnviron())
317 /* According to the Single Unix Specification, environ is not
318 * in any system header, although unistd.h often declares it.
320 extern char **environ
;
325 * @variable: the environment variable to remove, must not contain '='
327 * Removes an environment variable from the environment.
329 * Note that on some systems, when variables are overwritten, the
330 * memory used for the previous variables and its value isn't reclaimed.
333 * Environment variable handling in UNIX is not thread-safe, and your
334 * program may crash if one thread calls g_unsetenv() while another
335 * thread is calling getenv(). (And note that many functions, such as
336 * gettext(), call getenv() internally.) This function is only safe
337 * to use at the very start of your program, before creating any other
338 * threads (or creating objects that create worker threads of their
341 * If you need to set up the environment for a child process, you can
342 * use g_get_environ() to get an environment array, modify that with
343 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
344 * array directly to execvpe(), g_spawn_async(), or the like.
350 g_unsetenv (const gchar
*variable
)
353 g_return_if_fail (variable
!= NULL
);
354 g_return_if_fail (strchr (variable
, '=') == NULL
);
357 #else /* !HAVE_UNSETENV */
358 g_return_if_fail (variable
!= NULL
);
359 g_return_if_fail (strchr (variable
, '=') == NULL
);
361 /* Mess directly with the environ array.
362 * This seems to be the only portable way to do this.
364 g_environ_unsetenv_internal (environ
, variable
, FALSE
);
365 #endif /* !HAVE_UNSETENV */
371 * Gets the names of all variables set in the environment.
373 * Programs that want to be portable to Windows should typically use
374 * this function and g_getenv() instead of using the environ array
375 * from the C library directly. On Windows, the strings in the environ
376 * array are in system codepage encoding, while in most of the typical
377 * use cases for environment variables in GLib-using programs you want
378 * the UTF-8 encoding that this function and g_getenv() provide.
380 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
381 * list of strings which must be freed with g_strfreev().
391 len
= g_strv_length (environ
);
392 result
= g_new0 (gchar
*, len
+ 1);
395 for (i
= 0; i
< len
; i
++)
397 eq
= strchr (environ
[i
], '=');
399 result
[j
++] = g_strndup (environ
[i
], eq
- environ
[i
]);
410 * Gets the list of environment variables for the current process.
412 * The list is %NULL terminated and each item in the list is of the
415 * This is equivalent to direct access to the 'environ' global variable,
418 * The return value is freshly allocated and it should be freed with
419 * g_strfreev() when it is no longer needed.
421 * Returns: (array zero-terminated=1) (transfer full): the list of
422 * environment variables
429 return g_strdupv (environ
);
432 /* Win32 implementation {{{1 */
433 #else /* G_OS_WIN32 */
436 g_getenv (const gchar
*variable
)
440 wchar_t dummy
[2], *wname
, *wvalue
;
443 g_return_val_if_fail (variable
!= NULL
, NULL
);
444 g_return_val_if_fail (g_utf8_validate (variable
, -1, NULL
), NULL
);
446 /* On Windows NT, it is relatively typical that environment
447 * variables contain references to other environment variables. If
448 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
449 * environment variables would be stored in the Registry as
450 * REG_EXPAND_SZ type values, and would then get automatically
451 * expanded before a program sees them. But there is broken software
452 * that stores environment variables as REG_SZ values even if they
453 * contain references to other environment variables.)
456 wname
= g_utf8_to_utf16 (variable
, -1, NULL
, NULL
, NULL
);
458 len
= GetEnvironmentVariableW (wname
, dummy
, 2);
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 (g_utf8_validate (variable
, -1, NULL
), FALSE
);
520 g_return_val_if_fail (g_utf8_validate (value
, -1, NULL
), FALSE
);
522 if (!overwrite
&& g_getenv (variable
) != NULL
)
525 /* We want to (if possible) set both the environment variable copy
526 * kept by the C runtime and the one kept by the system.
528 * We can't use only the C runtime's putenv or _wputenv() as that
529 * won't work for arbitrary Unicode strings in a "non-Unicode" app
530 * (with main() and not wmain()). In a "main()" app the C runtime
531 * initializes the C runtime's environment table by converting the
532 * real (wide char) environment variables to system codepage, thus
533 * breaking those that aren't representable in the system codepage.
535 * As the C runtime's putenv() will also set the system copy, we do
536 * the putenv() first, then call SetEnvironmentValueW ourselves.
539 wname
= g_utf8_to_utf16 (variable
, -1, NULL
, NULL
, NULL
);
540 wvalue
= g_utf8_to_utf16 (value
, -1, NULL
, NULL
, NULL
);
541 tem
= g_strconcat (variable
, "=", value
, NULL
);
542 wassignment
= g_utf8_to_utf16 (tem
, -1, NULL
, NULL
, NULL
);
545 _wputenv (wassignment
);
546 g_free (wassignment
);
548 retval
= (SetEnvironmentVariableW (wname
, wvalue
) != 0);
557 g_unsetenv (const gchar
*variable
)
559 wchar_t *wname
, *wassignment
;
562 g_return_if_fail (variable
!= NULL
);
563 g_return_if_fail (strchr (variable
, '=') == NULL
);
564 g_return_if_fail (g_utf8_validate (variable
, -1, NULL
));
566 wname
= g_utf8_to_utf16 (variable
, -1, NULL
, NULL
, NULL
);
567 tem
= g_strconcat (variable
, "=", NULL
);
568 wassignment
= g_utf8_to_utf16 (tem
, -1, NULL
, NULL
, NULL
);
571 _wputenv (wassignment
);
572 g_free (wassignment
);
574 SetEnvironmentVariableW (wname
, NULL
);
586 p
= (wchar_t *) GetEnvironmentStringsW ();
596 result
= g_new0 (gchar
*, len
+ 1);
602 result
[j
] = g_utf16_to_utf8 (q
, -1, NULL
, NULL
, NULL
);
603 if (result
[j
] != NULL
)
605 eq
= strchr (result
[j
], '=');
606 if (eq
&& eq
> result
[j
])
617 FreeEnvironmentStringsW (p
);
629 strings
= GetEnvironmentStringsW ();
630 for (n
= 0; strings
[n
]; n
+= wcslen (strings
+ n
) + 1);
631 result
= g_new (char *, n
+ 1);
632 for (i
= 0; strings
[i
]; i
+= wcslen (strings
+ i
) + 1)
633 result
[i
] = g_utf16_to_utf8 (strings
+ i
, -1, NULL
, NULL
, NULL
);
634 FreeEnvironmentStringsW (strings
);
640 /* Win32 binary compatibility versions {{{1 */
646 g_getenv (const gchar
*variable
)
648 gchar
*utf8_variable
= g_locale_to_utf8 (variable
, -1, NULL
, NULL
, NULL
);
649 const gchar
*utf8_value
= g_getenv_utf8 (utf8_variable
);
653 g_free (utf8_variable
);
656 value
= g_locale_from_utf8 (utf8_value
, -1, NULL
, NULL
, NULL
);
657 quark
= g_quark_from_string (value
);
660 return g_quark_to_string (quark
);
666 g_setenv (const gchar
*variable
,
670 gchar
*utf8_variable
= g_locale_to_utf8 (variable
, -1, NULL
, NULL
, NULL
);
671 gchar
*utf8_value
= g_locale_to_utf8 (value
, -1, NULL
, NULL
, NULL
);
672 gboolean retval
= g_setenv_utf8 (utf8_variable
, utf8_value
, overwrite
);
674 g_free (utf8_variable
);
683 g_unsetenv (const gchar
*variable
)
685 gchar
*utf8_variable
= g_locale_to_utf8 (variable
, -1, NULL
, NULL
, NULL
);
687 g_unsetenv_utf8 (utf8_variable
);
689 g_free (utf8_variable
);
694 #endif /* G_OS_WIN32 */
697 /* vim: set foldmethod=marker: */