summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2023-03-31 00:27:20 +0200
committerBruno Haible <bruno@clisp.org>2023-03-31 00:27:20 +0200
commit7595a8817e03c1366817e69aa74263efa2ca4979 (patch)
tree46d5a26730a36675ee9b9600d86e72f08fa61064
parent8906a5101bf62b719ddafa376fd4e0805e35617b (diff)
downloadgnulib-7595a8817e03c1366817e69aa74263efa2ca4979.tar.gz
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.
-rw-r--r--ChangeLog16
-rw-r--r--doc/posix-functions/mbstowcs.texi6
-rw-r--r--lib/mbstowcs.c33
-rw-r--r--lib/stdlib.in.h30
-rw-r--r--m4/mbstowcs.m421
-rw-r--r--m4/stdlib_h.m48
-rw-r--r--modules/mbstowcs33
-rw-r--r--modules/stdlib2
-rw-r--r--tests/test-stdlib-c++.cc5
9 files changed, 150 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 4afba58a3c..bc7372479c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
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.
* m4/mbsnrtowcs.m4 (gl_FUNC_MBSNRTOWCS): Invoke gl_MBRTOWC_C_LOCALE. If
mbrtowc is buggy in the C locale, override also mbsnrtowcs.
diff --git a/doc/posix-functions/mbstowcs.texi b/doc/posix-functions/mbstowcs.texi
index 5d7dc4ca62..a695edc2af 100644
--- a/doc/posix-functions/mbstowcs.texi
+++ b/doc/posix-functions/mbstowcs.texi
@@ -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
index 0000000000..e32d9acf88
--- /dev/null
+++ b/lib/mbstowcs.c
@@ -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);
+}
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index a91f4e23d6..4ecfc96a6f 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -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
index 0000000000..c66e804f80
--- /dev/null
+++ b/m4/mbstowcs.m4
@@ -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], [
+ :
+])
diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4
index 249ef65722..ac28ed9efc 100644
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -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
index 0000000000..44ba43d977
--- /dev/null
+++ b/modules/mbstowcs
@@ -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
diff --git a/modules/stdlib b/modules/stdlib
index bafeb214ee..efea327794 100644
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -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' \
diff --git a/tests/test-stdlib-c++.cc b/tests/test-stdlib-c++.cc
index 184437c5fd..7683afd154 100644
--- a/tests/test-stdlib-c++.cc
+++ b/tests/test-stdlib-c++.cc
@@ -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