[PATCH v1 2/2] command: Change virCommandAddEnv so it replaces existing environment variables.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Richard W.M. Jones" <rjones@xxxxxxxxxx>

---
 src/util/command.c |   17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/util/command.c b/src/util/command.c
index f7d92dd..354e526 100644
--- a/src/util/command.c
+++ b/src/util/command.c
@@ -985,11 +985,26 @@ virCommandNonblockingFDs(virCommandPtr cmd)
 }
 
 /* Add an environment variable to the cmd->env list.  'env' is a
- * string like "name=value".
+ * string like "name=value".  If the named environment variable is
+ * already set, then it is replaced in the list.
  */
 static inline void
 virCommandAddEnv(virCommandPtr cmd, char *env)
 {
+    size_t namelen;
+    size_t i;
+
+    /* Search for the name in the existing environment. */
+    namelen = strcspn(env, "=");
+    for (i = 0; i < cmd->nenv; ++i) {
+        /* +1 because we want to match the '=' character too. */
+        if (STREQLEN(cmd->env[i], env, namelen+1)) {
+            VIR_FREE(cmd->env[i]);
+            cmd->env[i] = env;
+            return;
+        }
+    }
+
     /* Arg plus trailing NULL. */
     if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
         VIR_FREE(env);
-- 
1.7.10.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]