genviron.c 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include "config.h"

29 30
#include "genviron.h"

31 32 33 34 35 36 37 38
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h> /* for _NSGetEnviron */
#endif
39 40 41
#ifdef G_OS_WIN32
#include <windows.h>
#endif
42

43
#include "glib-private.h"
44 45 46
#include "gmem.h"
#include "gmessages.h"
#include "gstrfuncs.h"
47 48 49
#include "gunicode.h"
#include "gconvert.h"
#include "gquark.h"
50

Matthias Clasen's avatar
Matthias Clasen committed
51 52 53 54 55 56 57
/* Environ array functions {{{1 */
static gint
g_environ_find (gchar       **envp,
                const gchar  *variable)
{
  gint len, i;

58 59 60
  if (envp == NULL)
    return -1;

Matthias Clasen's avatar
Matthias Clasen committed
61 62 63 64 65 66 67 68 69 70 71
  len = strlen (variable);

  for (i = 0; envp[i]; i++)
    {
      if (strncmp (envp[i], variable, len) == 0 &&
          envp[i][len] == '=')
        return i;
    }

  return -1;
}
72 73

/**
Matthias Clasen's avatar
Matthias Clasen committed
74
 * g_environ_getenv:
75 76 77
 * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
 *     list (eg, as returned from g_get_environ()), or %NULL
 *     for an empty environment list
78 79 80
 * @variable: the environment variable to get, in the GLib file name
 *     encoding
 *
Matthias Clasen's avatar
Matthias Clasen committed
81 82
 * Returns the value of the environment variable @variable in the
 * provided list @envp.
83
 *
Matthias Clasen's avatar
Matthias Clasen committed
84 85 86 87 88 89
 * The name and value are in the GLib file name encoding.
 * On UNIX, this means the actual bytes which might or might not
 * be in some consistent character set and encoding. On Windows,
 * it is in UTF-8. On Windows, in case the environment variable's
 * value contains references to other environment variables, they
 * are expanded.
90 91
 *
 * Return value: the value of the environment variable, or %NULL if
Matthias Clasen's avatar
Matthias Clasen committed
92 93 94 95 96
 *     the environment variable is not set in @envp. The returned
 *     string is owned by @envp, and will be freed if @variable is
 *     set or unset again.
 *
 * Since: 2.32
97 98
 */
const gchar *
Matthias Clasen's avatar
Matthias Clasen committed
99 100
g_environ_getenv (gchar       **envp,
                  const gchar  *variable)
