From 1f329990398765741c803632e6f4bc5a0a0b634d Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 28 Sep 2007 23:36:06 +0000
Subject: [PATCH] On OS X, assume that an empty-string result for
 nl_langinfo(CODESET) means UTF-8.  Per examination of /usr/share/locale in
 10.4.10.

---
 src/port/chklocale.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/port/chklocale.c b/src/port/chklocale.c
index 399438fe775..955437f2c1c 100644
--- a/src/port/chklocale.c
+++ b/src/port/chklocale.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/chklocale.c,v 1.1 2007/09/28 22:25:49 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/port/chklocale.c,v 1.2 2007/09/28 23:36:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -158,6 +158,7 @@ pg_get_encoding_from_locale(const char *ctype)
 	char	   *sys;
 	int			i;
 
+	/* Get the CODESET property, and also LC_CTYPE if not passed in */
 	if (ctype)
 	{
 		char	   *save;
@@ -197,12 +198,14 @@ pg_get_encoding_from_locale(const char *ctype)
 	if (!sys)
 		return PG_SQL_ASCII;		/* out of memory; unlikely */
 
+	/* If locale is C or POSIX, we can allow all encodings */
 	if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
 	{
 		free(sys);
 		return PG_SQL_ASCII;
 	}
 
+	/* Check the table */
 	for (i = 0; encoding_match_list[i].system_enc_name; i++)
 	{
 		if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
@@ -212,6 +215,20 @@ pg_get_encoding_from_locale(const char *ctype)
 		}
 	}
 
+	/* Special-case kluges for particular platforms go here */
+
+#ifdef __darwin__
+	/*
+	 * Current OS X has many locales that report an empty string for CODESET,
+	 * but they all seem to actually use UTF-8.
+	 */
+	if (strlen(sys) == 0)
+	{
+		free(sys);
+		return PG_UTF8;
+	}
+#endif
+
 	/*
 	 * We print a warning if we got a CODESET string but couldn't recognize
 	 * it.  This means we need another entry in the table.
-- 
GitLab