Similar to virCommandArgAddFormat. We will use this shortly.
Signed-off-by: Cole Robinson <crobinso(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/command.c | 30 ++++++++++++++++++++++++++++++
src/util/command.h | 7 +++++++
3 files changed, 38 insertions(+), 0 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..f382cc5 100644
--- a/src/util/command.c
+++ b/src/util/command.c
@@ -259,6 +259,36 @@ virCommandNonblockingFDs(virCommandPtr cmd)
}
/*
+ * Add an environment variable to the child created by a printf-style format
+ */
+void
+virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
+{
+ char *env;
+ va_list list;
+
+ if (!cmd || cmd->has_error)
+ return;
+
+ va_start(list, format);
+ if (virVasprintf(&env, format, list) < 0) {
+ cmd->has_error = ENOMEM;
+ va_end(list);
+ return;
+ }
+ va_end(list);
+
+ /* Arg plus trailing NULL. */
+ if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
+ VIR_FREE(env);
+ cmd->has_error = ENOMEM;
+ return;
+ }
+
+ cmd->env[cmd->nenv++] = env;
+}
+
+/*
* Add an environment variable to the child
* using separate name & value strings
*/
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