Subject: bug#23269: MirBSD 10 i386 test failures [Re: grep-2.24.13-bed6 feedback

From: Paul Eggert <[email-masked]>
Date: Sun, 17 Apr 2016 23:40:03 -0700

Jim Meyering wrote:
> Conclusion: we'll have to make btowc work properly in the C locale, too.

Perhaps something like the attached (untested) patch? The basic idea is to have 
btowc and mbtowc use a fixed mbrtowc if the latter has the C-locale problem in 
question. While we're at it, btowc should invoke mbrtowc not mbtowc, as btowc is 
thread-safe but mbtowc is not.

diff --git a/lib/btowc.c b/lib/btowc.c
index e7badf9..98d0840 100644
--- a/lib/btowc.c
+++ b/lib/btowc.c
@@ -30,9 +30,10 @@ btowc (int c)
     {
       char buf[1];
       wchar_t wc;
+      mbstate_t mbs = { 0 };
 
       buf[0] = c;
-      if (mbtowc (&wc, buf, 1) >= 0)
+      if (mbrtowc (&wc, buf, 1, &mbs) >= 0)
         return wc;
     }
   return WEOF;
diff --git a/m4/btowc.m4 b/m4/btowc.m4
index 47e8fd8..6e941e1 100644
--- a/m4/btowc.m4
+++ b/m4/btowc.m4
@@ -1,4 +1,4 @@
-# btowc.m4 serial 10
+# btowc.m4 serial 11
 dnl Copyright (C) 2008-2016 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -13,15 +13,18 @@ AC_DEFUN([gl_FUNC_BTOWC],
   dnl <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42440>.
   AC_REQUIRE([gl_WCHAR_H_INLINE_OK])
 
+  AC_REQUIRE([AC_PROG_CC])
+  AC_REQUIRE([gt_LOCALE_FR])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+  AC_REQUIRE([gl_MBRTOWC_C_LOCALE])
+
   AC_CHECK_FUNCS_ONCE([btowc])
   if test $ac_cv_func_btowc = no; then
     HAVE_BTOWC=0
   else
-
-    AC_REQUIRE([AC_PROG_CC])
-    AC_REQUIRE([gt_LOCALE_FR])
-    AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
-
+   case $gl_cv_C_locale_sans_EILSEQ in
+   *no) REPLACE_BTOWC=1 ;;
+   *)
     dnl Cygwin 1.7.2 btowc('\0') is WEOF, not 0.
     AC_CACHE_CHECK([whether btowc(0) is correct],
       [gl_cv_func_btowc_nul],
@@ -106,7 +109,8 @@ int main ()
     case "$gl_cv_func_btowc_eof" in
       *yes) ;;
       *) REPLACE_BTOWC=1 ;;
-    esac
+    esac ;;
+   esac
   fi
 ])
 
diff --git a/m4/mbtowc.m4 b/m4/mbtowc.m4
index e770bbf..825abd4 100644
--- a/m4/mbtowc.m4
+++ b/m4/mbtowc.m4
@@ -1,4 +1,4 @@
-# mbtowc.m4 serial 2
+# mbtowc.m4 serial 3
 dnl Copyright (C) 2011-2016 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,10 +7,11 @@ dnl with or without modifications, as long as this notice is preserved.
 AC_DEFUN([gl_FUNC_MBTOWC],
 [
   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
+  AC_REQUIRE([gl_MBRTOWC_C_LOCALE])
 
-  if false; then
-    REPLACE_MBTOWC=1
-  fi
+  case $gl_cv_C_locale_sans_EILSEQ in
+    *no) REPLACE_MBTOWC=1;;
+  esac
 ])
 
 # Prerequisites of lib/mbtowc.c.
diff --git a/modules/btowc b/modules/btowc
index 68f4066..ac6d474 100644
--- a/modules/btowc
+++ b/modules/btowc
@@ -8,7 +8,7 @@ m4/locale-fr.m4
 
 Depends-on:
 wchar
-mbtowc          [test $HAVE_BTOWC = 0 || test $REPLACE_BTOWC = 1]
+mbrtowc         [test $HAVE_BTOWC = 0 || test $REPLACE_BTOWC = 1]
 
 configure.ac:
 gl_FUNC_BTOWC