From: Bruno Haible Date: Thu, 30 Mar 2023 22:27:20 +0000 (+0200) Subject: mbstowcs: New module. X-Git-Tag: v1.0~1540 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=7595a8817e03c1366817e69aa74263efa2ca4979;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 4afba58a3c..bc7372479c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2023-03-30 Bruno Haible + + 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 mbsnrtowcs: Fix behaviour in the C locale. 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 , 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 . */ + +#include + +/* Specification. */ +#include + +#include +#include + +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: + + +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