[PATCH 4/6] virsh: cmdEcho: Rewrite with new buffer helpers

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

 



Remove the need for temporary strings by fillin the output buffer
directly.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 tools/vsh.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 009c93254c..2456267426 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -3156,7 +3156,6 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
     bool shell = vshCommandOptBool(cmd, "shell");
     bool xml = vshCommandOptBool(cmd, "xml");
     bool err = vshCommandOptBool(cmd, "err");
-    int count = 0;
     const vshCmdOpt *opt = NULL;
     g_autofree char *arg = NULL;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
@@ -3164,27 +3163,21 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
     VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);

     while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
-        g_autofree char *str = NULL;
-        g_auto(virBuffer) xmlbuf = VIR_BUFFER_INITIALIZER;
         const char *curr = opt->data;

-        if (count)
-            virBufferAddChar(&buf, ' ');
-
         if (xml) {
-            virBufferEscapeString(&xmlbuf, "%s", curr);
-            str = virBufferContentAndReset(&xmlbuf);
+            virBufferEscapeString(&buf, "%s", curr);
+        } else if (shell) {
+            virBufferEscapeShell(&buf, curr);
         } else {
-            str = g_strdup(curr);
+            virBufferAdd(&buf, curr, -1);
         }

-        if (shell)
-            virBufferEscapeShell(&buf, str);
-        else
-            virBufferAdd(&buf, str, -1);
-        count++;
+        virBufferAddChar(&buf, ' ');
     }

+    virBufferTrim(&buf, " ");
+
     arg = virBufferContentAndReset(&buf);
     if (arg) {
         if (err)
-- 
2.31.1




[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]

  Powered by Linux