101
{
Matthias Clasen's avatar
Matthias Clasen committed
102
  gint index;
103 104 105

  g_return_val_if_fail (variable != NULL, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
106 107 108 109 110 111
  index = g_environ_find (envp, variable);
  if (index != -1)
    return envp[index] + strlen (variable) + 1;
  else
    return NULL;
}
112

Matthias Clasen's avatar
Matthias Clasen committed
113 114
/**
 * g_environ_setenv:
115 116 117
 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
 *     list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
 *     for an empty environment list
Matthias Clasen's avatar
Matthias Clasen committed
118 119 120 121 122 123 124 125 126 127 128 129
 * @variable: the environment variable to set, must not contain '='
 * @value: the value for to set the variable to
 * @overwrite: whether to change the variable if it already exists
 *
 * Sets the environment variable @variable in the provided list
 * @envp to @value.
 *
 * Both the variable's name and value should be in the GLib
 * file name encoding. On UNIX, this means that they can be
 * arbitrary byte strings. On Windows, they should be in UTF-8.
 *
 * Return value: (array zero-terminated=1) (transfer full): the
130
 *     updated environment list. Free it using g_strfreev().
Matthias Clasen's avatar
Matthias Clasen committed
131 132 133 134 135 136 137 138 139 140
 *
 * Since: 2.32
 */
gchar **
g_environ_setenv (gchar       **envp,
                  const gchar  *variable,
                  const gchar  *value,
                  gboolean      overwrite)
{
  gint index;
141 142

  g_return_val_if_fail (variable != NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
143
  g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
144

Matthias Clasen's avatar
Matthias Clasen committed
145 146
  index = g_environ_find (envp, variable);
  if (index != -1)
147
    {
Matthias Clasen's avatar
Matthias Clasen committed
148 149 150 151 152
      if (overwrite)
        {
          g_free (envp[index]);
          envp[index] = g_strdup_printf ("%s=%s", variable, value);
        }
153
    }
Matthias Clasen's avatar
Matthias Clasen committed
154
  else
155
    {
Matthias Clasen's avatar
Matthias Clasen committed
156 157
      gint length;

158
      length = envp ? g_strv_length (envp) : 0;
Matthias Clasen's avatar
Matthias Clasen committed
159 160 161
      envp = g_renew (gchar *, envp, length + 2);
      envp[length] = g_strdup_printf ("%s=%s", variable, value);
      envp[length + 1] = NULL;
162 163
    }

Matthias Clasen's avatar
Matthias Clasen committed
164 165
  return envp;
}
166

167 168 169 170
static gchar **
g_environ_unsetenv_internal (gchar        **envp,
                             const gchar   *variable,
                             gboolean       free_value)
Matthias Clasen's avatar
Matthias Clasen committed
171 172 173
{
  gint len;
  gchar **e, **f;
174

Matthias Clasen's avatar
Matthias Clasen committed
175 176 177 178 179 180 181 182 183 184 185 186
  len = strlen (variable);

  /* Note that we remove *all* environment entries for
   * the variable name, not just the first.
   */
  e = f = envp;
  while (*e != NULL)
    {
      if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
        {
          *f = *e;
          f++;
187
        }
188 189 190 191 192 193
      else
        {
          if (free_value)
            g_free (*e);
        }

Matthias Clasen's avatar
Matthias Clasen committed
194
      e++;
195
    }
Matthias Clasen's avatar
Matthias Clasen committed
196
  *f = NULL;
197

Marc-Antoine Perennou's avatar
Marc-Antoine Perennou committed
198
  return envp;
Matthias Clasen's avatar
Matthias Clasen committed
199
}
200

201 202 203

/**
 * g_environ_unsetenv:
204 205 206
 * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
 *     list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), 
 *     or %NULL for an empty environment list
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
 * @variable: the environment variable to remove, must not contain '='
 *
 * Removes the environment variable @variable from the provided
 * environment @envp.
 *
 * Return value: (array zero-terminated=1) (transfer full): the
 *     updated environment list. Free it using g_strfreev().
 *
 * Since: 2.32
 */
gchar **
g_environ_unsetenv (gchar       **envp,
                    const gchar  *variable)
{
  g_return_val_if_fail (variable != NULL, NULL);
  g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);

224 225 226
  if (envp == NULL)
    return NULL;

227 228 229
  return g_environ_unsetenv_internal (envp, variable, TRUE);
}

Matthias Clasen's avatar
Matthias Clasen committed
230 231
/* UNIX implemention {{{1 */
#ifndef G_OS_WIN32
232

Matthias Clasen's avatar
Matthias Clasen committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/**
 * g_getenv:
 * @variable: the environment variable to get, in the GLib file name
 *     encoding
 *
 * Returns the value of an environment variable.
 *
 * The name and value are in the GLib file name encoding. On UNIX,
 * this means the actual bytes which might or might not be in some
 * consistent character set and encoding. On Windows, it is in UTF-8.
 * On Windows, in case the environment variable's value contains
 * references to other environment variables, they are expanded.
 *
 * Return value: the value of the environment variable, or %NULL if
 *     the environment variable is not found. The returned string
 *     may be overwritten by the next call to g_getenv(), g_setenv()
 *     or g_unsetenv().
 */
const gchar *
g_getenv (const gchar *variable)
{
  g_return_val_if_fail (variable != NULL, NULL);
255

Matthias Clasen's avatar
Matthias Clasen committed
256
  return getenv (variable);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
}

/**
 * g_setenv:
 * @variable: the environment variable to set, must not contain '='.
 * @value: the value for to set the variable to.
 * @overwrite: whether to change the variable if it already exists.
 *
 * Sets an environment variable. Both the variable's name and value
 * should be in the GLib file name encoding. On UNIX, this means that
 * they can be arbitrary byte strings. On Windows, they should be in
 * UTF-8.
 *
 * Note that on some systems, when variables are overwritten, the memory
 * used for the previous variables and its value isn't reclaimed.
 *
 * <warning><para>
 * Environment variable handling in UNIX is not thread-safe, and your
 * program may crash if one thread calls g_setenv() while another
 * thread is calling getenv(). (And note that many functions, such as
 * gettext(), call getenv() internally.) This function is only safe to
 * use at the very start of your program, before creating any other
 * threads (or creating objects that create worker threads of their
 * own).
 * </para><para>
 * If you need to set up the environment for a child process, you can
 * use g_get_environ() to get an environment array, modify that with
 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
 * array directly to execvpe(), g_spawn_async(), or the like.
 * </para></warning>
 *
 * Returns: %FALSE if the environment variable couldn't be set.
 *
 * Since: 2.4
 */
gboolean
g_setenv (const gchar *variable,
          const gchar *value,
          gboolean     overwrite)
{
  gint result;
#ifndef HAVE_SETENV
  gchar *string;
#endif

  g_return_val_if_fail (variable != NULL, FALSE);
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);

#ifdef HAVE_SETENV
  result = setenv (variable, value, overwrite);
#else
  if (!overwrite && getenv (variable) != NULL)
    return TRUE;

  /* This results in a leak when you overwrite existing
   * settings. It would be fairly easy to fix this by keeping
   * our own parallel array or hash table.
   */
  string = g_strconcat (variable, "=", value, NULL);
  result = putenv (string);
#endif
  return result == 0;
}

