summaryrefslogtreecommitdiff
path: root/src/port
diff options
context:
space:
mode:
authorMagnus Hagander2013-01-05 14:40:19 +0000
committerMagnus Hagander2013-01-05 14:40:19 +0000
commit940d1366614a59713cdfd48daccd2ff9d26a4612 (patch)
tree4d9335e8d00aa240bbe9a4c9f2c2c8f03aac9c5f /src/port
parentfc8745070a53469a43ecbf999dc5692a36a649cc (diff)
Centralize single quote escaping in src/port/quotes.c
For code-reuse in upcoming functionality in pg_basebackup. Zoltan Boszormenyi
Diffstat (limited to 'src/port')
-rw-r--r--src/port/Makefile2
-rw-r--r--src/port/quotes.c36
2 files changed, 37 insertions, 1 deletions
diff --git a/src/port/Makefile b/src/port/Makefile
index a4ceb0c0e8..a3db615400 100644
--- a/src/port/Makefile
+++ b/src/port/Makefile
@@ -32,7 +32,7 @@ LIBS += $(PTHREAD_LIBS)
OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
- pgstrcasecmp.o qsort.o qsort_arg.o sprompt.o tar.o thread.o
+ pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
OBJS_SRV = $(OBJS:%.o=%_srv.o)
diff --git a/src/port/quotes.c b/src/port/quotes.c
new file mode 100644
index 0000000000..2ce60905a0
--- /dev/null
+++ b/src/port/quotes.c
@@ -0,0 +1,36 @@
+#include "c.h"
+
+/*
+ * Escape (by doubling) any single quotes or backslashes in given string
+ *
+ * Note: this is used to process postgresql.conf entries and to quote
+ * string literals in pg_basebackup for creating recovery.conf.
+ * Since postgresql.conf strings are defined to treat backslashes as escapes,
+ * we have to double backslashes here.
+ *
+ * Since this function is only used for parsing or creating configuration
+ * files, we do not care about encoding considerations.
+ *
+ * Returns a malloced() string that it's the responsibility of the caller
+ * to free.
+ */
+char *
+escape_single_quotes_ascii(const char *src)
+{
+ int len = strlen(src),
+ i,
+ j;
+ char *result = malloc(len * 2 + 1);
+
+ if (!result)
+ return NULL;
+
+ for (i = 0, j = 0; i < len; i++)
+ {
+ if (SQL_STR_DOUBLE(src[i], true))
+ result[j++] = src[i];
+ result[j++] = src[i];
+ }
+ result[j] = '\0';
+ return result;
+}