From fc3247f21180d6f33ab7ebd130ef3d7e9ccb9a74 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Fri, 10 Sep 2010 13:38:40 +0200 Subject: [PATCH] virsh: Use virBuffer for generating XML cmdAttachInterface and cmdAttachDisk still used vshRealloc and sprintf for generating XML, which is hardly maintainable. Let's get rid of this old code. --- tools/virsh.c | 158 ++++++++++++++++++-------------------------------- 1 file changed, 57 insertions(+), 101 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 57ea6184d7..55deb4c2be 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -7874,8 +7874,9 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) virDomainPtr dom = NULL; char *mac, *target, *script, *type, *source; int typ, ret = FALSE; - char *buf = NULL, *tmp = NULL; unsigned int flags; + virBuffer buf = VIR_BUFFER_INITIALIZER; + char *xml; if (!vshConnectionUsability(ctl, ctl->conn)) goto cleanup; @@ -7903,52 +7904,40 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) } /* Make XML of interface */ - tmp = vshMalloc(ctl, 1); - buf = vshMalloc(ctl, strlen(type) + 25); - sprintf(buf, " \n" , type); + virBufferVSprintf(&buf, "\n", type); - tmp = vshRealloc(ctl, tmp, strlen(source) + 28); - if (typ == 1) { - sprintf(tmp, " \n", source); - } else if (typ == 2) { - sprintf(tmp, " \n", source); - } - buf = vshRealloc(ctl, buf, strlen(buf) + strlen(tmp) + 1); - strcat(buf, tmp); + if (typ == 1) + virBufferVSprintf(&buf, " \n", source); + else if (typ == 2) + virBufferVSprintf(&buf, " \n", source); - if (target != NULL) { - tmp = vshRealloc(ctl, tmp, strlen(target) + 24); - sprintf(tmp, " \n", target); - buf = vshRealloc(ctl, buf, strlen(buf) + strlen(tmp) + 1); - strcat(buf, tmp); + if (target != NULL) + virBufferVSprintf(&buf, " \n", target); + if (mac != NULL) + virBufferVSprintf(&buf, " \n", mac); + if (script != NULL) + virBufferVSprintf(&buf, "