#ifdef HAVE__NSGETENVIRON
#define environ (*_NSGetEnviron())
Matthias Clasen's avatar
Matthias Clasen committed
323
#else
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/* According to the Single Unix Specification, environ is not
 * in any system header, although unistd.h often declares it.
 */
extern char **environ;
#endif

/**
 * g_unsetenv:
 * @variable: the environment variable to remove, must not contain '='
 *
 * Removes an environment variable from the environment.
 *
 * Note that on some systems, when variables are overwritten, the
 * memory used for the previous variables and its value isn't reclaimed.
 *
 * <warning><para>
 * Environment variable handling in UNIX is not thread-safe, and your
 * program may crash if one thread calls g_unsetenv() while another
 * thread is calling getenv(). (And note that many functions, such as
 * gettext(), call getenv() internally.) This function is only safe
 * to use at the very start of your program, before creating any other
 * threads (or creating objects that create worker threads of their
 * own).
 * </para><para>
 * If you need to set up the environment for a child process, you can
 * use g_get_environ() to get an environment array, modify that with
 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
 * array directly to execvpe(), g_spawn_async(), or the like.
 * </para></warning>
 *
 * Since: 2.4
 */
void
g_unsetenv (const gchar *variable)
{
Matthias Clasen's avatar
Matthias Clasen committed
359 360 361 362 363 364 365 366 367 368 369 370
#ifdef HAVE_UNSETENV
  g_return_if_fail (variable != NULL);
  g_return_if_fail (strchr (variable, '=') == NULL);

  unsetenv (variable);
#else /* !HAVE_UNSETENV */
  g_return_if_fail (variable != NULL);
  g_return_if_fail (strchr (variable, '=') == NULL);

  /* Mess directly with the environ array.
   * This seems to be the only portable way to do this.
   */
371
  g_environ_unsetenv_internal (environ, variable, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
#endif /* !HAVE_UNSETENV */
}

/**
 * g_listenv:
 *
 * Gets the names of all variables set in the environment.
 *
 * Programs that want to be portable to Windows should typically use
 * this function and g_getenv() instead of using the environ array
 * from the C library directly. On Windows, the strings in the environ
 * array are in system codepage encoding, while in most of the typical
 * use cases for environment variables in GLib-using programs you want
 * the UTF-8 encoding that this function and g_getenv() provide.
 *
 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated
 *     list of strings which must be freed with g_strfreev().
 *
 * Since: 2.8
 */
gchar **
g_listenv (void)
{
  gchar **result, *eq;
  gint len, i, j;

  len = g_strv_length (environ);
  result = g_new0 (gchar *, len + 1);

  j = 0;
  for (i = 0; i < len; i++)
    {
      eq = strchr (environ[i], '=');
      if (eq)
        result[j++] = g_strndup (environ[i], eq - environ[i]);
    }

  result[j] = NULL;

  return result;
}

/**
 * g_get_environ:
 *
 * Gets the list of environment variables for the current process.
 *
 * The list is %NULL terminated and each item in the list is of the
 * form 'NAME=VALUE'.
 *
 * This is equivalent to direct access to the 'environ' global variable,
 * except portable.
 *
 * The return value is freshly allocated and it should be freed with
 * g_strfreev() when it is no longer needed.
 *
 * Returns: (array zero-terminated=1) (transfer full): the list of
 *     environment variables
 *
 * Since: 2.28
 */
gchar **
g_get_environ (void)
{
  return g_strdupv (environ);
}

/* Win32 implementation {{{1 */
#else   /* G_OS_WIN32 */

const gchar *
g_getenv (const gchar *variable)
{
  GQuark quark;
  gchar *value;
  wchar_t dummy[2], *wname, *wvalue;
  int len;

  g_return_val_if_fail (variable != NULL, NULL);
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);

  /* On Windows NT, it is relatively typical that environment
   * variables contain references to other environment variables. If
   * so, use ExpandEnvironmentStrings(). (In an ideal world, such
   * environment variables would be stored in the Registry as
   * REG_EXPAND_SZ type values, and would then get automatically
   * expanded before a program sees them. But there is broken software
   * that stores environment variables as REG_SZ values even if they
   * contain references to other environment variables.)
   */

  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);

  len = GetEnvironmentVariableW (wname, dummy, 2);

  if (len == 0)
    {
      g_free (wname);
470 471 472 473 474
      if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
        return NULL;

      quark = g_quark_from_static_string ("");
      return g_quark_to_string (quark);
Matthias Clasen's avatar
Matthias Clasen committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    }
  else if (len == 1)
    len = 2;

  wvalue = g_new (wchar_t, len);

  if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
    {
      g_free (wname);
      g_free (wvalue);
      return NULL;
    }

  if (wcschr (wvalue, L'%') != NULL)
    {
      wchar_t *tem = wvalue;

      len = ExpandEnvironmentStringsW (wvalue, dummy, 2);

      if (len > 0)
        {
          wvalue = g_new (wchar_t, len);

          if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
            {
              g_free (wvalue);
              wvalue = tem;
            }
          else
            g_free (tem);
        }
    }

  value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);

  g_free (wname);
  g_free (wvalue);

  quark = g_quark_from_string (value);
  g_free (value);

  return g_quark_to_string (quark);
}

