[3/4] Allow NULL to be passed to environment-related functions

Message ID 20241218-local-env-v1-3-ce333e6ea3bb@adacore.com
State New
Headers
Series Add commands to modify the "local" environment |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom Tromey Dec. 18, 2024, 4:29 p.m. UTC
  This changse the various environment-related helper functions to
accept a NULL environment pointer.  This special case means that the
function should affect gdb's global environment.
---
 gdb/infcmd.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)
  

Patch

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 3bd114be912735c700d1cbfca5b5680bb8ec1900..41954b5ea9210f1e78dda2986ec50bc9ee3f2492 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2014,7 +2014,7 @@  display_environment (const gdb_environ *env, const char *var)
 {
   if (var)
     {
-      const char *val = env->get (var);
+      const char *val = env == nullptr ? getenv (var) : env->get (var);
 
       if (val)
 	{
@@ -2032,7 +2032,9 @@  display_environment (const gdb_environ *env, const char *var)
     }
   else
     {
-      char **envp = env->envp ();
+      char **envp = env == nullptr ? environ : env->envp ();
+      if (envp == nullptr)
+	return;
 
       for (int idx = 0; envp[idx] != nullptr; ++idx)
 	{
@@ -2108,10 +2110,18 @@  set_var_in_environment (gdb_environ *env, const char *arg)
       gdb_printf (_("Setting environment variable "
 		    "\"%s\" to null value.\n"),
 		  var.c_str ());
-      env->set (var.c_str (), "");
+      if (env == nullptr)
+	setenv (var.c_str (), "", 1);
+      else
+	env->set (var.c_str (), "");
     }
   else
-    env->set (var.c_str (), val);
+    {
+      if (env == nullptr)
+	setenv (var.c_str (), val, 1);
+      else
+	env->set (var.c_str (), val);
+    }
 }
 
 static void
@@ -2132,10 +2142,20 @@  unset_var_in_environment (gdb_environ *env, const char *var, int from_tty)
       /* If there is no argument, delete all environment variables.
 	 Ask for confirmation if reading from the terminal.  */
       if (!from_tty || query (_("Delete all environment variables? ")))
-	env->clear ();
+	{
+	  if (env == nullptr)
+	    clearenv ();
+	  else
+	    env->clear ();
+	}
     }
   else
-    env->unset (var);
+    {
+      if (env == nullptr)
+	unsetenv (var);
+      else
+	env->unset (var);
+    }
 }
 
 static void