Blame glib/genviron.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "genviron.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#ifdef HAVE_CRT_EXTERNS_H
Packit ae235b
#include <crt_externs.h> /* for _NSGetEnviron */
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-private.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gunicode.h"
Packit ae235b
#include "gconvert.h"
Packit ae235b
#include "gquark.h"
Packit ae235b
Packit ae235b
/* Environ array functions {{{1 */
Packit ae235b
static gint
Packit ae235b
g_environ_find (gchar       **envp,
Packit ae235b
                const gchar  *variable)
Packit ae235b
{
Packit ae235b
  gint len, i;
Packit ae235b
Packit ae235b
  if (envp == NULL)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  len = strlen (variable);
Packit ae235b
Packit ae235b
  for (i = 0; envp[i]; i++)
Packit ae235b
    {
Packit ae235b
      if (strncmp (envp[i], variable, len) == 0 &&
Packit ae235b
          envp[i][len] == '=')
Packit ae235b
        return i;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_environ_getenv:
Packit ae235b
 * @envp: (nullable) (array zero-terminated=1) (transfer none) (element-type filename):
Packit ae235b
 *     an environment list (eg, as returned from g_get_environ()), or %NULL
Packit ae235b
 *     for an empty environment list
Packit ae235b
 * @variable: (type filename): the environment variable to get
Packit ae235b
 *
Packit ae235b
 * Returns the value of the environment variable @variable in the
Packit ae235b
 * provided list @envp.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the value of the environment variable, or %NULL if
Packit ae235b
 *     the environment variable is not set in @envp. The returned
Packit ae235b
 *     string is owned by @envp, and will be freed if @variable is
Packit ae235b
 *     set or unset again.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_environ_getenv (gchar       **envp,
Packit ae235b
                  const gchar  *variable)
Packit ae235b
{
Packit ae235b
  gint index;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (variable != NULL, NULL);
Packit ae235b
Packit ae235b
  index = g_environ_find (envp, variable);
Packit ae235b
  if (index != -1)
Packit ae235b
    return envp[index] + strlen (variable) + 1;
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_environ_setenv:
Packit ae235b
 * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     an environment list that can be freed using g_strfreev() (e.g., as
Packit ae235b
 *     returned from g_get_environ()), or %NULL for an empty
Packit ae235b
 *     environment list
Packit ae235b
 * @variable: (type filename): the environment variable to set, must not
Packit ae235b
 *     contain '='
Packit ae235b
 * @value: (type filename): the value for to set the variable to
Packit ae235b
 * @overwrite: whether to change the variable if it already exists
Packit ae235b
 *
Packit ae235b
 * Sets the environment variable @variable in the provided list
Packit ae235b
 * @envp to @value.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     the updated environment list. Free it using g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_environ_setenv (gchar       **envp,
Packit ae235b
                  const gchar  *variable,
Packit ae235b
                  const gchar  *value,
Packit ae235b
                  gboolean      overwrite)
Packit ae235b
{
Packit ae235b
  gint index;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (variable != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
Packit ae235b
  g_return_val_if_fail (value != NULL, NULL);
Packit ae235b
Packit ae235b
  index = g_environ_find (envp, variable);
Packit ae235b
  if (index != -1)
Packit ae235b
    {
Packit ae235b
      if (overwrite)
Packit ae235b
        {
Packit ae235b
          g_free (envp[index]);
Packit ae235b
          envp[index] = g_strdup_printf ("%s=%s", variable, value);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gint length;
Packit ae235b
Packit ae235b
      length = envp ? g_strv_length (envp) : 0;
Packit ae235b
      envp = g_renew (gchar *, envp, length + 2);
Packit ae235b
      envp[length] = g_strdup_printf ("%s=%s", variable, value);
Packit ae235b
      envp[length + 1] = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return envp;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar **
Packit ae235b
g_environ_unsetenv_internal (gchar        **envp,
Packit ae235b
                             const gchar   *variable,
Packit ae235b
                             gboolean       free_value)
Packit ae235b
{
Packit ae235b
  gint len;
Packit ae235b
  gchar **e, **f;
Packit ae235b
Packit ae235b
  len = strlen (variable);
Packit ae235b
Packit ae235b
  /* Note that we remove *all* environment entries for
Packit ae235b
   * the variable name, not just the first.
Packit ae235b
   */
Packit ae235b
  e = f = envp;
Packit ae235b
  while (*e != NULL)
Packit ae235b
    {
Packit ae235b
      if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
Packit ae235b
        {
Packit ae235b
          *f = *e;
Packit ae235b
          f++;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (free_value)
Packit ae235b
            g_free (*e);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      e++;
Packit ae235b
    }
Packit ae235b
  *f = NULL;
Packit ae235b
Packit ae235b
  return envp;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_environ_unsetenv:
Packit ae235b
 * @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     an environment list that can be freed using g_strfreev() (e.g., as
Packit ae235b
 *     returned from g_get_environ()), or %NULL for an empty environment list
Packit ae235b
 * @variable: (type filename): the environment variable to remove, must not
Packit ae235b
 *     contain '='
Packit ae235b
 *
Packit ae235b
 * Removes the environment variable @variable from the provided
Packit ae235b
 * environment @envp.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     the updated environment list. Free it using g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_environ_unsetenv (gchar       **envp,
Packit ae235b
                    const gchar  *variable)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (variable != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
Packit ae235b
Packit ae235b
  if (envp == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return g_environ_unsetenv_internal (envp, variable, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* UNIX implemention {{{1 */
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_getenv:
Packit ae235b
 * @variable: (type filename): the environment variable to get
Packit ae235b
 *
Packit ae235b
 * Returns the value of an environment variable.
Packit ae235b
 *
Packit ae235b
 * On UNIX, the name and value are byte strings which might or might not
Packit ae235b
 * be in some consistent character set and encoding. On Windows, they are
Packit ae235b
 * in UTF-8.
Packit ae235b
 * On Windows, in case the environment variable's value contains
Packit ae235b
 * references to other environment variables, they are expanded.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the value of the environment variable, or %NULL if
Packit ae235b
 *     the environment variable is not found. The returned string
Packit ae235b
 *     may be overwritten by the next call to g_getenv(), g_setenv()
Packit ae235b
 *     or g_unsetenv().
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_getenv (const gchar *variable)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (variable != NULL, NULL);
Packit ae235b
Packit ae235b
  return getenv (variable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_setenv:
Packit ae235b
 * @variable: (type filename): the environment variable to set, must not
Packit ae235b
 *     contain '='.
Packit ae235b
 * @value: (type filename): the value for to set the variable to.
Packit ae235b
 * @overwrite: whether to change the variable if it already exists.
Packit ae235b
 *
Packit ae235b
 * Sets an environment variable. On UNIX, both the variable's name and
Packit ae235b
 * value can be arbitrary byte strings, except that the variable's name
Packit ae235b
 * cannot contain '='. On Windows, they should be in UTF-8.
Packit ae235b
 *
Packit ae235b
 * Note that on some systems, when variables are overwritten, the memory
Packit ae235b
 * used for the previous variables and its value isn't reclaimed.
Packit ae235b
 *
Packit ae235b
 * You should be mindful of the fact that environment variable handling
Packit ae235b
 * in UNIX is not thread-safe, and your program may crash if one thread
Packit ae235b
 * calls g_setenv() while another thread is calling getenv(). (And note
Packit ae235b
 * that many functions, such as gettext(), call getenv() internally.)
Packit ae235b
 * This function is only safe to use at the very start of your program,
Packit ae235b
 * before creating any other threads (or creating objects that create
Packit ae235b
 * worker threads of their own).
Packit ae235b
 *
Packit ae235b
 * If you need to set up the environment for a child process, you can
Packit ae235b
 * use g_get_environ() to get an environment array, modify that with
Packit ae235b
 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
Packit ae235b
 * array directly to execvpe(), g_spawn_async(), or the like.
Packit ae235b
 *
Packit ae235b
 * Returns: %FALSE if the environment variable couldn't be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_setenv (const gchar *variable,
Packit ae235b
          const gchar *value,
Packit ae235b
          gboolean     overwrite)
Packit ae235b
{
Packit ae235b
  gint result;
Packit ae235b
#ifndef HAVE_SETENV
Packit ae235b
  gchar *string;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (variable != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (value != NULL, FALSE);
Packit ae235b
Packit ae235b
#ifdef HAVE_SETENV
Packit ae235b
  result = setenv (variable, value, overwrite);
Packit ae235b
#else
Packit ae235b
  if (!overwrite && getenv (variable) != NULL)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  /* This results in a leak when you overwrite existing
Packit ae235b
   * settings. It would be fairly easy to fix this by keeping
Packit ae235b
   * our own parallel array or hash table.
Packit ae235b
   */
Packit ae235b
  string = g_strconcat (variable, "=", value, NULL);
Packit ae235b
  result = putenv (string);
Packit ae235b
#endif
Packit ae235b
  return result == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef HAVE__NSGETENVIRON
Packit ae235b
#define environ (*_NSGetEnviron())
Packit ae235b
#else
Packit ae235b
/* According to the Single Unix Specification, environ is not
Packit ae235b
 * in any system header, although unistd.h often declares it.
Packit ae235b
 */
Packit ae235b
extern char **environ;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unsetenv:
Packit ae235b
 * @variable: (type filename): the environment variable to remove, must
Packit ae235b
 *     not contain '='
Packit ae235b
 *
Packit ae235b
 * Removes an environment variable from the environment.
Packit ae235b
 *
Packit ae235b
 * Note that on some systems, when variables are overwritten, the
Packit ae235b
 * memory used for the previous variables and its value isn't reclaimed.
Packit ae235b
 *
Packit ae235b
 * You should be mindful of the fact that environment variable handling
Packit ae235b
 * in UNIX is not thread-safe, and your program may crash if one thread
Packit ae235b
 * calls g_unsetenv() while another thread is calling getenv(). (And note
Packit ae235b
 * that many functions, such as gettext(), call getenv() internally.) This
Packit ae235b
 * function is only safe to use at the very start of your program, before
Packit ae235b
 * creating any other threads (or creating objects that create worker
Packit ae235b
 * threads of their own).
Packit ae235b
 * 
Packit ae235b
 * If you need to set up the environment for a child process, you can
Packit ae235b
 * use g_get_environ() to get an environment array, modify that with
Packit ae235b
 * g_environ_setenv() and g_environ_unsetenv(), and then pass that
Packit ae235b
 * array directly to execvpe(), g_spawn_async(), or the like.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_unsetenv (const gchar *variable)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (variable != NULL);
Packit ae235b
  g_return_if_fail (strchr (variable, '=') == NULL);
Packit ae235b
Packit ae235b
#ifdef HAVE_UNSETENV
Packit ae235b
  unsetenv (variable);
Packit ae235b
#else /* !HAVE_UNSETENV */
Packit ae235b
  /* Mess directly with the environ array.
Packit ae235b
   * This seems to be the only portable way to do this.
Packit ae235b
   */
Packit ae235b
  g_environ_unsetenv_internal (environ, variable, FALSE);
Packit ae235b
#endif /* !HAVE_UNSETENV */
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_listenv:
Packit ae235b
 *
Packit ae235b
 * Gets the names of all variables set in the environment.
Packit ae235b
 *
Packit ae235b
 * Programs that want to be portable to Windows should typically use
Packit ae235b
 * this function and g_getenv() instead of using the environ array
Packit ae235b
 * from the C library directly. On Windows, the strings in the environ
Packit ae235b
 * array are in system codepage encoding, while in most of the typical
Packit ae235b
 * use cases for environment variables in GLib-using programs you want
Packit ae235b
 * the UTF-8 encoding that this function and g_getenv() provide.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     a %NULL-terminated list of strings which must be freed with
Packit ae235b
 *     g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_listenv (void)
Packit ae235b
{
Packit ae235b
  gchar **result, *eq;
Packit ae235b
  gint len, i, j;
Packit ae235b
Packit ae235b
  len = g_strv_length (environ);
Packit ae235b
  result = g_new0 (gchar *, len + 1);
Packit ae235b
Packit ae235b
  j = 0;
Packit ae235b
  for (i = 0; i < len; i++)
Packit ae235b
    {
Packit ae235b
      eq = strchr (environ[i], '=');
Packit ae235b
      if (eq)
Packit ae235b
        result[j++] = g_strndup (environ[i], eq - environ[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result[j] = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_environ:
Packit ae235b
 *
Packit ae235b
 * Gets the list of environment variables for the current process.
Packit ae235b
 *
Packit ae235b
 * The list is %NULL terminated and each item in the list is of the
Packit ae235b
 * form 'NAME=VALUE'.
Packit ae235b
 *
Packit ae235b
 * This is equivalent to direct access to the 'environ' global variable,
Packit ae235b
 * except portable.
Packit ae235b
 *
Packit ae235b
 * The return value is freshly allocated and it should be freed with
Packit ae235b
 * g_strfreev() when it is no longer needed.
Packit ae235b
 *
Packit ae235b
 * Returns: (array zero-terminated=1) (element-type filename) (transfer full):
Packit ae235b
 *     the list of environment variables
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_get_environ (void)
Packit ae235b
{
Packit ae235b
  return g_strdupv (environ);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Win32 implementation {{{1 */
Packit ae235b
#else   /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
const gchar *
Packit ae235b
g_getenv (const gchar *variable)
Packit ae235b
{
Packit ae235b
  GQuark quark;
Packit ae235b
  gchar *value;
Packit ae235b
  wchar_t dummy[2], *wname, *wvalue;
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (variable != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
Packit ae235b
Packit ae235b
  /* On Windows NT, it is relatively typical that environment
Packit ae235b
   * variables contain references to other environment variables. If
Packit ae235b
   * so, use ExpandEnvironmentStrings(). (In an ideal world, such
Packit ae235b
   * environment variables would be stored in the Registry as
Packit ae235b
   * REG_EXPAND_SZ type values, and would then get automatically
Packit ae235b
   * expanded before a program sees them. But there is broken software
Packit ae235b
   * that stores environment variables as REG_SZ values even if they
Packit ae235b
   * contain references to other environment variables.)
Packit ae235b
   */
Packit ae235b
Packit ae235b
  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  len = GetEnvironmentVariableW (wname, dummy, 2);
Packit ae235b
Packit ae235b
  if (len == 0)
Packit ae235b
    {
Packit ae235b
      g_free (wname);
Packit ae235b
      if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
Packit ae235b
        return NULL;
Packit ae235b
Packit ae235b
      quark = g_quark_from_static_string ("");
Packit ae235b
      return g_quark_to_string (quark);
Packit ae235b
    }
Packit ae235b
  else if (len == 1)
Packit ae235b
    len = 2;
Packit ae235b
Packit ae235b
  wvalue = g_new (wchar_t, len);
Packit ae235b
Packit ae235b
  if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
Packit ae235b
    {
Packit ae235b
      g_free (wname);
Packit ae235b
      g_free (wvalue);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (wcschr (wvalue, L'%') != NULL)
Packit ae235b
    {
Packit ae235b
      wchar_t *tem = wvalue;
Packit ae235b
Packit ae235b
      len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
Packit ae235b
Packit ae235b
      if (len > 0)
Packit ae235b
        {
Packit ae235b
          wvalue = g_new (wchar_t, len);
Packit ae235b
Packit ae235b
          if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
Packit ae235b
            {
Packit ae235b
              g_free (wvalue);
Packit ae235b
              wvalue = tem;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            g_free (tem);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_free (wname);
Packit ae235b
  g_free (wvalue);
Packit ae235b
Packit ae235b
  quark = g_quark_from_string (value);
Packit ae235b
  g_free (value);
Packit ae235b
Packit ae235b
  return g_quark_to_string (quark);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_setenv (const gchar *variable,
Packit ae235b
          const gchar *value,
Packit ae235b
          gboolean     overwrite)
Packit ae235b
{
Packit ae235b
  gboolean retval;
Packit ae235b
  wchar_t *wname, *wvalue, *wassignment;
Packit ae235b
  gchar *tem;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (variable != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (value != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
Packit ae235b
  g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
Packit ae235b
Packit ae235b
  if (!overwrite && g_getenv (variable) != NULL)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  /* We want to (if possible) set both the environment variable copy
Packit ae235b
   * kept by the C runtime and the one kept by the system.
Packit ae235b
   *
Packit ae235b
   * We can't use only the C runtime's putenv or _wputenv() as that
Packit ae235b
   * won't work for arbitrary Unicode strings in a "non-Unicode" app
Packit ae235b
   * (with main() and not wmain()). In a "main()" app the C runtime
Packit ae235b
   * initializes the C runtime's environment table by converting the
Packit ae235b
   * real (wide char) environment variables to system codepage, thus
Packit ae235b
   * breaking those that aren't representable in the system codepage.
Packit ae235b
   *
Packit ae235b
   * As the C runtime's putenv() will also set the system copy, we do
Packit ae235b
   * the putenv() first, then call SetEnvironmentValueW ourselves.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
Packit ae235b
  wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
Packit ae235b
  tem = g_strconcat (variable, "=", value, NULL);
Packit ae235b
  wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_free (tem);
Packit ae235b
  _wputenv (wassignment);
Packit ae235b
  g_free (wassignment);
Packit ae235b
Packit ae235b
  retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
Packit ae235b
Packit ae235b
  g_free (wname);
Packit ae235b
  g_free (wvalue);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_unsetenv (const gchar *variable)
Packit ae235b
{
Packit ae235b
  wchar_t *wname, *wassignment;
Packit ae235b
  gchar *tem;
Packit ae235b
Packit ae235b
  g_return_if_fail (variable != NULL);
Packit ae235b
  g_return_if_fail (strchr (variable, '=') == NULL);
Packit ae235b
  g_return_if_fail (g_utf8_validate (variable, -1, NULL));
Packit ae235b
Packit ae235b
  wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
Packit ae235b
  tem = g_strconcat (variable, "=", NULL);
Packit ae235b
  wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_free (tem);
Packit ae235b
  _wputenv (wassignment);
Packit ae235b
  g_free (wassignment);
Packit ae235b
Packit ae235b
  SetEnvironmentVariableW (wname, NULL);
Packit ae235b
Packit ae235b
  g_free (wname);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar **
Packit ae235b
g_listenv (void)
Packit ae235b
{
Packit ae235b
  gchar **result, *eq;
Packit ae235b
  gint len = 0, j;
Packit ae235b
  wchar_t *p, *q;
Packit ae235b
Packit ae235b
  p = (wchar_t *) GetEnvironmentStringsW ();
Packit ae235b
  if (p != NULL)
Packit ae235b
    {
Packit ae235b
      q = p;
Packit ae235b
      while (*q)
Packit ae235b
        {
Packit ae235b
          q += wcslen (q) + 1;
Packit ae235b
          len++;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  result = g_new0 (gchar *, len + 1);
Packit ae235b
Packit ae235b
  j = 0;
Packit ae235b
  q = p;
Packit ae235b
  while (*q)
Packit ae235b
    {
Packit ae235b
      result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
Packit ae235b
      if (result[j] != NULL)
Packit ae235b
        {
Packit ae235b
          eq = strchr (result[j], '=');
Packit ae235b
          if (eq && eq > result[j])
Packit ae235b
            {
Packit ae235b
              *eq = '\0';
Packit ae235b
              j++;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            g_free (result[j]);
Packit ae235b
        }
Packit ae235b
      q += wcslen (q) + 1;
Packit ae235b
    }
Packit ae235b
  result[j] = NULL;
Packit ae235b
  FreeEnvironmentStringsW (p);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar **
Packit ae235b
g_get_environ (void)
Packit ae235b
{
Packit ae235b
  gunichar2 *strings;
Packit ae235b
  gchar **result;
Packit ae235b
  gint i, n;
Packit ae235b
Packit ae235b
  strings = GetEnvironmentStringsW ();
Packit ae235b
  for (n = 0, i = 0; strings[n]; i++)
Packit ae235b
    n += wcslen (strings + n) + 1;
Packit ae235b
Packit ae235b
  result = g_new (char *, i + 1);
Packit ae235b
  for (n = 0, i = 0; strings[n]; i++)
Packit ae235b
    {
Packit ae235b
      result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
Packit ae235b
      n += wcslen (strings + n) + 1;
Packit ae235b
    }
Packit ae235b
  FreeEnvironmentStringsW (strings);
Packit ae235b
  result[i] = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif  /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN const gchar *g_getenv_utf8   (const gchar  *variable);
Packit ae235b
_GLIB_EXTERN gboolean     g_setenv_utf8   (const gchar  *variable,
Packit ae235b
                                           const gchar  *value,
Packit ae235b
                                           gboolean      overwrite);
Packit ae235b
_GLIB_EXTERN void         g_unsetenv_utf8 (const gchar  *variable);
Packit ae235b
Packit ae235b
const gchar *
Packit ae235b
g_getenv_utf8 (const gchar *variable)
Packit ae235b
{
Packit ae235b
  return g_getenv (variable);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_setenv_utf8 (const gchar *variable,
Packit ae235b
               const gchar *value,
Packit ae235b
               gboolean     overwrite)
Packit ae235b
{
Packit ae235b
  return g_setenv (variable, value, overwrite);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_unsetenv_utf8 (const gchar *variable)
Packit ae235b
{
Packit ae235b
  g_unsetenv (variable);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* Epilogue {{{1 */
Packit ae235b
/* vim: set foldmethod=marker: */