gboolean
g_setenv (const gchar *variable,
          const gchar *value,
          gboolean     overwrite)
{
  gboolean retval;
  wchar_t *wname, *wvalue, *wassignment;
  gchar *tem;

  g_return_val_if_fail (variable != NULL, FALSE);
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
  g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);

  if (!overwrite && g_getenv (variable) != NULL)
    return TRUE;

  /* We want to (if possible) set both the environment variable copy
   * kept by the C runtime and the one kept by the system.
   *
   * We can't use only the C runtime's putenv or _wputenv() as that
   * won't work for arbitrary Unicode strings in a "non-Unicode" app
   * (with main() and not wmain()). In a "main()" app the C runtime
   * initializes the C runtime's environment table by converting the
   * real (wide char) environment variables to system codepage, thus
   * breaking those that aren't representable in the system codepage.
   *
   * As the C runtime's putenv() will also set the system copy, we do
   * the putenv() first, then call SetEnvironmentValueW ourselves.
   */
549

Matthias Clasen's avatar
Matthias Clasen committed
550 551 552 553
  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
  wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
  tem = g_strconcat (variable, "=", value, NULL);
  wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
554

Matthias Clasen's avatar
Matthias Clasen committed
555 556 557
  g_free (tem);
  _wputenv (wassignment);
  g_free (wassignment);
558

Matthias Clasen's avatar
Matthias Clasen committed
559
  retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
560

Matthias Clasen's avatar
Matthias Clasen committed
561 562 563 564 565
  g_free (wname);
  g_free (wvalue);

  return retval;
}
566

