summaryrefslogtreecommitdiffstats
path: root/src/port/quotes.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
commit5e45211a64149b3c659b90ff2de6fa982a5a93ed (patch)
tree739caf8c461053357daa9f162bef34516c7bf452 /src/port/quotes.c
parentInitial commit. (diff)
downloadpostgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.tar.xz
postgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.zip
Adding upstream version 15.5.upstream/15.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/port/quotes.c')
-rw-r--r--src/port/quotes.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/port/quotes.c b/src/port/quotes.c
new file mode 100644
index 0000000..d90a456
--- /dev/null
+++ b/src/port/quotes.c
@@ -0,0 +1,51 @@
+/*-------------------------------------------------------------------------
+ *
+ * quotes.c
+ * string quoting and escaping functions
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/port/quotes.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#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 writing the recovery configuration.
+ * 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;
+}