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 : 134572 : 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 : 22603 : return g_ascii_strncasecmp (env, variable, len) == 0 && env[len] == '=';
57 : : #else
58 : 111969 : return strncmp (env, variable, len) == 0 && env[len] == '=';
59 : : #endif
60 : : }
61 : :
62 : : static gint
63 : 521 : g_environ_find (gchar **envp,
64 : : const gchar *variable)
65 : : {
66 : : gsize len;
67 : : gint i;
68 : :
69 : 521 : if (envp == NULL)
70 : 6 : return -1;
71 : :
72 : 515 : len = strlen (variable);
73 : :
74 : 88074 : for (i = 0; envp[i]; i++)
75 : : {
76 : 87937 : if (g_environ_matches (envp[i], variable, len))
77 : 378 : return i;
78 : 7250 : }
79 : :
80 : 137 : return -1;
81 : 59 : }
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 : 187 : g_environ_getenv (gchar **envp,
102 : : const gchar *variable)
103 : : {
104 : : gint index;
105 : :
106 : 187 : g_return_val_if_fail (variable != NULL, NULL);
107 : :
108 : 185 : index = g_environ_find (envp, variable);
109 : 185 : if (index != -1)
110 : 165 : return envp[index] + strlen (variable) + 1;
111 : : else
112 : 20 : return NULL;
113 : 31 : }
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 : 342 : g_environ_setenv (gchar **envp,
136 : : const gchar *variable,
137 : : const gchar *value,
138 : : gboolean overwrite)
139 : : {
140 : : gint index;
141 : :
142 : 342 : g_return_val_if_fail (variable != NULL, NULL);
143 : 340 : g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
144 : 338 : g_return_val_if_fail (value != NULL, NULL);
145 : :
146 : 336 : index = g_environ_find (envp, variable);
147 : 336 : if (index != -1)
148 : : {
149 : 213 : if (overwrite)
150 : : {
151 : 209 : g_free (envp[index]);
152 : 209 : envp[index] = g_strdup_printf ("%s=%s", variable, value);
153 : 12 : }
154 : 14 : }
155 : : else
156 : : {
157 : : guint length;
158 : :
159 : 123 : length = envp ? g_strv_length (envp) : 0;
160 : 123 : envp = g_renew (gchar *, envp, length + 2);
161 : 123 : envp[length] = g_strdup_printf ("%s=%s", variable, value);
162 : 123 : envp[length + 1] = NULL;
163 : : }
164 : :
165 : 336 : return envp;
166 : 32 : }
167 : :
168 : : static gchar **
169 : 217 : g_environ_unsetenv_internal (gchar **envp,
170 : : const gchar *variable,
171 : : gboolean free_value)
172 : : {
173 : : gsize len;
174 : : gchar **e, **f;
175 : :
176 : 217 : len = strlen (variable);
177 : :
178 : : /* Note that we remove *all* environment entries for
179 : : * the variable name, not just the first.
180 : : */
181 : 217 : e = f = envp;
182 : 46852 : while (*e != NULL)
183 : : {
184 : 46635 : if (!g_environ_matches (*e, variable, len))
185 : : {
186 : 46519 : *f = *e;
187 : 46519 : f++;
188 : 15278 : }
189 : : else
190 : : {
191 : 116 : if (free_value)
192 : 116 : g_free (*e);
193 : : }
194 : :
195 : 46635 : e++;
196 : : }
197 : 217 : *f = NULL;
198 : :
199 : 217 : 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 : 221 : g_environ_unsetenv (gchar **envp,
221 : : const gchar *variable)
222 : : {
223 : 221 : g_return_val_if_fail (variable != NULL, NULL);
224 : 219 : g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
225 : :
226 : 219 : if (envp == NULL)
227 : 2 : return NULL;
228 : :
229 : 217 : return g_environ_unsetenv_internal (envp, variable, TRUE);
230 : 65 : }
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 : 45911 : g_getenv (const gchar *variable)
254 : : {
255 : 45911 : g_return_val_if_fail (variable != NULL, NULL);
256 : :
257 : 45911 : 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 : 1526 : 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 : 1526 : g_return_val_if_fail (variable != NULL, FALSE);
302 : 1525 : g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
303 : 1524 : 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 : 1523 : if (g_thread_n_created () > 0)
309 : 290 : 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 : 1523 : 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 : 1523 : return result == 0;
326 : : }
327 : :
328 : : #ifdef HAVE__NSGETENVIRON
329 : : #define environ (*_NSGetEnviron())
330 : : #elif defined(__FreeBSD__)
331 : : /* FreeBSD has environ in crt rather than libc. Using "extern char** environ"
332 : : * in the code of a shared library makes it fail to link with -Wl,--no-undefined
333 : : * See https://reviews.freebsd.org/D30842#840642
334 : : */
335 : : #include <dlfcn.h>
336 : : #define environ (*((char***)dlsym(RTLD_DEFAULT, "environ")))
337 : : #else
338 : : /* According to the Single Unix Specification, environ is not
339 : : * in any system header, although unistd.h often declares it.
340 : : */
341 : : extern char **environ;
342 : : #endif
343 : :
344 : : /**
345 : : * g_unsetenv:
346 : : * @variable: (type filename): the environment variable to remove, must
347 : : * not contain '='
348 : : *
349 : : * Removes an environment variable from the environment.
350 : : *
351 : : * Note that on some systems, when variables are overwritten, the
352 : : * memory used for the previous variables and its value isn't reclaimed.
353 : : *
354 : : * You should be mindful of the fact that environment variable handling
355 : : * in UNIX is not thread-safe, and your program may crash if one thread
356 : : * calls g_unsetenv() while another thread is calling getenv(). (And note
357 : : * that many functions, such as gettext(), call getenv() internally.) This
358 : : * function is only safe to use at the very start of your program, before
359 : : * creating any other threads (or creating objects that create worker
360 : : * threads of their own).
361 : : *
362 : : * If you need to set up the environment for a child process, you can
363 : : * use g_get_environ() to get an environment array, modify that with
364 : : * g_environ_setenv() and g_environ_unsetenv(), and then pass that
365 : : * array directly to execvpe(), g_spawn_async(), or the like.
366 : : *
367 : : * Since: 2.4
368 : : */
369 : : void
370 : 1031 : g_unsetenv (const gchar *variable)
371 : : {
372 : 1031 : g_return_if_fail (variable != NULL);
373 : 1030 : g_return_if_fail (strchr (variable, '=') == NULL);
374 : :
375 : : #ifndef G_DISABLE_CHECKS
376 : : /* FIXME: This will be upgraded to a g_warning() in a future release of GLib.
377 : : * See https://gitlab.gnome.org/GNOME/glib/issues/715 */
378 : 1029 : if (g_thread_n_created () > 0)
379 : 745 : g_debug ("unsetenv() is not thread-safe and should not be used after threads are created");
380 : : #endif
381 : :
382 : : #ifdef HAVE_UNSETENV
383 : 1029 : unsetenv (variable);
384 : : #else /* !HAVE_UNSETENV */
385 : : /* Mess directly with the environ array.
386 : : * This seems to be the only portable way to do this.
387 : : */
388 : : g_environ_unsetenv_internal (environ, variable, FALSE);
389 : : #endif /* !HAVE_UNSETENV */
390 : : }
391 : :
392 : : /**
393 : : * g_listenv:
394 : : *
395 : : * Gets the names of all variables set in the environment.
396 : : *
397 : : * Programs that want to be portable to Windows should typically use
398 : : * this function and g_getenv() instead of using the environ array
399 : : * from the C library directly. On Windows, the strings in the environ
400 : : * array are in system codepage encoding, while in most of the typical
401 : : * use cases for environment variables in GLib-using programs you want
402 : : * the UTF-8 encoding that this function and g_getenv() provide.
403 : : *
404 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
405 : : * a %NULL-terminated list of strings which must be freed with
406 : : * g_strfreev().
407 : : *
408 : : * Since: 2.8
409 : : */
410 : : gchar **
411 : 1 : g_listenv (void)
412 : : {
413 : : gchar **result, *eq;
414 : : guint len, i, j;
415 : :
416 : 1 : len = g_strv_length (environ);
417 : 1 : result = g_new0 (gchar *, len + 1);
418 : :
419 : 1 : j = 0;
420 : 202 : for (i = 0; i < len; i++)
421 : : {
422 : 201 : eq = strchr (environ[i], '=');
423 : 201 : if (eq)
424 : 201 : result[j++] = g_strndup (environ[i], eq - environ[i]);
425 : : }
426 : :
427 : 1 : result[j] = NULL;
428 : :
429 : 1 : return result;
430 : : }
431 : :
432 : : /**
433 : : * g_get_environ:
434 : : *
435 : : * Gets the list of environment variables for the current process.
436 : : *
437 : : * The list is %NULL terminated and each item in the list is of the
438 : : * form 'NAME=VALUE'.
439 : : *
440 : : * This is equivalent to direct access to the 'environ' global variable,
441 : : * except portable.
442 : : *
443 : : * The return value is freshly allocated and it should be freed with
444 : : * g_strfreev() when it is no longer needed.
445 : : *
446 : : * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
447 : : * the list of environment variables
448 : : *
449 : : * Since: 2.28
450 : : */
451 : : gchar **
452 : 209 : g_get_environ (void)
453 : : {
454 : 209 : return g_strdupv (environ);
455 : : }
456 : :
457 : : /* Win32 implementation {{{1 */
458 : :
459 : : #else /* G_OS_WIN32 */
460 : :
461 : : static wchar_t *
462 : 5 : expand_environment_string (const wchar_t *string_utf16)
463 : : {
464 : 5 : wchar_t *expanded = NULL;
465 : 5 : DWORD previous_wchars_count = 0;
466 : 5 : DWORD wchars_count = 0;
467 : :
468 : 5 : do
469 : : {
470 : 10 : previous_wchars_count = wchars_count;
471 : 10 : expanded = g_renew (wchar_t, expanded, wchars_count);
472 : :
473 : : /* Note: ExpandEnvironmentStrings is 1-pass only. In addition, placeholders
474 : : * referring to non-existing variables are kept as-is, they aren't removed.
475 : : * That differs e.g from CMD. */
476 : 10 : wchars_count = ExpandEnvironmentStrings (string_utf16, expanded, wchars_count);
477 : 10 : }
478 : 10 : while (wchars_count > previous_wchars_count);
479 : :
480 : 5 : if (wchars_count == 0)
481 : : {
482 : 0 : g_warning ("%s failed with error code %u",
483 : 0 : "ExpandEnvironmentStrings", (unsigned int) GetLastError ());
484 : 0 : g_clear_pointer (&expanded, g_free);
485 : 0 : }
486 : :
487 : 5 : return expanded;
488 : : }
489 : :
490 : : const gchar *
491 : 34332 : g_getenv (const gchar *variable)
492 : : {
493 : 34332 : wchar_t *name_utf16 = NULL;
494 : 34332 : wchar_t *value_utf16 = NULL;
495 : 34332 : DWORD previous_wchars_count = 0;
496 : 34332 : DWORD wchars_count = 0;
497 : 34332 : char *value_utf8 = NULL;
498 : 34332 : GQuark quark = 0;
499 : :
500 : 34332 : g_return_val_if_fail (variable != NULL, NULL);
501 : 34332 : g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
502 : :
503 : 34332 : name_utf16 = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
504 : 34332 : g_assert (name_utf16);
505 : :
506 : 34332 : do
507 : : {
508 : 37859 : value_utf16 = g_renew (wchar_t, value_utf16, wchars_count);
509 : :
510 : 37859 : previous_wchars_count = wchars_count;
511 : :
512 : 37859 : SetLastError (ERROR_SUCCESS);
513 : 37859 : wchars_count = GetEnvironmentVariable (name_utf16, value_utf16, wchars_count);
514 : 37859 : }
515 : 37859 : while (wchars_count > previous_wchars_count);
516 : :
517 : 34332 : if (wchars_count == 0)
518 : : {
519 : 30857 : DWORD code = GetLastError ();
520 : :
521 : 30857 : if (code == ERROR_SUCCESS)
522 : : {
523 : : /* Value is an empty string */
524 : 52 : quark = g_quark_from_static_string ("");
525 : 52 : }
526 : 30805 : else if (code == ERROR_ENVVAR_NOT_FOUND)
527 : : {
528 : : /* The variable doesn't exist */
529 : 30805 : }
530 : : else
531 : : {
532 : 0 : g_warning ("%s failed with error code %u",
533 : 0 : "GetEnvironmentVariable", (unsigned int) GetLastError ());
534 : : }
535 : 30857 : }
536 : : else
537 : : {
538 : 3475 : g_assert (wchars_count != previous_wchars_count);
539 : 3475 : g_assert (value_utf16[wchars_count] == L'\0');
540 : :
541 : : /* On Windows NT, it is relatively typical that environment
542 : : * variables contain references to other environment variables.
543 : : * If so, use ExpandEnvironmentStrings() */
544 : 3475 : if (wcschr (value_utf16, L'%'))
545 : : {
546 : 5 : wchar_t *expanded = expand_environment_string (value_utf16);
547 : :
548 : 5 : g_free (value_utf16);
549 : 5 : value_utf16 = expanded;
550 : 5 : }
551 : :
552 : 3475 : if (value_utf16)
553 : : {
554 : 3475 : value_utf8 = g_utf16_to_utf8 (value_utf16, -1, NULL, NULL, NULL);
555 : :
556 : 3475 : if (value_utf8 == NULL)
557 : 1 : g_warning ("Environment variable `%s' contains invalid UTF-16", variable);
558 : : else
559 : 3474 : quark = g_quark_from_string (value_utf8);
560 : 3475 : }
561 : : }
562 : :
563 : 34332 : g_free (name_utf16);
564 : 34332 : g_free (value_utf16);
565 : 34332 : g_free (value_utf8);
566 : :
567 : 34332 : return g_quark_to_string (quark);
568 : 34332 : }
569 : :
570 : : gboolean
571 : 732 : g_setenv (const gchar *variable,
572 : : const gchar *value,
573 : : gboolean overwrite)
574 : : {
575 : : gboolean retval;
576 : : wchar_t *wname, *wvalue, *wassignment;
577 : : gchar *tem;
578 : :
579 : 732 : g_return_val_if_fail (variable != NULL, FALSE);
580 : 731 : g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
581 : 730 : g_return_val_if_fail (value != NULL, FALSE);
582 : 729 : g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
583 : 729 : g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
584 : :
585 : 729 : if (!overwrite && g_getenv (variable) != NULL)
586 : 2 : return TRUE;
587 : :
588 : : /* We want to (if possible) set both the environment variable copy
589 : : * kept by the C runtime and the one kept by the system.
590 : : *
591 : : * We can't use only the C runtime's putenv or _wputenv() as that
592 : : * won't work for arbitrary Unicode strings in a "non-Unicode" app
593 : : * (with main() and not wmain()). In a "main()" app the C runtime
594 : : * initializes the C runtime's environment table by converting the
595 : : * real (wide char) environment variables to system codepage, thus
596 : : * breaking those that aren't representable in the system codepage.
597 : : *
598 : : * As the C runtime's putenv() will also set the system copy, we do
599 : : * the putenv() first, then call SetEnvironmentValueW ourselves.
600 : : */
601 : :
602 : 727 : wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
603 : 727 : wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
604 : 727 : tem = g_strconcat (variable, "=", value, NULL);
605 : 727 : wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
606 : :
607 : 727 : g_free (tem);
608 : 727 : _wputenv (wassignment);
609 : 727 : g_free (wassignment);
610 : :
611 : 727 : retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
612 : :
613 : 727 : g_free (wname);
614 : 727 : g_free (wvalue);
615 : :
616 : 727 : return retval;
617 : 732 : }
618 : :
619 : : void
620 : 30 : g_unsetenv (const gchar *variable)
621 : : {
622 : : wchar_t *wname, *wassignment;
623 : : gchar *tem;
624 : :
625 : 30 : g_return_if_fail (variable != NULL);
626 : 29 : g_return_if_fail (strchr (variable, '=') == NULL);
627 : 28 : g_return_if_fail (g_utf8_validate (variable, -1, NULL));
628 : :
629 : 28 : wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
630 : 28 : tem = g_strconcat (variable, "=", NULL);
631 : 28 : wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
632 : :
633 : 28 : g_free (tem);
634 : 28 : _wputenv (wassignment);
635 : 28 : g_free (wassignment);
636 : :
637 : 28 : SetEnvironmentVariableW (wname, NULL);
638 : :
639 : 28 : g_free (wname);
640 : 30 : }
641 : :
642 : : gchar **
643 : 1 : g_listenv (void)
644 : : {
645 : : gchar **result, *eq;
646 : 1 : size_t len = 0, j;
647 : : wchar_t *p, *q;
648 : :
649 : 1 : p = (wchar_t *) GetEnvironmentStringsW ();
650 : 1 : if (p != NULL)
651 : : {
652 : 1 : q = p;
653 : 254 : while (*q)
654 : : {
655 : 253 : q += wcslen (q) + 1;
656 : 253 : len++;
657 : : }
658 : 1 : }
659 : 1 : result = g_new0 (gchar *, len + 1);
660 : :
661 : 1 : j = 0;
662 : 1 : q = p;
663 : 254 : while (*q)
664 : : {
665 : 253 : result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
666 : 253 : if (result[j] != NULL)
667 : : {
668 : 253 : eq = strchr (result[j], '=');
669 : 253 : if (eq && eq > result[j])
670 : : {
671 : 253 : *eq = '\0';
672 : 253 : j++;
673 : 253 : }
674 : : else
675 : 0 : g_free (result[j]);
676 : 253 : }
677 : 253 : q += wcslen (q) + 1;
678 : : }
679 : 1 : result[j] = NULL;
680 : 1 : FreeEnvironmentStringsW (p);
681 : :
682 : 1 : return result;
683 : : }
684 : :
685 : : gchar **
686 : 58 : g_get_environ (void)
687 : : {
688 : : gunichar2 *strings;
689 : : gchar **result;
690 : : size_t i, n;
691 : :
692 : 58 : strings = GetEnvironmentStringsW ();
693 : 14591 : for (n = 0, i = 0; strings[n]; i++)
694 : 14533 : n += wcslen (strings + n) + 1;
695 : :
696 : 58 : result = g_new (char *, i + 1);
697 : 14591 : for (n = 0, i = 0; strings[n]; i++)
698 : : {
699 : 14533 : result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
700 : 14533 : n += wcslen (strings + n) + 1;
701 : 14533 : }
702 : 58 : FreeEnvironmentStringsW (strings);
703 : 58 : result[i] = NULL;
704 : :
705 : 58 : return result;
706 : : }
707 : :
708 : : #endif /* G_OS_WIN32 */
709 : :
710 : : #ifdef G_OS_WIN32
711 : :
712 : : /* Binary compatibility versions. Not for newly compiled code. */
713 : :
714 : : _GLIB_EXTERN const gchar *g_getenv_utf8 (const gchar *variable);
715 : : _GLIB_EXTERN gboolean g_setenv_utf8 (const gchar *variable,
716 : : const gchar *value,
717 : : gboolean overwrite);
718 : : _GLIB_EXTERN void g_unsetenv_utf8 (const gchar *variable);
719 : :
720 : : const gchar *
721 : 0 : g_getenv_utf8 (const gchar *variable)
722 : : {
723 : 0 : return g_getenv (variable);
724 : : }
725 : :
726 : : gboolean
727 : 0 : g_setenv_utf8 (const gchar *variable,
728 : : const gchar *value,
729 : : gboolean overwrite)
730 : : {
731 : 0 : return g_setenv (variable, value, overwrite);
732 : : }
733 : :
734 : : void
735 : 0 : g_unsetenv_utf8 (const gchar *variable)
736 : : {
737 : 0 : g_unsetenv (variable);
738 : 0 : }
739 : :
740 : : #endif
741 : :
742 : : /* Epilogue {{{1 */
743 : : /* vim: set foldmethod=marker: */
|