Matthias Clasen's avatar
Matthias Clasen committed
567 568 569
void
g_unsetenv (const gchar *variable)
{
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  wchar_t *wname, *wassignment;
  gchar *tem;

  g_return_if_fail (variable != NULL);
  g_return_if_fail (strchr (variable, '=') == NULL);
  g_return_if_fail (g_utf8_validate (variable, -1, NULL));

  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
  tem = g_strconcat (variable, "=", NULL);
  wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);

  g_free (tem);
  _wputenv (wassignment);
  g_free (wassignment);

  SetEnvironmentVariableW (wname, NULL);

  g_free (wname);
}

gchar **
g_listenv (void)
{
  gchar **result, *eq;
  gint len = 0, j;
  wchar_t *p, *q;

  p = (wchar_t *) GetEnvironmentStringsW ();
  if (p != NULL)
    {
      q = p;
      while (*q)
        {
          q += wcslen (q) + 1;
          len++;
        }
    }
  result = g_new0 (gchar *, len + 1);

  j = 0;
  q = p;
  while (*q)
    {
      result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
      if (result[j] != NULL)
        {
          eq = strchr (result[j], '=');
          if (eq && eq > result[j])
            {
              *eq = '\0';
              j++;
            }
          else
            g_free (result[j]);
        }
      q += wcslen (q) + 1;
    }
  result[j] = NULL;
  FreeEnvironmentStringsW (p);

  return result;
}

gchar **
g_get_environ (void)
{
  gunichar2 *strings;
  gchar **result;
  gint i, n;

  strings = GetEnvironmentStringsW ();
641 642 643 644 645 646 647 648 649
  for (n = 0, i = 0; strings[n]; i++)
    n += wcslen (strings + n) + 1;

  result = g_new (char *, i + 1);
  for (n = 0, i = 0; strings[n]; i++)
    {
      result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
      n += wcslen (strings + n) + 1;
    }
650 651 652 653 654 655
  FreeEnvironmentStringsW (strings);
  result[i] = NULL;

  return result;
}

Matthias Clasen's avatar
Matthias Clasen committed
656
/* Win32 binary compatibility versions {{{1 */
Matthias Clasen's avatar
Matthias Clasen committed
657
#ifndef _WIN64
658

Matthias Clasen's avatar
Matthias Clasen committed
659
#undef g_getenv
660 661

const gchar *
Matthias Clasen's avatar
Matthias Clasen committed
662
g_getenv (const gchar *variable)
663
{
Matthias Clasen's avatar
Matthias Clasen committed
664 665 666 667
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
  const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
  gchar *value;
  GQuark quark;
668

Matthias Clasen's avatar
Matthias Clasen committed
669 670
  g_free (utf8_variable);
  if (!utf8_value)
671
    return NULL;
Matthias Clasen's avatar
Matthias Clasen committed
672 673 674
  value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
  quark = g_quark_from_string (value);
  g_free (value);
675

Matthias Clasen's avatar
Matthias Clasen committed
676 677
  return g_quark_to_string (quark);
}
678

Matthias Clasen's avatar
Matthias Clasen committed
679
#undef g_setenv
680

Matthias Clasen's avatar
Matthias Clasen committed
681 682 683 684 685 686 687 688
gboolean
g_setenv (const gchar *variable,
          const gchar *value,
          gboolean     overwrite)
{
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
  gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
  gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
689

Matthias Clasen's avatar
Matthias Clasen committed
690 691
  g_free (utf8_variable);
  g_free (utf8_value);
692

Matthias Clasen's avatar
Matthias Clasen committed
693
  return retval;
694 695
}

Matthias Clasen's avatar
Matthias Clasen committed
696 697 698 699
#undef g_unsetenv

void
g_unsetenv (const gchar *variable)
700
{
Matthias Clasen's avatar
Matthias Clasen committed
701
  gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
702

Matthias Clasen's avatar
Matthias Clasen committed
703
  g_unsetenv_utf8 (utf8_variable);
704

Matthias Clasen's avatar
Matthias Clasen committed
705 706
  g_free (utf8_variable);
}
707

Matthias Clasen's avatar
Matthias Clasen committed
708
#endif  /* _WIN64 */
709

Matthias Clasen's avatar
Matthias Clasen committed
710 711 712 713
#endif  /* G_OS_WIN32 */

/* Epilogue {{{1 */
/* vim: set foldmethod=marker: */