2011-10-16 00:23:07 +02:00
|
|
|
/* 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
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2011-10-16 00:23:07 +02:00
|
|
|
*
|
|
|
|
* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2011-10-16 00:23:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2011-10-17 03:13:22 +02:00
|
|
|
#include "genviron.h"
|
|
|
|
|
2011-10-16 00:23:07 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_CRT_EXTERNS_H
|
|
|
|
#include <crt_externs.h> /* for _NSGetEnviron */
|
|
|
|
#endif
|
2011-10-17 08:53:58 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2011-10-16 00:23:07 +02:00
|
|
|
|
CVE-2012-3524: Hardening for being run in a setuid environment
Some programs attempt to use libglib (or even libgio) when setuid.
For a long time, GTK+ simply aborted if launched in this
configuration, but we never had a real policy for GLib.
I'm not sure whether we should advertise such support. However, given
that there are real-world programs that do this currently, we can make
them safer with not too much effort.
Better to fix a problem caused by an interaction between two
components in *both* places if possible.
This patch adds a private function g_check_setuid() which is used to
first ensure we don't run an external dbus-launch binary if
DBUS_SESSION_BUS_ADDRESS isn't set.
Second, we also ensure the local VFS is used in this case. The
gdaemonvfs extension point will end up talking to the session bus
which is typically undesirable in a setuid context.
Implementing g_check_setuid() is interesting - whether or not we're
running in a privilege-escalated path is operating system specific.
Note that GTK+'s code to check euid versus uid worked historically on
Unix, more modern systems have filesystem capabilities and SELinux
domain transitions, neither of which are captured by the uid
comparison.
On Linux/glibc, the way this works is that the kernel sets an
AT_SECURE flag in the ELF auxiliary vector, and glibc looks for it on
startup. If found, then glibc sets a public-but-undocumented
__libc_enable_secure variable which we can use. Unfortunately, while
it *previously* worked to check this variable, a combination of newer
binutils and RPM break it:
http://www.openwall.com/lists/owl-dev/2012/08/14/1
So for now on Linux/glibc, we fall back to the historical Unix version
until we get glibc fixed.
On some BSD variants, there is a issetugid() function. On other Unix
variants, we fall back to what GTK+ has been doing.
Reported-By: Sebastian Krahmer <krahmer@suse.de>
Signed-off-by: Colin Walters <walters@verbum.org>
2012-08-22 20:26:11 +02:00
|
|
|
#include "glib-private.h"
|
2011-10-16 00:23:07 +02:00
|
|
|
#include "gmem.h"
|
|
|
|
#include "gmessages.h"
|
|
|
|
#include "gstrfuncs.h"
|
2011-10-17 08:53:58 +02:00
|
|
|
#include "gunicode.h"
|
|
|
|
#include "gconvert.h"
|
|
|
|
#include "gquark.h"
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
/* Environ array functions {{{1 */
|
2018-11-27 16:02:37 +01:00
|
|
|
static gboolean
|
|
|
|
g_environ_matches (const gchar *env, const gchar *variable, gsize len)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* TODO handle Unicode environment variable names */
|
|
|
|
/* Like filesystem paths, environment variables are case-insensitive. */
|
|
|
|
return g_ascii_strncasecmp (env, variable, len) == 0 && env[len] == '=';
|
|
|
|
#else
|
|
|
|
return strncmp (env, variable, len) == 0 && env[len] == '=';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
static gint
|
|
|
|
g_environ_find (gchar **envp,
|
|
|
|
const gchar *variable)
|
|
|
|
{
|
2018-11-27 16:02:37 +01:00
|
|
|
gsize len;
|
|
|
|
gint i;
|
2011-10-17 06:33:54 +02:00
|
|
|
|
2012-05-19 23:59:01 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
len = strlen (variable);
|
|
|
|
|
|
|
|
for (i = 0; envp[i]; i++)
|
|
|
|
{
|
2018-11-27 16:02:37 +01:00
|
|
|
if (g_environ_matches (envp[i], variable, len))
|
2011-10-17 06:33:54 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
|
|
|
/**
|
2011-10-17 06:33:54 +02:00
|
|
|
* g_environ_getenv:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @envp: (nullable) (array zero-terminated=1) (transfer none) (element-type filename):
|
|
|
|
* an environment list (eg, as returned from g_get_environ()), or %NULL
|
2012-05-19 23:59:01 +02:00
|
|
|
* for an empty environment list
|
2017-10-11 18:49:11 +02:00
|
|
|
* @variable: (type filename): the environment variable to get
|
2011-10-16 00:23:07 +02:00
|
|
|
*
|
2011-10-17 06:33:54 +02:00
|
|
|
* Returns the value of the environment variable @variable in the
|
|
|
|
* provided list @envp.
|
2011-10-16 00:23:07 +02:00
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (type filename): the value of the environment variable, or %NULL if
|
2011-10-17 06:33:54 +02:00
|
|
|
* 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
|
2011-10-16 00:23:07 +02:00
|
|
|
*/
|
|
|
|
const gchar *
|
2011-10-17 06:33:54 +02:00
|
|
|
g_environ_getenv (gchar **envp,
|
|
|
|
const gchar *variable)
|
2011-10-16 00:23:07 +02:00
|
|
|
{
|
2011-10-17 06:33:54 +02:00
|
|
|
gint index;
|
2011-10-16 00:23:07 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (variable != NULL, NULL);
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
index = g_environ_find (envp, variable);
|
|
|
|
if (index != -1)
|
|
|
|
return envp[index] + strlen (variable) + 1;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
/**
|
|
|
|
* g_environ_setenv:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @envp: (nullable) (array zero-terminated=1) (element-type filename) (transfer full):
|
|
|
|
* an environment list that can be freed using g_strfreev() (e.g., as
|
2013-10-19 17:31:06 +02:00
|
|
|
* returned from g_get_environ()), or %NULL for an empty
|
|
|
|
* environment list
|
2017-10-11 18:49:11 +02:00
|
|
|
* @variable: (type filename): the environment variable to set, must not
|
|
|
|
* contain '='
|
|
|
|
* @value: (type filename): the value for to set the variable to
|
2011-10-17 06:33:54 +02:00
|
|
|
* @overwrite: whether to change the variable if it already exists
|
|
|
|
*
|
|
|
|
* Sets the environment variable @variable in the provided list
|
|
|
|
* @envp to @value.
|
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (array zero-terminated=1) (element-type filename) (transfer full):
|
|
|
|
* the updated environment list. Free it using g_strfreev().
|
2011-10-17 06:33:54 +02:00
|
|
|
*
|
|
|
|
* Since: 2.32
|
|
|
|
*/
|
|
|
|
gchar **
|
|
|
|
g_environ_setenv (gchar **envp,
|
|
|
|
const gchar *variable,
|
|
|
|
const gchar *value,
|
|
|
|
gboolean overwrite)
|
|
|
|
{
|
|
|
|
gint index;
|
2011-10-16 00:23:07 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (variable != NULL, NULL);
|
2011-10-17 06:33:54 +02:00
|
|
|
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
2013-10-19 17:31:06 +02:00
|
|
|
g_return_val_if_fail (value != NULL, NULL);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
index = g_environ_find (envp, variable);
|
|
|
|
if (index != -1)
|
2011-10-16 00:23:07 +02:00
|
|
|
{
|
2011-10-17 06:33:54 +02:00
|
|
|
if (overwrite)
|
|
|
|
{
|
|
|
|
g_free (envp[index]);
|
|
|
|
envp[index] = g_strdup_printf ("%s=%s", variable, value);
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
2011-10-17 06:33:54 +02:00
|
|
|
else
|
2011-10-16 00:23:07 +02:00
|
|
|
{
|
2011-10-17 06:33:54 +02:00
|
|
|
gint length;
|
|
|
|
|
2012-05-19 23:59:01 +02:00
|
|
|
length = envp ? g_strv_length (envp) : 0;
|
2011-10-17 06:33:54 +02:00
|
|
|
envp = g_renew (gchar *, envp, length + 2);
|
|
|
|
envp[length] = g_strdup_printf ("%s=%s", variable, value);
|
|
|
|
envp[length + 1] = NULL;
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
return envp;
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2012-02-05 16:00:31 +01:00
|
|
|
static gchar **
|
|
|
|
g_environ_unsetenv_internal (gchar **envp,
|
|
|
|
const gchar *variable,
|
|
|
|
gboolean free_value)
|
2011-10-17 06:33:54 +02:00
|
|
|
{
|
2018-11-27 16:02:37 +01:00
|
|
|
gsize len;
|
2011-10-17 06:33:54 +02:00
|
|
|
gchar **e, **f;
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
len = strlen (variable);
|
|
|
|
|
|
|
|
/* Note that we remove *all* environment entries for
|
|
|
|
* the variable name, not just the first.
|
|
|
|
*/
|
|
|
|
e = f = envp;
|
|
|
|
while (*e != NULL)
|
|
|
|
{
|
2018-11-27 16:02:37 +01:00
|
|
|
if (!g_environ_matches (*e, variable, len))
|
2011-10-17 06:33:54 +02:00
|
|
|
{
|
|
|
|
*f = *e;
|
|
|
|
f++;
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
2012-02-05 16:00:31 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (free_value)
|
|
|
|
g_free (*e);
|
|
|
|
}
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
e++;
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
2011-10-17 06:33:54 +02:00
|
|
|
*f = NULL;
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2012-05-22 20:38:52 +02:00
|
|
|
return envp;
|
2011-10-17 06:33:54 +02:00
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2012-02-05 16:00:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_environ_unsetenv:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @envp: (nullable) (array zero-terminated=1) (element-type filename) (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
|
|
|
|
* @variable: (type filename): the environment variable to remove, must not
|
|
|
|
* contain '='
|
2012-02-05 16:00:31 +01:00
|
|
|
*
|
|
|
|
* Removes the environment variable @variable from the provided
|
|
|
|
* environment @envp.
|
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (array zero-terminated=1) (element-type filename) (transfer full):
|
|
|
|
* the updated environment list. Free it using g_strfreev().
|
2012-02-05 16:00:31 +01:00
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
2012-05-19 23:59:01 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2012-02-05 16:00:31 +01:00
|
|
|
return g_environ_unsetenv_internal (envp, variable, TRUE);
|
|
|
|
}
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
/* UNIX implemention {{{1 */
|
|
|
|
#ifndef G_OS_WIN32
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
/**
|
|
|
|
* g_getenv:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @variable: (type filename): the environment variable to get
|
2011-10-17 06:33:54 +02:00
|
|
|
*
|
|
|
|
* Returns the value of an environment variable.
|
|
|
|
*
|
2014-10-06 01:01:38 +02:00
|
|
|
* On UNIX, the name and value are byte strings which might or might not
|
|
|
|
* be in some consistent character set and encoding. On Windows, they are
|
|
|
|
* in UTF-8.
|
2011-10-17 06:33:54 +02:00
|
|
|
* On Windows, in case the environment variable's value contains
|
|
|
|
* references to other environment variables, they are expanded.
|
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (type filename): the value of the environment variable, or %NULL if
|
2011-10-17 06:33:54 +02:00
|
|
|
* 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);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
return getenv (variable);
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_setenv:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @variable: (type filename): the environment variable to set, must not
|
|
|
|
* contain '='.
|
|
|
|
* @value: (type filename): the value for to set the variable to.
|
2011-10-16 00:23:07 +02:00
|
|
|
* @overwrite: whether to change the variable if it already exists.
|
|
|
|
*
|
2014-10-06 01:01:38 +02:00
|
|
|
* Sets an environment variable. On UNIX, both the variable's name and
|
|
|
|
* value can be arbitrary byte strings, except that the variable's name
|
|
|
|
* cannot contain '='. On Windows, they should be in UTF-8.
|
2011-10-16 00:23:07 +02:00
|
|
|
*
|
|
|
|
* Note that on some systems, when variables are overwritten, the memory
|
|
|
|
* used for the previous variables and its value isn't reclaimed.
|
|
|
|
*
|
2014-05-05 12:30:56 +02:00
|
|
|
* You should be mindful of the fact that environment variable handling
|
2014-01-31 23:38:27 +01:00
|
|
|
* 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).
|
|
|
|
*
|
2011-10-16 00:23:07 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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);
|
2013-10-19 17:31:06 +02:00
|
|
|
g_return_val_if_fail (value != NULL, FALSE);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
|
|
|
#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())
|
2011-10-17 06:33:54 +02:00
|
|
|
#else
|
2011-10-16 00:23:07 +02:00
|
|
|
/* 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:
|
2017-10-11 18:49:11 +02:00
|
|
|
* @variable: (type filename): the environment variable to remove, must
|
|
|
|
* not contain '='
|
2011-10-16 00:23:07 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2014-01-31 23:38:27 +01:00
|
|
|
* You should be mindful of the fact that 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).
|
|
|
|
*
|
2011-10-16 00:23:07 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Since: 2.4
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_unsetenv (const gchar *variable)
|
|
|
|
{
|
2011-10-17 06:33:54 +02:00
|
|
|
g_return_if_fail (variable != NULL);
|
|
|
|
g_return_if_fail (strchr (variable, '=') == NULL);
|
|
|
|
|
2013-10-19 17:31:06 +02:00
|
|
|
#ifdef HAVE_UNSETENV
|
2011-10-17 06:33:54 +02:00
|
|
|
unsetenv (variable);
|
|
|
|
#else /* !HAVE_UNSETENV */
|
|
|
|
/* Mess directly with the environ array.
|
|
|
|
* This seems to be the only portable way to do this.
|
|
|
|
*/
|
2012-02-05 16:00:31 +01:00
|
|
|
g_environ_unsetenv_internal (environ, variable, FALSE);
|
2011-10-17 06:33:54 +02:00
|
|
|
#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.
|
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (array zero-terminated=1) (element-type filename) (transfer full):
|
|
|
|
* a %NULL-terminated list of strings which must be freed with
|
|
|
|
* g_strfreev().
|
2011-10-17 06:33:54 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2017-10-11 18:49:11 +02:00
|
|
|
* Returns: (array zero-terminated=1) (element-type filename) (transfer full):
|
|
|
|
* the list of environment variables
|
2011-10-17 06:33:54 +02:00
|
|
|
*
|
|
|
|
* 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);
|
2012-07-09 04:12:02 +02:00
|
|
|
if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
quark = g_quark_from_static_string ("");
|
|
|
|
return g_quark_to_string (quark);
|
2011-10-17 06:33:54 +02:00
|
|
|
}
|
|
|
|
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);
|
2013-10-19 17:31:06 +02:00
|
|
|
g_return_val_if_fail (value != NULL, FALSE);
|
2011-10-17 06:33:54 +02:00
|
|
|
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.
|
|
|
|
*/
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
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);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
g_free (tem);
|
|
|
|
_wputenv (wassignment);
|
|
|
|
g_free (wassignment);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
g_free (wname);
|
|
|
|
g_free (wvalue);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
void
|
|
|
|
g_unsetenv (const gchar *variable)
|
|
|
|
{
|
2011-10-16 00:23:07 +02:00
|
|
|
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 ();
|
2012-07-09 03:54:55 +02:00
|
|
|
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;
|
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
FreeEnvironmentStringsW (strings);
|
|
|
|
result[i] = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
#endif /* G_OS_WIN32 */
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
#ifdef G_OS_WIN32
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
/* Binary compatibility versions. Not for newly compiled code. */
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
_GLIB_EXTERN const gchar *g_getenv_utf8 (const gchar *variable);
|
|
|
|
_GLIB_EXTERN gboolean g_setenv_utf8 (const gchar *variable,
|
|
|
|
const gchar *value,
|
|
|
|
gboolean overwrite);
|
|
|
|
_GLIB_EXTERN void g_unsetenv_utf8 (const gchar *variable);
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
const gchar *
|
|
|
|
g_getenv_utf8 (const gchar *variable)
|
|
|
|
{
|
|
|
|
return g_getenv (variable);
|
2011-10-17 06:33:54 +02:00
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
gboolean
|
2017-03-28 08:00:46 +02:00
|
|
|
g_setenv_utf8 (const gchar *variable,
|
|
|
|
const gchar *value,
|
|
|
|
gboolean overwrite)
|
2011-10-17 06:33:54 +02:00
|
|
|
{
|
2017-03-28 08:00:46 +02:00
|
|
|
return g_setenv (variable, value, overwrite);
|
2011-10-16 00:23:07 +02:00
|
|
|
}
|
|
|
|
|
2011-10-17 06:33:54 +02:00
|
|
|
void
|
2017-03-28 08:00:46 +02:00
|
|
|
g_unsetenv_utf8 (const gchar *variable)
|
2011-10-16 00:23:07 +02:00
|
|
|
{
|
2018-01-10 18:24:36 +01:00
|
|
|
g_unsetenv (variable);
|
2011-10-17 06:33:54 +02:00
|
|
|
}
|
2011-10-16 00:23:07 +02:00
|
|
|
|
2017-03-28 08:00:46 +02:00
|
|
|
#endif
|
2011-10-17 06:33:54 +02:00
|
|
|
|
|
|
|
/* Epilogue {{{1 */
|
|
|
|
/* vim: set foldmethod=marker: */
|