static int
runPgDump(const char *dbname)
{
- PQExpBuffer connstr = createPQExpBuffer();
+ PQExpBuffer connstrbuf = createPQExpBuffer();
PQExpBuffer cmd = createPQExpBuffer();
int ret;
* database name as is, but if it contains any = characters, it would
* incorrectly treat it as a connection string.
*/
- appendPQExpBuffer(connstr, "dbname='");
- doConnStrQuoting(connstr, dbname);
- appendPQExpBuffer(connstr, "'");
+ appendPQExpBufferStr(connstrbuf, "dbname=");
+ doConnStrQuoting(connstrbuf, dbname);
- doShellQuoting(cmd, connstr->data);
+ doShellQuoting(cmd, connstrbuf->data);
appendPQExpBuffer(cmd, "%s", SYSTEMQUOTE);
ret = system(cmd->data);
destroyPQExpBuffer(cmd);
- destroyPQExpBuffer(connstr);
+ destroyPQExpBuffer(connstrbuf);
return ret;
}
static void
doConnStrQuoting(PQExpBuffer buf, const char *str)
{
- while (*str)
+ const char *s;
+ bool needquotes;
+
+ /*
+ * If the string consists entirely of plain ASCII characters, no need to
+ * quote it. This is quite conservative, but better safe than sorry.
+ */
+ needquotes = false;
+ for (s = str; *s; s++)
+ {
+ if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
+ (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
+ {
+ needquotes = true;
+ break;
+ }
+ }
+
+ if (needquotes)
{
- /* ' and \ must be escaped by to \' and \\ */
- if (*str == '\'' || *str == '\\')
- appendPQExpBufferChar(buf, '\\');
+ appendPQExpBufferChar(buf, '\'');
+ while (*str)
+ {
+ /* ' and \ must be escaped by to \' and \\ */
+ if (*str == '\'' || *str == '\\')
+ appendPQExpBufferChar(buf, '\\');
- appendPQExpBufferChar(buf, *str);
- str++;
+ appendPQExpBufferChar(buf, *str);
+ str++;
+ }
+ appendPQExpBufferChar(buf, '\'');
}
+ else
+ appendPQExpBufferStr(buf, str);
}
/*