]> Savannah Git Hosting - gnulib.git/commitdiff
mbstowcs: New module.
authorBruno Haible <bruno@clisp.org>
Thu, 30 Mar 2023 22:27:20 +0000 (00:27 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 30 Mar 2023 22:27:20 +0000 (00:27 +0200)
* lib/stdlib.in.h (mbstowcs): New declaration.
* lib/mbstowcs.c: New file, based on lib/mbstoc32s.c.
* m4/mbstowcs.m4: New file.
* m4/stdlib_h.m4 (gl_STDLIB_H): Test whether mbstowcs is declared.
(gl_STDLIB_H_REQUIRE_DEFAULTS): Initialize GNULIB_MBSTOWCS.
(gl_STDLIB_H_DEFAULTS): Initialize REPLACE_MBSTOWCS.
* modules/stdlib (Makefile.am): Substitute GNULIB_MBSTOWCS,
REPLACE_MBSTOWCS.
* modules/mbstowcs: New file.
* tests/test-stdlib-c++.cc (mbstowcs): Check signature.
* doc/posix-functions/mbstowcs.texi: Mention the C locale behaviour bug
and the new module.

ChangeLog
doc/posix-functions/mbstowcs.texi
lib/mbstowcs.c [new file with mode: 0644]
lib/stdlib.in.h
m4/mbstowcs.m4 [new file with mode: 0644]
m4/stdlib_h.m4
modules/mbstowcs [new file with mode: 0644]
modules/stdlib
tests/test-stdlib-c++.cc

index 4afba58a3cf7bddbe0b2832b498866e8da029fc6..bc7372479cccc8afe13e2c4bae450e5dc7d212fc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2023-03-30  Bruno Haible  <bruno@clisp.org>
+
+       mbstowcs: New module.
+       * lib/stdlib.in.h (mbstowcs): New declaration.
+       * lib/mbstowcs.c: New file, based on lib/mbstoc32s.c.
+       * m4/mbstowcs.m4: New file.
+       * m4/stdlib_h.m4 (gl_STDLIB_H): Test whether mbstowcs is declared.
+       (gl_STDLIB_H_REQUIRE_DEFAULTS): Initialize GNULIB_MBSTOWCS.
+       (gl_STDLIB_H_DEFAULTS): Initialize REPLACE_MBSTOWCS.
+       * modules/stdlib (Makefile.am): Substitute GNULIB_MBSTOWCS,
+       REPLACE_MBSTOWCS.
+       * modules/mbstowcs: New file.
+       * tests/test-stdlib-c++.cc (mbstowcs): Check signature.
+       * doc/posix-functions/mbstowcs.texi: Mention the C locale behaviour bug
+       and the new module.
+
 2023-03-30  Bruno Haible  <bruno@clisp.org>
 
        mbsnrtowcs: Fix behaviour in the C locale.
index 5d7dc4ca62e8f585b578f3196437cee4ab30d868..a695edc2afb176cef516b7b6c586cd28e3691d34 100644 (file)
@@ -4,10 +4,14 @@
 
 POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/mbstowcs.html}
 
