tests: Avoid check-subst failures in QEMU user-mode environments.

* Makefile.devel (GNULIB_MODULES): Add stdbool.
(gnulib-imported-files): Copy also tests/qemu.h.
* tests/is-native.c: New file.
* tests/Makefile.in (is-native, is-native.@OBJEXT@): New targets.
(clean): Remove the 'is-native' program.
(SOURCE_FILES): Add is-native.c.
(IMPORTED_FILES): Add qemu.h.
* tests/check-subst: Invoke 'locale charmap' only in native
environments.
diff --git a/.gitignore b/.gitignore
index f8a3b98..708c510 100644
--- a/.gitignore
+++ b/.gitignore
@@ -233,6 +233,7 @@
 /srcm4/wint_t.m4
 /srcm4/year2038.m4
 /srcm4/zzgnulib.m4
+/tests/qemu.h
 # Cached parameters of gnulib-tool:
 /srcm4/gnulib-cache.m4
 
diff --git a/ChangeLog b/ChangeLog
index d6b8130..2815c86 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2021-09-19  Bruno Haible  <bruno@clisp.org>
+
+	tests: Avoid check-subst failures in QEMU user-mode environments.
+	* Makefile.devel (GNULIB_MODULES): Add stdbool.
+	(gnulib-imported-files): Copy also tests/qemu.h.
+	* tests/is-native.c: New file.
+	* tests/Makefile.in (is-native, is-native.@OBJEXT@): New targets.
+	(clean): Remove the 'is-native' program.
+	(SOURCE_FILES): Add is-native.c.
+	(IMPORTED_FILES): Add qemu.h.
+	* tests/check-subst: Invoke 'locale charmap' only in native
+	environments.
+
 2021-06-27  Bruno Haible  <bruno@clisp.org>
 
 	README: Update.
diff --git a/Makefile.devel b/Makefile.devel
index fcf3b77..7f22e0f 100644
--- a/Makefile.devel
+++ b/Makefile.devel
@@ -56,6 +56,7 @@
   relocatable-prog \
   safe-read \
   sigpipe \
+  stdbool \
   stdio \
   stdlib \
   strerror \
@@ -78,6 +79,7 @@
 	    $$GNULIB_TOOL --copy-file build-aux/$$file || exit $$?; \
 	    chmod a+x build-aux/$$file || exit $$?; \
 	  done; \
+	  $$GNULIB_TOOL --copy-file tests/qemu.h || exit $$?; \
 	fi
 
 srclib/Makefile.in : srclib/Makefile.am srclib/Makefile.gnulib aclocal.m4
diff --git a/tests/Makefile.in b/tests/Makefile.in
index 16a0072..8ed9d47 100644
--- a/tests/Makefile.in
+++ b/tests/Makefile.in
@@ -28,7 +28,7 @@
 
 all :
 
-check : all table-from table-to ../src/iconv_no_i18n test-shiftseq test-to-wchar
+check : all table-from table-to ../src/iconv_no_i18n is-native test-shiftseq test-to-wchar
 	$(SHELL) $(srcdir)/check-stateless $(srcdir) ASCII
 #	/* General multi-byte encodings */
 	$(CC) $(LDFLAGS) $(INCLUDES) $(CFLAGS) $(CPPFLAGS) $(srcdir)/genutf8.c ../srclib/libicrt.a -o genutf8 && \
@@ -218,6 +218,12 @@
 table-to.@OBJEXT@ : $(srcdir)/table-to.c
 	$(CC) -c $(INCLUDES) $(CFLAGS) $(CPPFLAGS) $(srcdir)/table-to.c
 
+is-native : is-native.@OBJEXT@
+	$(LIBTOOL_LINK) $(CC) $(LDFLAGS) $(CFLAGS) is-native.@OBJEXT@ ../srclib/libicrt.a -o $@
+
+is-native.@OBJEXT@ : $(srcdir)/is-native.c
+	$(CC) -c $(INCLUDES) $(CFLAGS) $(CPPFLAGS) $(srcdir)/is-native.c
+
 test-shiftseq : test-shiftseq.@OBJEXT@ ../lib/libiconv.la
 	$(LIBTOOL_LINK) $(CC) $(LDFLAGS) $(CFLAGS) test-shiftseq.@OBJEXT@ ../srclib/libicrt.a ../lib/libiconv.la -o $@
 
@@ -237,7 +243,7 @@
 mostlyclean : clean
 
 clean : force
