[PATCH 1/6] command: Add virCommandEnvAddFormat

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

 



Similar to virCommandArgAddFormat. We will use this shortly.

v2:
    Convert virCommandEnvAddPair to use the new function

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 src/libvirt_private.syms |    1 +
 src/util/command.c       |   23 ++++++++++++++++++-----
 src/util/command.h       |    7 +++++++
 3 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1b22be6..d89b191 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -99,6 +99,7 @@ virCommandAddArgList;
 virCommandAddArgPair;
 virCommandAddArgSet;
 virCommandAddEnvBuffer;
+virCommandAddEnvFormat;
 virCommandAddEnvPair;
 virCommandAddEnvPass;
 virCommandAddEnvPassCommon;
diff --git a/src/util/command.c b/src/util/command.c
index 862a913..ff4869d 100644
--- a/src/util/command.c
+++ b/src/util/command.c
@@ -259,23 +259,26 @@ virCommandNonblockingFDs(virCommandPtr cmd)
 }
 
 /*
- * Add an environment variable to the child
- * using separate name & value strings
+ * Add an environment variable to the child created by a printf-style format
  */
 void
-virCommandAddEnvPair(virCommandPtr cmd, const char *name, const char *value)
+virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
 {
     char *env;
+    va_list list;
 
     if (!cmd || cmd->has_error)
         return;
 
-    if (virAsprintf(&env, "%s=%s", name, value ? value : "") < 0) {
+    va_start(list, format);
+    if (virVasprintf(&env, format, list) < 0) {
         cmd->has_error = ENOMEM;
+        va_end(list);
         return;
     }
+    va_end(list);
 
-    /* env plus trailing NULL */
+    /* Arg plus trailing NULL. */
     if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
         VIR_FREE(env);
         cmd->has_error = ENOMEM;
@@ -285,6 +288,16 @@ virCommandAddEnvPair(virCommandPtr cmd, const char *name, const char *value)
     cmd->env[cmd->nenv++] = env;
 }
 
+/*
+ * Add an environment variable to the child
+ * using separate name & value strings
+ */
+void
+virCommandAddEnvPair(virCommandPtr cmd, const char *name, const char *value)
+{
+    virCommandAddEnvFormat(cmd, "%s=%s", name, value);
+}
+
 
 /*
  * Add an environment variable to the child
diff --git a/src/util/command.h b/src/util/command.h
index ff8ccf5..69e9169 100644
--- a/src/util/command.h
+++ b/src/util/command.h
@@ -98,6 +98,13 @@ void virCommandDaemonize(virCommandPtr cmd);
 void virCommandNonblockingFDs(virCommandPtr cmd);
 
 /*
+ * Add an environment variable to the child created by a printf-style format
+ */
+void
+virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
+                       ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3);
+
+/*
  * Add an environment variable to the child
  * using separate name & value strings
  */
-- 
1.7.4.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]