mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-01 13:23:07 +02:00
environ: Allow NULL envp
https://bugzilla.gnome.org/show_bug.cgi?id=676397
This commit is contained in:
parent
44d4990442
commit
6969b63827
@ -54,6 +54,9 @@ g_environ_find (gchar **envp,
|
|||||||
{
|
{
|
||||||
gint len, i;
|
gint len, i;
|
||||||
|
|
||||||
|
if (envp == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
len = strlen (variable);
|
len = strlen (variable);
|
||||||
|
|
||||||
for (i = 0; envp[i]; i++)
|
for (i = 0; envp[i]; i++)
|
||||||
@ -68,8 +71,9 @@ g_environ_find (gchar **envp,
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* g_environ_getenv:
|
* g_environ_getenv:
|
||||||
* @envp: (array zero-terminated=1) (transfer none): an environment
|
* @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
|
||||||
* list (eg, as returned from g_get_environ())
|
* list (eg, as returned from g_get_environ()), or %NULL
|
||||||
|
* for an empty environment list
|
||||||
* @variable: the environment variable to get, in the GLib file name
|
* @variable: the environment variable to get, in the GLib file name
|
||||||
* encoding
|
* encoding
|
||||||
*
|
*
|
||||||
@ -96,7 +100,6 @@ g_environ_getenv (gchar **envp,
|
|||||||
{
|
{
|
||||||
gint index;
|
gint index;
|
||||||
|
|
||||||
g_return_val_if_fail (envp != NULL, NULL);
|
|
||||||
g_return_val_if_fail (variable != NULL, NULL);
|
g_return_val_if_fail (variable != NULL, NULL);
|
||||||
|
|
||||||
index = g_environ_find (envp, variable);
|
index = g_environ_find (envp, variable);
|
||||||
@ -108,8 +111,9 @@ g_environ_getenv (gchar **envp,
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* g_environ_setenv:
|
* g_environ_setenv:
|
||||||
* @envp: (array zero-terminated=1) (transfer full): an environment
|
* @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())
|
* list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
|
||||||
|
* for an empty environment list
|
||||||
* @variable: the environment variable to set, must not contain '='
|
* @variable: the environment variable to set, must not contain '='
|
||||||
* @value: the value for to set the variable to
|
* @value: the value for to set the variable to
|
||||||
* @overwrite: whether to change the variable if it already exists
|
* @overwrite: whether to change the variable if it already exists
|
||||||
@ -134,7 +138,6 @@ g_environ_setenv (gchar **envp,
|
|||||||
{
|
{
|
||||||
gint index;
|
gint index;
|
||||||
|
|
||||||
g_return_val_if_fail (envp != NULL, NULL);
|
|
||||||
g_return_val_if_fail (variable != NULL, NULL);
|
g_return_val_if_fail (variable != NULL, NULL);
|
||||||
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
||||||
|
|
||||||
@ -151,7 +154,7 @@ g_environ_setenv (gchar **envp,
|
|||||||
{
|
{
|
||||||
gint length;
|
gint length;
|
||||||
|
|
||||||
length = g_strv_length (envp);
|
length = envp ? g_strv_length (envp) : 0;
|
||||||
envp = g_renew (gchar *, envp, length + 2);
|
envp = g_renew (gchar *, envp, length + 2);
|
||||||
envp[length] = g_strdup_printf ("%s=%s", variable, value);
|
envp[length] = g_strdup_printf ("%s=%s", variable, value);
|
||||||
envp[length + 1] = NULL;
|
envp[length + 1] = NULL;
|
||||||
@ -191,14 +194,18 @@ g_environ_unsetenv_internal (gchar **envp,
|
|||||||
}
|
}
|
||||||
*f = NULL;
|
*f = NULL;
|
||||||
|
|
||||||
|
if (free_value_and_realloc)
|
||||||
|
return g_renew (gchar *, envp, envc + 1);
|
||||||
|
else
|
||||||
return envp;
|
return envp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_environ_unsetenv:
|
* g_environ_unsetenv:
|
||||||
* @envp: (array zero-terminated=1) (transfer full): an environment
|
* @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())
|
* list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
|
||||||
|
* or %NULL for an empty environment list
|
||||||
* @variable: the environment variable to remove, must not contain '='
|
* @variable: the environment variable to remove, must not contain '='
|
||||||
*
|
*
|
||||||
* Removes the environment variable @variable from the provided
|
* Removes the environment variable @variable from the provided
|
||||||
@ -213,10 +220,12 @@ gchar **
|
|||||||
g_environ_unsetenv (gchar **envp,
|
g_environ_unsetenv (gchar **envp,
|
||||||
const gchar *variable)
|
const gchar *variable)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail (envp != NULL, NULL);
|
|
||||||
g_return_val_if_fail (variable != NULL, NULL);
|
g_return_val_if_fail (variable != NULL, NULL);
|
||||||
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
||||||
|
|
||||||
|
if (envp == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return g_environ_unsetenv_internal (envp, variable, TRUE);
|
return g_environ_unsetenv_internal (envp, variable, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +96,25 @@ test_environ_array (void)
|
|||||||
g_strfreev (env);
|
g_strfreev (env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_environ_null (void)
|
||||||
|
{
|
||||||
|
gchar **env;
|
||||||
|
const gchar *value;
|
||||||
|
|
||||||
|
env = NULL;
|
||||||
|
|
||||||
|
value = g_environ_getenv (env, "foo");
|
||||||
|
g_assert (value == NULL);
|
||||||
|
|
||||||
|
env = g_environ_setenv (NULL, "foo", "bar", TRUE);
|
||||||
|
g_assert (env != NULL);
|
||||||
|
g_strfreev (env);
|
||||||
|
|
||||||
|
env = g_environ_unsetenv (NULL, "foo");
|
||||||
|
g_assert (env == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -104,6 +123,7 @@ main (int argc, char **argv)
|
|||||||
g_test_add_func ("/environ/listenv", test_listenv);
|
g_test_add_func ("/environ/listenv", test_listenv);
|
||||||
g_test_add_func ("/environ/setenv", test_setenv);
|
g_test_add_func ("/environ/setenv", test_setenv);
|
||||||
g_test_add_func ("/environ/array", test_environ_array);
|
g_test_add_func ("/environ/array", test_environ_array);
|
||||||
|
g_test_add_func ("/environ/null", test_environ_null);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user