render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
404e58
From 2b32735af480055e27400068d27364d521071117 Mon Sep 17 00:00:00 2001
404e58
From: "Richard W.M. Jones" <rjones@redhat.com>
404e58
Date: Mon, 24 Sep 2012 17:35:47 +0100
404e58
Subject: [PATCH 2/2] command: Change virCommandAddEnv so it replaces existing
404e58
 environment variables.
404e58
404e58
---
404e58
 src/util/command.c |   17 ++++++++++++++++-
404e58
 1 file changed, 16 insertions(+), 1 deletion(-)
404e58
404e58
diff --git a/src/util/command.c b/src/util/command.c
404e58
index f7d92dd..1adf7b9 100644
404e58
--- a/src/util/command.c
404e58
+++ b/src/util/command.c
404e58
@@ -985,11 +985,26 @@ virCommandNonblockingFDs(virCommandPtr cmd)
404e58
 }
404e58
 
404e58
 /* Add an environment variable to the cmd->env list.  'env' is a
404e58
- * string like "name=value".
404e58
+ * string like "name=value".  If the named environment variable is
404e58
+ * already set, then it is replaced in the list.
404e58
  */
404e58
 static inline void
404e58
 virCommandAddEnv(virCommandPtr cmd, char *env)
404e58
 {
404e58
+    size_t namelen;
404e58
+    size_t i;
404e58
+
404e58
+    /* Search for the name in the existing environment. */
404e58
+    namelen = strcspn(env, "=");
404e58
+    for (i = 0; i < cmd->nenv; ++i) {
404e58
+        /* + 1 because we want to match the '=' character too. */
404e58
+        if (STREQLEN(cmd->env[i], env, namelen + 1)) {
404e58
+            VIR_FREE(cmd->env[i]);
404e58
+            cmd->env[i] = env;
404e58
+            return;
404e58
+        }
404e58
+    }
404e58
+
404e58
     /* Arg plus trailing NULL. */
404e58
     if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
404e58
         VIR_FREE(env);
404e58
-- 
404e58
1.7.10.4
404e58