void
virBufferEscapeShell(virBuffer *buf, const char *str)
{
- int len;
g_autofree char *escaped = NULL;
char *out;
const char *cur;
if ((buf == NULL) || (str == NULL))
return;
- /* Only quote if str includes shell metacharacters. */
- if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
- virBufferAdd(buf, str, -1);
+ if (!*str) {
+ virBufferAddLit(buf, "''");
return;
}
- if (*str) {
- len = strlen(str);
-
- escaped = g_malloc0_n(len + 1, 4);
- } else {
- virBufferAddLit(buf, "''");
+ /* Only quote if str includes shell metacharacters. */
+ if (!strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
+ virBufferAdd(buf, str, -1);
return;
}
+ escaped = g_malloc0_n(strlen(str) + 1, 4);
+
cur = str;
out = escaped;