Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : */
19 : :
20 : : /*
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/.
25 : : */
26 : :
27 : : #include "config.h"
28 : :
29 : : #include "genviron.h"
30 : :
31 : : #include <stdlib.h>
32 : : #include <string.h>
33 : : #ifdef HAVE_CRT_EXTERNS_H
34 : : #include <crt_externs.h> /* for _NSGetEnviron */
35 : : #endif
36 : : #ifdef G_OS_WIN32
37 : : #include <windows.h>
38 : : #endif
39 : :
40 : : #include "glib-private.h"
41 : : #include "gmem.h"
42 : : #include "gmessages.h"
43 : : #include "gstrfuncs.h"
44 : : #include "gunicode.h"
45 : : #include "gconvert.h"
46 : : #include "gquark.h"
47 : : #include "gthreadprivate.h"
48 : :
49 : : /* Environ array functions {{{1 */
50 : : static gboolean
51 : 105671 : g_environ_matches (const gchar *env, const gchar *variable, gsize len)
52 : : {
53 : : #ifdef G_OS_WIN32
54 : : /* TODO handle Unicode environment variable names */
55 : : /* Like filesystem paths, environment variables are case-insensitive. */
56 : : return g_ascii_strncasecmp (env, variable, len) == 0 && env[len] == '=';
57 : : #else
58 : 105671 : return strncmp (env, variable, len) == 0 && env[len] == '=';
59 : : #endif
60 : : }
61 : :
62 : : static gint
63 : 461 : g_environ_find (gchar **envp,
64 : : const gchar *variable)
65 : : {
66 : : gsize len;
67 : : gint i;
68 : :
69 : 461 : if (envp == NULL)
70 : 3 : return -1;
71 : :
72 : 458 : len = strlen (variable);
73 : :
74 : 76131 : for (i = 0; envp[i]; i++)
75 : : {
76 : 76014 : if (g_environ_matches (envp[i], variable, len))
77 : 341 : return i;
78 : : }
79 : :
80 : 117 : return -1;
81 : : }
82 : :
83 : : /**
84 : : * g_environ_getenv:
85 : : * @envp: (nullable) (array zero-terminated=1) (transfer none) (element-type filename):
86 : : * an environment list (eg, as returned from g_get_environ()), or %NULL
87 : : * for an empty environment list
88 : : * @variable: (type filename): the environment variable to get
89 : : *
90 : : * Returns the value of the environment variable @variable in the
91 : : * provided list @envp.
92 : : *
93 : : * Returns: (type filename) (nullable): the value of the environment variable, or %NULL if
94 : : * the environment variable is not set in @envp. The returned
95 : : * string is owned by @envp, and will be freed if @variable is
96 : : * set or unset again.
97 : : *
98 : : * Since: 2.32
99 : : */
100 : : const gchar *
101 : 155 : g_environ_getenv (gchar **envp,
102 : : const gchar *variable)
103 : : {
104 : : gint index;
105 : :
106 : 155 : g_return_val_if_fail (variable != NULL, NULL);
107 : :
108 : 154 : index = g_environ_find (envp, variable);
109 : 154 : if (index != -1)
110 : 142 : return envp[index] + strlen (variable) + 1;
111 : : else
112 : 12 : return NULL;
113 : : }
114 : :
115 : : /**
116 : : * g_environ_setenv:
117 : : * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
118 : : * an environment list that can be freed using g_strfreev() (e.g., as
119 : : * returned from g_get_environ()), or %NULL for an empty
120 : : * environment list
121 : : * @variable: (type filename): the environment variable to set, must not
122 : : * contain '='
123 : : * @value: (type filename): the value for to set the variable to
124 : : * @overwrite: whether to change the variable if it already exists
125 : : *
126 : : * Sets the environment variable @variable in the provided list
127 : : * @envp to @value.
128 : : *
129 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
130 : : * the updated environment list. Free it using g_strfreev().
131 : : *
132 : : * Since: 2.32
133 : : */
134 : : gchar **
135 : 310 : g_environ_setenv (gchar **envp,
136 : : const gchar *variable,
137 : : const gchar *value,
138 : : gboolean overwrite)
139 : : {
140 : : gint index;
141 : :
142 : 310 : g_return_val_if_fail (variable != NULL, NULL);
143 : 309 : g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
144 : 308 : g_return_val_if_fail (value != NULL, NULL);
145 : :
146 : 307 : index = g_environ_find (envp, variable);
147 : 307 : if (index != -1)
148 : : {
149 : 199 : if (overwrite)
150 : : {
151 : 197 : g_free (envp[index]);
152 : 197 : envp[index] = g_strdup_printf ("%s=%s", variable, value);
153 : : }
154 : : }
155 : : else
156 : : {
157 : : gint length;
158 : :
159 : 108 : length = envp ? g_strv_length (envp) : 0;
160 : 108 : envp = g_renew (gchar *, envp, length + 2);
161 : 108 : envp[length] = g_strdup_printf ("%s=%s", variable, value);
162 : 108 : envp[length + 1] = NULL;
163 : : }
164 : :
165 : 307 : return envp;
166 : : }
167 : :
168 : : static gchar **
169 : 154 : g_environ_unsetenv_internal (gchar **envp,
170 : : const gchar *variable,
171 : : gboolean free_value)
172 : : {
173 : : gsize len;
174 : : gchar **e, **f;
175 : :
176 : 154 : len = strlen (variable);
177 : :
178 : : /* Note that we remove *all* environment entries for
179 : : * the variable name, not just the first.
180 : : */
181 : 154 : e = f = envp;
182 : 29811 : while (*e != NULL)
183 : : {
184 : 29657 : if (!g_environ_matches (*e, variable, len))
185 : : {
186 : 29580 : *f = *e;
187 : 29580 : f++;
188 : : }
189 : : else
190 : : {
191 : 77 : if (free_value)
192 : 77 : g_free (*e);
193 : : }
194 : :
195 : 29657 : e++;
196 : : }
197 : 154 : *f = NULL;
198 : :
199 : 154 : return envp;
200 : : }
201 : :
202 : :
203 : : /**
204 : : * g_environ_unsetenv:
205 : : * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
206 : : * an environment list that can be freed using g_strfreev() (e.g., as
207 : : * returned from g_get_environ()), or %NULL for an empty environment list
208 : : * @variable: (type filename): the environment variable to remove, must not
209 : : * contain '='
210 : : *
211 : : * Removes the environment variable @variable from the provided
212 : : * environment @envp.
213 : : *
214 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
215 : : * the updated environment list. Free it using g_strfreev().
216 : : *
217 : : * Since: 2.32
218 : : */
219 : : gchar **
220 : 156 : g_environ_unsetenv (gchar **envp,
221 : : const gchar *variable)
222 : : {
223 : 156 : g_return_val_if_fail (variable != NULL, NULL);
224 : 155 : g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
225 : :
226 : 155 : if (envp == NULL)
227 : 1 : return NULL;
228 : :
229 : 154 : return g_environ_unsetenv_internal (envp, variable, TRUE);
230 : : }
231 : :
232 : : /* UNIX implementation {{{1 */
233 : : #ifndef G_OS_WIN32
234 : :
235 : : /**
236 : : * g_getenv:
237 : : * @variable: (type filename): the environment variable to get
238 : : *
239 : : * Returns the value of an environment variable.
240 : : *
241 : : * On UNIX, the name and value are byte strings which might or might not
242 : : * be in some consistent character set and encoding. On Windows, they are
243 : : * in UTF-8.
244 : : * On Windows, in case the environment variable's value contains
245 : : * references to other environment variables, they are expanded.
246 : : *
247 : : * Returns: (type filename) (nullable): the value of the environment variable, or %NULL if
248 : : * the environment variable is not found. The returned string
249 : : * may be overwritten by the next call to g_getenv(), g_setenv()
250 : : * or g_unsetenv().
251 : : */
252 : : const gchar *
253 : 45112 : g_getenv (const gchar *variable)
254 : : {
255 : 45112 : g_return_val_if_fail (variable != NULL, NULL);
256 : :
257 : 45112 : return getenv (variable);
258 : : }
259 : :
260 : : /**
261 : : * g_setenv:
262 : : * @variable: (type filename): the environment variable to set, must not
263 : : * contain '='.
264 : : * @value: (type filename): the value for to set the variable to.
265 : : * @overwrite: whether to change the variable if it already exists.
266 : : *
267 : : * Sets an environment variable. On UNIX, both the variable's name and
268 : : * value can be arbitrary byte strings, except that the variable's name
269 : : * cannot contain '='. On Windows, they should be in UTF-8.
270 : : *
271 : : * Note that on some systems, when variables are overwritten, the memory
272 : : * used for the previous variables and its value isn't reclaimed.
273 : : *
274 : : * You should be mindful of the fact that environment variable handling
275 : : * in UNIX is not thread-safe, and your program may crash if one thread
276 : : * calls g_setenv() while another thread is calling getenv(). (And note
277 : : * that many functions, such as gettext(), call getenv() internally.)
278 : : * This function is only safe to use at the very start of your program,
279 : : * before creating any other threads (or creating objects that create
280 : : * worker threads of their own).
281 : : *
282 : : * If you need to set up the environment for a child process, you can
283 : : * use g_get_environ() to get an environment array, modify that with
284 : : * g_environ_setenv() and g_environ_unsetenv(), and then pass that
285 : : * array directly to execvpe(), g_spawn_async(), or the like.
286 : : *
287 : : * Returns: %FALSE if the environment variable couldn't be set.
288 : : *
289 : : * Since: 2.4
290 : : */
291 : : gboolean
292 : 1483 : g_setenv (const gchar *variable,
293 : : const gchar *value,
294 : : gboolean overwrite)
295 : : {
296 : : gint result;
297 : : #ifndef HAVE_SETENV
298 : : gchar *string;
299 : : #endif
300 : :
301 : 1483 : g_return_val_if_fail (variable != NULL, FALSE);
302 : 1482 : g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
303 : 1481 : g_return_val_if_fail (value != NULL, FALSE);
304 : :
305 : : #ifndef G_DISABLE_CHECKS
306 : : /* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
307 : : * See https://gitlab.gnome.org/GNOME/glib/issues/715 */
308 : 1480 : if (g_thread_n_created () > 0)
309 : 283 : g_debug ("setenv()/putenv() are not thread-safe and should not be used after threads are created");
310 : : #endif
311 : :
312 : : #ifdef HAVE_SETENV
313 : 1480 : result = setenv (variable, value, overwrite);
314 : : #else
315 : : if (!overwrite && getenv (variable) != NULL)
316 : : return TRUE;
317 : :
318 : : /* This results in a leak when you overwrite existing
319 : : * settings. It would be fairly easy to fix this by keeping
320 : : * our own parallel array or hash table.
321 : : */
322 : : string = g_strconcat (variable, "=", value, NULL);
323 : : result = putenv (string);
324 : : #endif
325 : 1480 : return result == 0;
326 : : }
327 : :
328 : : #ifdef HAVE__NSGETENVIRON
329 : : #define environ (*_NSGetEnviron())
330 : : #else
331 : : /* According to the Single Unix Specification, environ is not
332 : : * in any system header, although unistd.h often declares it.
333 : : */
334 : : extern char **environ;
335 : : #endif
336 : :
337 : : /**
338 : : * g_unsetenv:
339 : : * @variable: (type filename): the environment variable to remove, must
340 : : * not contain '='
341 : : *
342 : : * Removes an environment variable from the environment.
343 : : *
344 : : * Note that on some systems, when variables are overwritten, the
345 : : * memory used for the previous variables and its value isn't reclaimed.
346 : : *
347 : : * You should be mindful of the fact that environment variable handling
348 : : * in UNIX is not thread-safe, and your program may crash if one thread
349 : : * calls g_unsetenv() while another thread is calling getenv(). (And note
350 : : * that many functions, such as gettext(), call getenv() internally.) This
351 : : * function is only safe to use at the very start of your program, before
352 : : * creating any other threads (or creating objects that create worker
353 : : * threads of their own).
354 : : *
355 : : * If you need to set up the environment for a child process, you can
356 : : * use g_get_environ() to get an environment array, modify that with
357 : : * g_environ_setenv() and g_environ_unsetenv(), and then pass that
358 : : * array directly to execvpe(), g_spawn_async(), or the like.
359 : : *
360 : : * Since: 2.4
361 : : */
362 : : void
363 : 975 : g_unsetenv (const gchar *variable)
364 : : {
365 : 975 : g_return_if_fail (variable != NULL);
366 : 974 : g_return_if_fail (strchr (variable, '=') == NULL);
367 : :
368 : : #ifndef G_DISABLE_CHECKS
369 : : /* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
370 : : * See https://gitlab.gnome.org/GNOME/glib/issues/715 */
371 : 973 : if (g_thread_n_created () > 0)
372 : 739 : g_debug ("unsetenv() is not thread-safe and should not be used after threads are created");
373 : : #endif
374 : :
375 : : #ifdef HAVE_UNSETENV
376 : 973 : unsetenv (variable);
377 : : #else /* !HAVE_UNSETENV */
378 : : /* Mess directly with the environ array.
379 : : * This seems to be the only portable way to do this.
380 : : */
381 : : g_environ_unsetenv_internal (environ, variable, FALSE);
382 : : #endif /* !HAVE_UNSETENV */
383 : : }
384 : :
385 : : /**
386 : : * g_listenv:
387 : : *
388 : : * Gets the names of all variables set in the environment.
389 : : *
390 : : * Programs that want to be portable to Windows should typically use
391 : : * this function and g_getenv() instead of using the environ array
392 : : * from the C library directly. On Windows, the strings in the environ
393 : : * array are in system codepage encoding, while in most of the typical
394 : : * use cases for environment variables in GLib-using programs you want
395 : : * the UTF-8 encoding that this function and g_getenv() provide.
396 : : *
397 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
398 : : * a %NULL-terminated list of strings which must be freed with
399 : : * g_strfreev().
400 : : *
401 : : * Since: 2.8
402 : : */
403 : : gchar **
404 : 1 : g_listenv (void)
405 : : {
406 : : gchar **result, *eq;
407 : : gint len, i, j;
408 : :
409 : 1 : len = g_strv_length (environ);
410 : 1 : result = g_new0 (gchar *, len + 1);
411 : :
412 : 1 : j = 0;
413 : 191 : for (i = 0; i < len; i++)
414 : : {
415 : 190 : eq = strchr (environ[i], '=');
416 : 190 : if (eq)
417 : 190 : result[j++] = g_strndup (environ[i], eq - environ[i]);
418 : : }
419 : :
420 : 1 : result[j] = NULL;
421 : :
422 : 1 : return result;
423 : : }
424 : :
425 : : /**
426 : : * g_get_environ:
427 : : *
428 : : * Gets the list of environment variables for the current process.
429 : : *
430 : : * The list is %NULL terminated and each item in the list is of the
431 : : * form 'NAME=VALUE'.
432 : : *
433 : : * This is equivalent to direct access to the 'environ' global variable,
434 : : * except portable.
435 : : *
436 : : * The return value is freshly allocated and it should be freed with
437 : : * g_strfreev() when it is no longer needed.
438 : : *
439 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
440 : : * the list of environment variables
441 : : *
442 : : * Since: 2.28
443 : : */
444 : : gchar **
445 : 208 : g_get_environ (void)
446 : : {
447 : 208 : return g_strdupv (environ);
448 : : }
449 : :
450 : : /* Win32 implementation {{{1 */
451 : :
452 : : #else /* G_OS_WIN32 */
453 : :
454 : : static wchar_t *
455 : : expand_environment_string (const wchar_t *string_utf16)
456 : : {
457 : : wchar_t *expanded = NULL;
458 : : DWORD previous_wchars_count = 0;
459 : : DWORD wchars_count = 0;
460 : :
461 : : do
462 : : {
463 : : previous_wchars_count = wchars_count;
464 : : expanded = g_renew (wchar_t, expanded, wchars_count);
465 : :
466 : : /* Note: ExpandEnvironmentStrings is 1-pass only. In addition, placeholders
467 : : * referring to non-existing variables are kept as-is, they aren't removed.
468 : : * That differs e.g from CMD. */
469 : : wchars_count = ExpandEnvironmentStrings (string_utf16, expanded, wchars_count);
470 : : }
471 : : while (wchars_count > previous_wchars_count);
472 : :
473 : : if (wchars_count == 0)
474 : : {
475 : : g_warning ("%s failed with error code %u",
476 : : "ExpandEnvironmentStrings", (unsigned int) GetLastError ());
477 : : g_clear_pointer (&expanded, g_free);
478 : : }
479 : :
480 : : return expanded;
481 : : }
482 : :
483 : : const gchar *
484 : : g_getenv (const gchar *variable)
485 : : {
486 : : wchar_t *name_utf16 = NULL;
487 : : wchar_t *value_utf16 = NULL;
488 : : DWORD previous_wchars_count = 0;
489 : : DWORD wchars_count = 0;
490 : : char *value_utf8 = NULL;
491 : : GQuark quark = 0;
492 : :
493 : : g_return_val_if_fail (variable != NULL, NULL);
494 : : g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
495 : :
496 : : name_utf16 = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
497 : : g_assert (name_utf16);
498 : :
499 : : do
500 : : {
501 : : value_utf16 = g_renew (wchar_t, value_utf16, wchars_count);
502 : :
503 : : previous_wchars_count = wchars_count;
504 : :
505 : : SetLastError (ERROR_SUCCESS);
506 : : wchars_count = GetEnvironmentVariable (name_utf16, value_utf16, wchars_count);
507 : : }
508 : : while (wchars_count > previous_wchars_count);
509 : :
510 : : if (wchars_count == 0)
511 : : {
512 : : DWORD code = GetLastError ();
513 : :
514 : : if (code == ERROR_SUCCESS)
515 : : {
516 : : /* Value is an empty string */
517 : : quark = g_quark_from_static_string ("");
518 : : }
519 : : else if (code == ERROR_ENVVAR_NOT_FOUND)
520 : : {
521 : : /* The variable doesn't exist */
522 : : }
523 : : else
524 : : {
525 : : g_warning ("%s failed with error code %u",
526 : : "GetEnvironmentVariable", (unsigned int) GetLastError ());
527 : : }
528 : : }
529 : : else
530 : : {
531 : : g_assert (wchars_count != previous_wchars_count);
532 : : g_assert (value_utf16[wchars_count] == L'\0');
533 : :
534 : : /* On Windows NT, it is relatively typical that environment
535 : : * variables contain references to other environment variables.
536 : : * If so, use ExpandEnvironmentStrings() */
537 : : if (wcschr (value_utf16, L'%'))
538 : : {
539 : : wchar_t *expanded = expand_environment_string (value_utf16);
540 : :
541 : : g_free (value_utf16);
542 : : value_utf16 = expanded;
543 : : }
544 : :
545 : : if (value_utf16)
546 : : {
547 : : value_utf8 = g_utf16_to_utf8 (value_utf16, -1, NULL, NULL, NULL);
548 : :
549 : : if (value_utf8 == NULL)
550 : : g_warning ("Environment variable `%s' contains invalid UTF-16", variable);
551 : : else
552 : : quark = g_quark_from_string (value_utf8);
553 : : }
554 : : }
555 : :
556 : : g_free (name_utf16);
557 : : g_free (value_utf16);
558 : : g_free (value_utf8);
559 : :
560 : : return g_quark_to_string (quark);
561 : : }
562 : :
563 : : gboolean
564 : : g_setenv (const gchar *variable,
565 : : const gchar *value,
566 : : gboolean overwrite)
567 : : {
568 : : gboolean retval;
569 : : wchar_t *wname, *wvalue, *wassignment;
570 : : gchar *tem;
571 : :
572 : : g_return_val_if_fail (variable != NULL, FALSE);
573 : : g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
574 : : g_return_val_if_fail (value != NULL, FALSE);
575 : : g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
576 : : g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
577 : :
578 : : if (!overwrite && g_getenv (variable) != NULL)
579 : : return TRUE;
580 : :
581 : : /* We want to (if possible) set both the environment variable copy
582 : : * kept by the C runtime and the one kept by the system.
583 : : *
584 : : * We can't use only the C runtime's putenv or _wputenv() as that
585 : : * won't work for arbitrary Unicode strings in a "non-Unicode" app
586 : : * (with main() and not wmain()). In a "main()" app the C runtime
587 : : * initializes the C runtime's environment table by converting the
588 : : * real (wide char) environment variables to system codepage, thus
589 : : * breaking those that aren't representable in the system codepage.
590 : : *
591 : : * As the C runtime's putenv() will also set the system copy, we do
592 : : * the putenv() first, then call SetEnvironmentValueW ourselves.
593 : : */
594 : :
595 : : wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
596 : : wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
597 : : tem = g_strconcat (variable, "=", value, NULL);
598 : : wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
599 : :
600 : : g_free (tem);
601 : : _wputenv (wassignment);
602 : : g_free (wassignment);
603 : :
604 : : retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
605 : :
606 : : g_free (wname);
607 : : g_free (wvalue);
608 : :
609 : : return retval;
610 : : }
611 : :
612 : : void
613 : : g_unsetenv (const gchar *variable)
614 : : {
615 : : wchar_t *wname, *wassignment;
616 : : gchar *tem;
617 : :
618 : : g_return_if_fail (variable != NULL);
619 : : g_return_if_fail (strchr (variable, '=') == NULL);
620 : : g_return_if_fail (g_utf8_validate (variable, -1, NULL));
621 : :
622 : : wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
623 : : tem = g_strconcat (variable, "=", NULL);
624 : : wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
625 : :
626 : : g_free (tem);
627 : : _wputenv (wassignment);
628 : : g_free (wassignment);
629 : :
630 : : SetEnvironmentVariableW (wname, NULL);
631 : :
632 : : g_free (wname);
633 : : }
634 : :
635 : : gchar **
636 : : g_listenv (void)
637 : : {
638 : : gchar **result, *eq;
639 : : gint len = 0, j;
640 : : wchar_t *p, *q;
641 : :
642 : : p = (wchar_t *) GetEnvironmentStringsW ();
643 : : if (p != NULL)
644 : : {
645 : : q = p;
646 : : while (*q)
647 : : {
648 : : q += wcslen (q) + 1;
649 : : len++;
650 : : }
651 : : }
652 : : result = g_new0 (gchar *, len + 1);
653 : :
654 : : j = 0;
655 : : q = p;
656 : : while (*q)
657 : : {
658 : : result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
659 : : if (result[j] != NULL)
660 : : {
661 : : eq = strchr (result[j], '=');
662 : : if (eq && eq > result[j])
663 : : {
664 : : *eq = '\0';
665 : : j++;
666 : : }
667 : : else
668 : : g_free (result[j]);
669 : : }
670 : : q += wcslen (q) + 1;
671 : : }
672 : : result[j] = NULL;
673 : : FreeEnvironmentStringsW (p);
674 : :
675 : : return result;
676 : : }
677 : :
678 : : gchar **
679 : : g_get_environ (void)
680 : : {
681 : : gunichar2 *strings;
682 : : gchar **result;
683 : : size_t i, n;
684 : :
685 : : strings = GetEnvironmentStringsW ();
686 : : for (n = 0, i = 0; strings[n]; i++)
687 : : n += wcslen (strings + n) + 1;
688 : :
689 : : result = g_new (char *, i + 1);
690 : : for (n = 0, i = 0; strings[n]; i++)
691 : : {
692 : : result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
693 : : n += wcslen (strings + n) + 1;
694 : : }
695 : : FreeEnvironmentStringsW (strings);
696 : : result[i] = NULL;
697 : :
698 : : return result;
699 : : }
700 : :
701 : : #endif /* G_OS_WIN32 */
702 : :
703 : : #ifdef G_OS_WIN32
704 : :
705 : : /* Binary compatibility versions. Not for newly compiled code. */
706 : :
707 : : _GLIB_EXTERN const gchar *g_getenv_utf8 (const gchar *variable);
708 : : _GLIB_EXTERN gboolean g_setenv_utf8 (const gchar *variable,
709 : : const gchar *value,
710 : : gboolean overwrite);
711 : : _GLIB_EXTERN void g_unsetenv_utf8 (const gchar *variable);
712 : :
713 : : const gchar *
714 : : g_getenv_utf8 (const gchar *variable)
715 : : {
716 : : return g_getenv (variable);
717 : : }
718 : :
719 : : gboolean
720 : : g_setenv_utf8 (const gchar *variable,
721 : : const gchar *value,
722 : : gboolean overwrite)
723 : : {
724 : : return g_setenv (variable, value, overwrite);
725 : : }
726 : :
727 : : void
728 : : g_unsetenv_utf8 (const gchar *variable)
729 : : {
730 : : g_unsetenv (variable);
731 : : }
732 : :
733 : : #endif
734 : :
735 : : /* Epilogue {{{1 */
736 : : /* vim: set foldmethod=marker: */
|