From cd408d93fdf347ff63a8062f75f1f4ee3e898b17 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Fri, 6 Feb 2015 13:23:07 -0500
Subject: Add setlocale() calls around use of libarchive APIs (#14934, #15377)

The libarchive APIs use nl_langinfo(CODESET) for iconv so they need the
locale to be set for LC_CTYPE.  However, the rest of CMake does not
define any behavior for non-ASCII character classification/conversion so
we do not want to setlocale() globally.  Add a RAII class to save, set,
and restore the locale around calls to libarchive APIs.

Inspired-by: Clinton Stimpson <clinton@elemtech.com>
---
 Source/CMakeLists.txt     |  1 +
 Source/cmArchiveWrite.cxx |  4 ++++
 Source/cmLocale.h         | 31 +++++++++++++++++++++++++++++++
 Source/cmSystemTools.cxx  |  3 +++
 4 files changed, 39 insertions(+)
 create mode 100644 Source/cmLocale.h

diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt
index f9405b3..1ff1c16 100644
--- a/Source/CMakeLists.txt
+++ b/Source/CMakeLists.txt
@@ -261,6 +261,7 @@ set(SRCS
   cmLocalGenerator.cxx
   cmLocalGenerator.h
   cmLocalUnixMakefileGenerator3.cxx
+  cmLocale.h
   cmMakeDepend.cxx
   cmMakeDepend.h
   cmMakefile.cxx
diff --git a/Source/cmArchiveWrite.cxx b/Source/cmArchiveWrite.cxx
index a2aecac..9f28d42 100644
--- a/Source/cmArchiveWrite.cxx
+++ b/Source/cmArchiveWrite.cxx
@@ -12,6 +12,7 @@
 #include "cmArchiveWrite.h"
 
 #include "cmSystemTools.h"
+#include "cmLocale.h"
 #include <cmsys/ios/iostream>
 #include <cmsys/Directory.hxx>
 #include <cmsys/FStream.hxx>
@@ -259,6 +260,9 @@ bool cmArchiveWrite::AddFile(const char* file,
     }
   const char* out = file + skip;
 
+  cmLocaleRAII localeRAII;
+  static_cast<void>(localeRAII);
+
   // Meta-data.
   std::string dest = prefix? prefix : "";
   dest += out;
diff --git a/Source/cmLocale.h b/Source/cmLocale.h
new file mode 100644
index 0000000..727f0f5
--- /dev/null
+++ b/Source/cmLocale.h
@@ -0,0 +1,31 @@
+/*============================================================================
+  CMake - Cross Platform Makefile Generator
+  Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
+
+  Distributed under the OSI-approved BSD License (the "License");
+  see accompanying file Copyright.txt for details.
+
+  This software is distributed WITHOUT ANY WARRANTY; without even the
+  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the License for more information.
+============================================================================*/
+#ifndef cmLocale_h
+#define cmLocale_h
+
+#include <locale.h>
+
+class cmLocaleRAII
+{
+  const char* OldLocale;
+public:
+  cmLocaleRAII(): OldLocale(setlocale(LC_CTYPE, 0))
+    {
+    setlocale(LC_CTYPE, "");
+    }
+  ~cmLocaleRAII()
+    {
+    setlocale(LC_CTYPE, this->OldLocale);
+    }
+};
+
+#endif
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index fbb4416..89efdc7 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -28,6 +28,7 @@
 #include <cmsys/Encoding.hxx>
 #if defined(CMAKE_BUILD_WITH_CMAKE)
 # include "cmArchiveWrite.h"
+# include "cmLocale.h"
 # include <cm_libarchive.h>
 # include <cmsys/Terminal.h>
 #endif
@@ -1691,6 +1692,8 @@ long copy_data(struct archive *ar, struct archive *aw)
 bool extract_tar(const char* outFileName, bool verbose,
                  bool extract)
 {
+  cmLocaleRAII localeRAII;
+  static_cast<void>(localeRAII);
   struct archive* a = archive_read_new();
   struct archive *ext = archive_write_disk_new();
   archive_read_support_compression_all(a);
-- 
cgit v0.12