-	$(RM) *.@OBJEXT@ *.lo table-from table-from@EXEEXT@ table-to table-to@EXEEXT@ test-shiftseq test-shiftseq@EXEEXT@ test-to-wchar test-to-wchar@EXEEXT@ tmp-* genutf8 genutf8@EXEEXT@ UTF-8.TXT gengb18030z gengb18030z@EXEEXT@ GB18030.TXT core *.stackdump
+	$(RM) *.@OBJEXT@ *.lo table-from table-from@EXEEXT@ table-to table-to@EXEEXT@ is-native is-native@EXEEXT@ test-shiftseq test-shiftseq@EXEEXT@ test-to-wchar test-to-wchar@EXEEXT@ tmp-* genutf8 genutf8@EXEEXT@ UTF-8.TXT gengb18030z gengb18030z@EXEEXT@ GB18030.TXT core *.stackdump
 	$(RM) -r .libs _libs
 
 distclean : clean
@@ -363,6 +369,7 @@
   Quotes.UTF-8 Quotes.ISO-8859-1 Quotes.ASCII \
   Translit1.ISO-8859-1 Translit1.ASCII \
   TranslitFail1.ISO-8859-1 \
+  is-native.c \
   check-subst \
   test-shiftseq.c \
   test-to-wchar.c \
@@ -399,7 +406,8 @@
   ATARIST.TXT \
   RISCOS-LATIN1.TXT
 # List of distributed files imported from other packages.
-IMPORTED_FILES =
+IMPORTED_FILES = \
+  qemu.h
 # List of distributed files generated by autotools or Makefile.devel.
 GENERATED_FILES =
 # List of distributed files generated by "make".
diff --git a/tests/check-subst b/tests/check-subst
index 1b5f387..8fc721f 100755
--- a/tests/check-subst
+++ b/tests/check-subst
@@ -19,7 +19,7 @@
 
 # Test of --byte-subst with a non-ASCII substitution.
 
-if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
+if ./is-native && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
   cat > tmp-in <<\EOF
 Böse Bübchen
 EOF
@@ -30,7 +30,7 @@
   cmp tmp-out tmp-ok
 fi
 
-if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
+if ./is-native && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
   cat > tmp-in <<\EOF
 Böse Bübchen
 EOF
@@ -77,7 +77,7 @@
 
 # Test of --unicode-subst with a non-ASCII substitution.
 
-if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
+if ./is-native && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
   cat > tmp-in <<\EOF
 Russian (Русский)
 EOF
@@ -116,7 +116,7 @@
 # wcrtomb() doesn't exist on FreeBSD 4.0 and is broken on MacOS X 10.3.
 # So far this has been tested only on a glibc system with !__STDC_ISO_10646__.
 
-if false && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
+if false && ./is-native && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
   cat > tmp-in <<\EOF
 Russian (Русский)
 EOF
@@ -128,7 +128,7 @@
 EOF
   cmp tmp-out1 tmp-ok
   cmp tmp-out2 tmp-ok
-  if test "`(LC_ALL=de_DE.ISO-8859-1 locale charmap) 2>/dev/null`" = ISO-8859-1; then
+  if ./is-native && test "`(LC_ALL=de_DE.ISO-8859-1 locale charmap) 2>/dev/null`" = ISO-8859-1; then
     options_latin1=`echo " $options_utf8" | $iconv -f UTF-8 -t ISO-8859-1`
     LC_ALL=de_DE.ISO-8859-1 $iconv $options_latin1 -f wchar_t -t UTF-8 < tmp-inw > tmp-out1
     cat > tmp-ok <<\EOF
diff --git a/tests/is-native.c b/tests/is-native.c
new file mode 100644
index 0000000..109d376
--- /dev/null
+++ b/tests/is-native.c
@@ -0,0 +1,30 @@
+/* Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU LIBICONV Library.
+
+   The GNU LIBICONV Library is free software; you can redistribute it
+   and/or modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either version 2.1
+   of the License, or (at your option) any later version.
+
+   The GNU LIBICONV Library is distributed in the hope that it will be
+   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU LIBICONV Library; see the file COPYING.LIB.
+   If not, see <https://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+
+#include "qemu.h"
+
+/* Returns 0 (success) in a native environment.
+   Returns 1 (failure) in a cross-executing environment, that is, in an
+   environment where compiled programs use a different libc than the system's
+   libc.  Currently, only QEMU user-mode environments are recognized.  */
+
+int main ()
+{
+  return is_running_under_qemu_user () ? 1 : 0;
+}