From 31fa241b46a848f6e90e5e7a42fc2fa10246cd90 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 11 Aug 2021 11:10:12 +0200 Subject: [PATCH] virsh: cmdEcho: Rewrite with new buffer helpers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Remove the need for temporary strings by filling the output buffer directly. Signed-off-by: Peter Krempa Reviewed-by: Martin Kletzander Reviewed-by: Ján Tomko --- tools/vsh.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index fc20fb21ee..cca2920711 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3153,7 +3153,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; @@ -3161,27 +3160,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.47.2