-Gnulib module: ---
+Gnulib module: mbstowcs
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+In the C or POSIX locales, this function can return @code{(size_t) -1}
+and set @code{errno} to @code{EILSEQ}:
+glibc 2.35.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/mbstowcs.c b/lib/mbstowcs.c
new file mode 100644 (file)
index 0000000..e32d9ac
--- /dev/null
@@ -0,0 +1,33 @@
+/* Convert string to wide string.
+   Copyright (C) 2020-2023 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2020.
+
+   This file 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.
+
+   This file 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 this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdlib.h>
+
+#include <string.h>
+#include <wchar.h>
+
+size_t
+mbstowcs (wchar_t *dest, const char *src, size_t len)
+{
+  mbstate_t state;
+
+  memset (&state, '\0', sizeof (mbstate_t));
+  return mbsrtowcs (dest, &src, len, &state);
+}
index a91f4e23d6792ce81ba1a3cd9e6711a542601c44..4ecfc96a6fd6522dccfcf8eb17092e8b8c09293f 100644 (file)
@@ -589,6 +589,36 @@ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - "
 # endif
 #endif
 
+/* Convert a string to a wide string.  */
+#if @GNULIB_MBSTOWCS@
+# if @REPLACE_MBSTOWCS@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef mbstowcs
+#   define mbstowcs rpl_mbstowcs
+#  endif
+_GL_FUNCDECL_RPL (mbstowcs, size_t,
+                  (wchar_t *restrict dest, const char *restrict src,
+                   size_t len)
+                  _GL_ARG_NONNULL ((2)));
+_GL_CXXALIAS_RPL (mbstowcs, size_t,
+                  (wchar_t *restrict dest, const char *restrict src,
+                   size_t len));
+# else
+_GL_CXXALIAS_SYS (mbstowcs, size_t,
+                  (wchar_t *restrict dest, const char *restrict src,
+                   size_t len));
+# endif
+# if __GLIBC__ >= 2
+_GL_CXXALIASWARN (mbstowcs);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef mbstowcs
+# if HAVE_RAW_DECL_MBSTOWCS
+_GL_WARN_ON_USE (mbstowcs, "mbstowcs is unportable - "
+                 "use gnulib module mbstowcs for portability");
+# endif
+#endif
+
 /* Convert a multibyte character to a wide character.  */
 #if @GNULIB_MBTOWC@
 # if @REPLACE_MBTOWC@
diff --git a/m4/mbstowcs.m4 b/m4/mbstowcs.m4
new file mode 100644 (file)
index 0000000..c66e804
--- /dev/null
@@ -0,0 +1,21 @@
+# mbstowcs.m4 serial 1
+dnl Copyright (C) 2023 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_MBSTOWCS],
+[
+  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
+
+  gl_MBRTOWC_C_LOCALE
+  case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
+    *yes) ;;
+    *) REPLACE_MBSTOWCS=1 ;;
+  esac
+])
+
+# Prerequisites of lib/mbstowcs.c.
+AC_DEFUN([gl_PREREQ_MBSTOWCS], [
+  :
+])
index 249ef657224ba9d2505f53f53d176a6067f23834..ac28ed9efcc61ac4c5e3b5068dc43bfc3c8eb452 100644 (file)
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 71
+# stdlib_h.m4 serial 72
 dnl Copyright (C) 2007-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -24,8 +24,8 @@ AC_DEFUN_ONCE([gl_STDLIB_H],
 #endif
     ]], [_Exit aligned_alloc atoll canonicalize_file_name free
     getloadavg getprogname getsubopt grantpt
-    initstate initstate_r mbtowc mkdtemp mkostemp mkostemps mkstemp mkstemps
-    posix_memalign posix_openpt ptsname ptsname_r qsort_r
+    initstate initstate_r mbstowcs mbtowc mkdtemp mkostemp mkostemps mkstemp
+    mkstemps posix_memalign posix_openpt ptsname ptsname_r qsort_r
     random random_r reallocarray realpath rpmatch secure_getenv setenv
     setstate setstate_r srandom srandom_r
     strtod strtol strtold strtoll strtoul strtoull unlockpt unsetenv])
@@ -78,6 +78,7 @@ AC_DEFUN([gl_STDLIB_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GRANTPT])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MALLOC_GNU])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MALLOC_POSIX])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSTOWCS])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBTOWC])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MKDTEMP])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MKOSTEMP])
@@ -180,6 +181,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
   REPLACE_INITSTATE=0;       AC_SUBST([REPLACE_INITSTATE])
   REPLACE_MALLOC_FOR_MALLOC_GNU=0;    AC_SUBST([REPLACE_MALLOC_FOR_MALLOC_GNU])
   REPLACE_MALLOC_FOR_MALLOC_POSIX=0;  AC_SUBST([REPLACE_MALLOC_FOR_MALLOC_POSIX])
+  REPLACE_MBSTOWCS=0;        AC_SUBST([REPLACE_MBSTOWCS])
   REPLACE_MBTOWC=0;          AC_SUBST([REPLACE_MBTOWC])
   REPLACE_MKOSTEMP=0;        AC_SUBST([REPLACE_MKOSTEMP])
   REPLACE_MKOSTEMPS=0;       AC_SUBST([REPLACE_MKOSTEMPS])
diff --git a/modules/mbstowcs b/modules/mbstowcs
new file mode 100644 (file)
index 0000000..44ba43d
--- /dev/null
@@ -0,0 +1,33 @@
+Description:
+mbstowcs() function: convert string to wide string.
+
+Files:
+lib/mbstowcs.c
+m4/mbstowcs.m4
+m4/mbrtowc.m4
+
+Depends-on:
+stdlib
+mbsrtowcs       [test $REPLACE_MBSTOWCS = 1]
+
+configure.ac:
+gl_FUNC_MBSTOWCS
+gl_CONDITIONAL([GL_COND_OBJ_MBSTOWCS], [test $REPLACE_MBSTOWCS = 1])
+AM_COND_IF([GL_COND_OBJ_MBSTOWCS], [
+  gl_PREREQ_MBSTOWCS
+])
+gl_STDLIB_MODULE_INDICATOR([mbstowcs])
+
+Makefile.am:
+if GL_COND_OBJ_MBSTOWCS
+lib_SOURCES += mbstowcs.c
+endif
+
+Include:
+<stdlib.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
index bafeb214eef289bc1ff7b1196fda5b919d4d2584..efea327794c035de565becb9c98121696ffd505b 100644 (file)
@@ -47,6 +47,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
              -e 's/@''GNULIB_GRANTPT''@/$(GNULIB_GRANTPT)/g' \
              -e 's/@''GNULIB_MALLOC_GNU''@/$(GNULIB_MALLOC_GNU)/g' \
              -e 's/@''GNULIB_MALLOC_POSIX''@/$(GNULIB_MALLOC_POSIX)/g' \
+             -e 's/@''GNULIB_MBSTOWCS''@/$(GNULIB_MBSTOWCS)/g' \
              -e 's/@''GNULIB_MBTOWC''@/$(GNULIB_MBTOWC)/g' \
              -e 's/@''GNULIB_MKDTEMP''@/$(GNULIB_MKDTEMP)/g' \
              -e 's/@''GNULIB_MKOSTEMP''@/$(GNULIB_MKOSTEMP)/g' \
@@ -140,6 +141,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
              -e 's|@''REPLACE_INITSTATE''@|$(REPLACE_INITSTATE)|g' \
              -e 's|@''REPLACE_MALLOC_FOR_MALLOC_GNU''@|$(REPLACE_MALLOC_FOR_MALLOC_GNU)|g' \
              -e 's|@''REPLACE_MALLOC_FOR_MALLOC_POSIX''@|$(REPLACE_MALLOC_FOR_MALLOC_POSIX)|g' \
+             -e 's|@''REPLACE_MBSTOWCS''@|$(REPLACE_MBSTOWCS)|g' \
              -e 's|@''REPLACE_MBTOWC''@|$(REPLACE_MBTOWC)|g' \
              -e 's|@''REPLACE_MKOSTEMP''@|$(REPLACE_MKOSTEMP)|g' \
              -e 's|@''REPLACE_MKOSTEMPS''@|$(REPLACE_MKOSTEMPS)|g' \
index 184437c5fd21ac4f236c82eed2b657c1c7cfadcb..7683afd154dbba673a5cba97443e599596363947 100644 (file)
@@ -73,6 +73,11 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::mbtowc, int,
                  (wchar_t *, const char *, size_t));
 #endif
 
+#if GNULIB_TEST_MBSTOWCS
+SIGNATURE_CHECK (GNULIB_NAMESPACE::mbstowcs, size_t,
+                 (wchar_t *, const char *, size_t));
+#endif
+
 #if GNULIB_TEST_MKDTEMP
 SIGNATURE_CHECK (GNULIB_NAMESPACE::mkdtemp, char *, (char *));
 #endif