After mbsrtowcs comes mbsnrtowcs. I'm committing a module that provides this function.
2008-12-21 Bruno Haible <br...@clisp.org> New module 'mbsnrtowcs'. * lib/wchar.in.h (mbsnrtowcs): New declaration. * lib/mbsnrtowcs.c: New file. * lib/mbsrtowcs-state.c: New file. * lib/mbsrtowcs.c: Refer to _gl_mbsrtowcs_state. (internal_state): Remove variable. * m4/mbsnrtowcs.m4: New file. * m4/mbsrtowcs.m4 (gl_FUNC_MBSRTOWCS): Add mbsrtowcs-state.c to the compilation units. * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_MBSNRTOWCS, HAVE_MBSNRTOWCS, REPLACE_MBSNRTOWCS. * modules/wchar (Makefile.am): Substitute GNULIB_MBSNRTOWCS, HAVE_MBSNRTOWCS, REPLACE_MBSNRTOWCS. * modules/mbsnrtowcs: New file. * modules/mbsrtowcs (Files): Add lib/mbsrtowcs-state.c. * doc/posix-functions/mbsnrtowcs.texi: Mention the new module and a portability problem. ============================== lib/mbsnrtowcs.c ============================== /* Convert string to wide string. Copyright (C) 2008 Free Software Foundation, Inc. Written by Bruno Haible <br...@clisp.org>, 2008. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> /* Specification. */ #include <wchar.h> #include <errno.h> #include <limits.h> #include <stdlib.h> #include "minmax.h" #include "strnlen1.h" extern mbstate_t _gl_mbsrtowcs_state; size_t mbsnrtowcs (wchar_t *dest, const char **srcp, size_t srclen, size_t len, mbstate_t *ps) { if (ps == NULL) ps = &_gl_mbsrtowcs_state; { const char *src = *srcp; if (dest != NULL) { wchar_t *destptr = dest; for (; srclen > 0 && len > 0; destptr++, len--) { size_t src_avail; size_t ret; /* An optimized variant of src_avail = strnlen1 (src, MIN (srclen, MB_LEN_MAX)); */ if (srclen == 1 || src[0] == '\0') src_avail = 1; else if (srclen == 2 || src[1] == '\0') src_avail = 2; else if (srclen == 3 || src[2] == '\0') src_avail = 3; else if (MB_LEN_MAX <= 4 || srclen == 4 || src[3] == '\0') src_avail = 4; else src_avail = 4 + strnlen1 (src + 4, MIN (srclen, MB_LEN_MAX) - 4); /* Parse the next multibyte character. */ ret = mbrtowc (destptr, src, src_avail, ps); if (ret == (size_t)(-2)) /* Encountered a multibyte character that extends past a '\0' byte or that is longer than MB_LEN_MAX bytes. Cannot happen. */ abort (); if (ret == (size_t)(-1)) goto bad_input; if (ret == 0) { src = NULL; /* Here mbsinit (ps). */ break; } src += ret; srclen -= ret; } *srcp = src; return destptr - dest; } else { /* Ignore dest and len, don't store *srcp at the end, and don't clobber *ps. */ mbstate_t state = *ps; size_t totalcount = 0; for (; srclen > 0; totalcount++) { size_t src_avail; size_t ret; /* An optimized variant of src_avail = strnlen1 (src, MIN (srclen, MB_LEN_MAX)); */ if (srclen == 1 || src[0] == '\0') src_avail = 1; else if (srclen == 2 || src[1] == '\0') src_avail = 2; else if (srclen == 3 || src[2] == '\0') src_avail = 3; else if (MB_LEN_MAX <= 4 || srclen == 4 || src[3] == '\0') src_avail = 4; else src_avail = 4 + strnlen1 (src + 4, MIN (srclen, MB_LEN_MAX) - 4); /* Parse the next multibyte character. */ ret = mbrtowc (NULL, src, src_avail, &state); if (ret == (size_t)(-2)) /* Encountered a multibyte character that extends past a '\0' byte or that is longer than MB_LEN_MAX bytes. Cannot happen. */ abort (); if (ret == (size_t)(-1)) goto bad_input2; if (ret == 0) { /* Here mbsinit (&state). */ break; } src += ret; srclen -= ret; } return totalcount; } bad_input: *srcp = src; bad_input2: errno = EILSEQ; return (size_t)(-1); } } =========================== lib/mbsrtowcs-state.c ============================ /* Convert string to wide string. Copyright (C) 2008 Free Software Foundation, Inc. Written by Bruno Haible <br...@clisp.org>, 2008. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <config.h> #include <wchar.h> /* Internal state used by the functions mbsrtowcs() and mbsnrtowcs(). */ mbstate_t _gl_mbsrtowcs_state; ============================== m4/mbsnrtowcs.m4 ============================== # mbsnrtowcs.m4 serial 1 dnl Copyright (C) 2008 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_MBSNRTOWCS], [ AC_REQUIRE([gl_WCHAR_H_DEFAULTS]) dnl Persuade glibc <wchar.h> to declare mbsnrtowcs(). AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) AC_REQUIRE([AC_TYPE_MBSTATE_T]) gl_MBSTATE_T_BROKEN if test $REPLACE_MBSTATE_T = 1; then REPLACE_MBSNRTOWCS=1 fi AC_CHECK_FUNCS_ONCE([mbsnrtowcs]) if test $ac_cv_func_mbsnrtowcs = no; then HAVE_MBSNRTOWCS=0 fi if test $HAVE_MBSNRTOWCS = 0 || test $REPLACE_MBSNRTOWCS = 1; then gl_REPLACE_WCHAR_H AC_LIBOBJ([mbsnrtowcs]) AC_LIBOBJ([mbsrtowcs-state]) gl_PREREQ_MBSNRTOWCS fi ]) # Prerequisites of lib/mbsnrtowcs.c. AC_DEFUN([gl_PREREQ_MBSNRTOWCS], [ : ]) ============================= modules/mbsnrtowcs ============================= Description: mbsnrtowcs() function: convert string to wide string. Files: lib/mbsnrtowcs.c lib/mbsrtowcs-state.c m4/mbsnrtowcs.m4 m4/mbstate_t.m4 Depends-on: extensions wchar mbrtowc minmax strnlen1 configure.ac: gl_FUNC_MBSNRTOWCS gl_WCHAR_MODULE_INDICATOR([mbsnrtowcs]) Makefile.am: Include: <wchar.h> License: LGPL Maintainer: Bruno Haible ============================================================================== --- lib/wchar.in.h.orig 2008-12-21 14:32:12.000000000 +0100 +++ lib/wchar.in.h 2008-12-21 13:37:35.000000000 +0100 @@ -183,6 +183,24 @@ #endif +/* Convert a string to a wide string. */ +#if @GNULIB_MBSNRTOWCS@ +# if @REPLACE_MBSNRTOWCS@ +# undef mbsnrtowcs +# define mbsnrtowcs rpl_mbsnrtowcs +# endif +# if !...@have_mbsnrtowcs@ || @REPLACE_MBSNRTOWCS@ +extern size_t mbsnrtowcs (wchar_t *dest, const char **srcp, size_t srclen, size_t len, mbstate_t *ps); +# endif +#elif defined GNULIB_POSIXCHECK +# undef mbsnrtowcs +# define mbsnrtowcs(d,s,n,l,p) \ + (GL_LINK_WARNING ("mbsnrtowcs is unportable - " \ + "use gnulib module mbsnrtowcs for portability"), \ + mbsnrtowcs (d, s, n, l, p)) +#endif + + /* Return the number of screen columns needed for WC. */ #if @GNULIB_WCWIDTH@ # if @REPLACE_WCWIDTH@ --- lib/mbsrtowcs.c.orig 2008-12-21 14:32:12.000000000 +0100 +++ lib/mbsrtowcs.c 2008-12-21 13:37:35.000000000 +0100 @@ -27,13 +27,13 @@ #include "strnlen1.h" -static mbstate_t internal_state; +extern mbstate_t _gl_mbsrtowcs_state; size_t mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps) { if (ps == NULL) - ps = &internal_state; + ps = &_gl_mbsrtowcs_state; { const char *src = *srcp; --- m4/mbsrtowcs.m4.orig 2008-12-21 14:32:12.000000000 +0100 +++ m4/mbsrtowcs.m4 2008-12-21 13:38:03.000000000 +0100 @@ -1,4 +1,4 @@ -# mbsrtowcs.m4 serial 3 +# mbsrtowcs.m4 serial 4 dnl Copyright (C) 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -27,6 +27,7 @@ if test $HAVE_MBSRTOWCS = 0 || test $REPLACE_MBSRTOWCS = 1; then gl_REPLACE_WCHAR_H AC_LIBOBJ([mbsrtowcs]) + AC_LIBOBJ([mbsrtowcs-state]) gl_PREREQ_MBSRTOWCS fi ]) --- m4/wchar.m4.orig 2008-12-21 14:32:12.000000000 +0100 +++ m4/wchar.m4 2008-12-21 13:37:35.000000000 +0100 @@ -7,7 +7,7 @@ dnl Written by Eric Blake. -# wchar.m4 serial 15 +# wchar.m4 serial 16 AC_DEFUN([gl_WCHAR_H], [ @@ -67,6 +67,7 @@ GNULIB_MBRTOWC=0; AC_SUBST([GNULIB_MBRTOWC]) GNULIB_MBRLEN=0; AC_SUBST([GNULIB_MBRLEN]) GNULIB_MBSRTOWCS=0; AC_SUBST([GNULIB_MBSRTOWCS]) + GNULIB_MBSNRTOWCS=0; AC_SUBST([GNULIB_MBSNRTOWCS]) GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC]) @@ -74,6 +75,7 @@ HAVE_MBRTOWC=1; AC_SUBST([HAVE_MBRTOWC]) HAVE_MBRLEN=1; AC_SUBST([HAVE_MBRLEN]) HAVE_MBSRTOWCS=1; AC_SUBST([HAVE_MBSRTOWCS]) + HAVE_MBSNRTOWCS=1; AC_SUBST([HAVE_MBSNRTOWCS]) HAVE_DECL_WCTOB=1; AC_SUBST([HAVE_DECL_WCTOB]) HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH]) REPLACE_MBSTATE_T=0; AC_SUBST([REPLACE_MBSTATE_T]) @@ -81,6 +83,7 @@ REPLACE_MBSINIT=0; AC_SUBST([REPLACE_MBSINIT]) REPLACE_MBRTOWC=0; AC_SUBST([REPLACE_MBRTOWC]) REPLACE_MBSRTOWCS=0; AC_SUBST([REPLACE_MBSRTOWCS]) + REPLACE_MBSNRTOWCS=0;AC_SUBST([REPLACE_MBSNRTOWCS]) REPLACE_WCWIDTH=0; AC_SUBST([REPLACE_WCWIDTH]) WCHAR_H=''; AC_SUBST([WCHAR_H]) ]) --- modules/wchar.orig 2008-12-21 14:32:12.000000000 +0100 +++ modules/wchar 2008-12-21 13:37:35.000000000 +0100 @@ -31,6 +31,7 @@ -e 's|@''GNULIB_MBRTOWC''@|$(GNULIB_MBRTOWC)|g' \ -e 's|@''GNULIB_MBRLEN''@|$(GNULIB_MBRLEN)|g' \ -e 's|@''GNULIB_MBSRTOWCS''@|$(GNULIB_MBSRTOWCS)|g' \ + -e 's|@''GNULIB_MBSNRTOWCS''@|$(GNULIB_MBSNRTOWCS)|g' \ -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \ -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \ -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \ @@ -38,6 +39,7 @@ -e 's|@''HAVE_MBRTOWC''@|$(HAVE_MBRTOWC)|g' \ -e 's|@''HAVE_MBRLEN''@|$(HAVE_MBRLEN)|g' \ -e 's|@''HAVE_MBSRTOWCS''@|$(HAVE_MBSRTOWCS)|g' \ + -e 's|@''HAVE_MBSNRTOWCS''@|$(HAVE_MBSNRTOWCS)|g' \ -e 's|@''HAVE_DECL_WCTOB''@|$(HAVE_DECL_WCTOB)|g' \ -e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \ -e 's|@''REPLACE_MBSTATE_T''@|$(REPLACE_MBSTATE_T)|g' \ @@ -45,6 +47,7 @@ -e 's|@''REPLACE_MBSINIT''@|$(REPLACE_MBSINIT)|g' \ -e 's|@''REPLACE_MBRTOWC''@|$(REPLACE_MBRTOWC)|g' \ -e 's|@''REPLACE_MBSRTOWCS''@|$(REPLACE_MBSRTOWCS)|g' \ + -e 's|@''REPLACE_MBSNRTOWCS''@|$(REPLACE_MBSNRTOWCS)|g' \ -e 's|@''REPLACE_WCWIDTH''@|$(REPLACE_WCWIDTH)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ < $(srcdir)/wchar.in.h; \ --- modules/mbsrtowcs.orig 2008-12-21 14:32:12.000000000 +0100 +++ modules/mbsrtowcs 2008-12-21 13:37:35.000000000 +0100 @@ -3,6 +3,7 @@ Files: lib/mbsrtowcs.c +lib/mbsrtowcs-state.c m4/mbsrtowcs.m4 m4/mbstate_t.m4 m4/locale-fr.m4 --- doc/posix-functions/mbsnrtowcs.texi.orig 2008-12-21 14:32:12.000000000 +0100 +++ doc/posix-functions/mbsnrtowcs.texi 2008-12-21 13:37:35.000000000 +0100 @@ -4,15 +4,21 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/mbsnrtowcs.html} -Gnulib module: --- +Gnulib module: mbsnrtowcs Portability problems fixed by Gnulib: @itemize +...@item +This function is missing on some platforms: +MacOS X 10.3, FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -MacOS X 10.3, FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS. +The specification is not clear about whether this function should update the +conversion state when the first argument (the destination pointer) is NULL. +The glibc, MacOS X, FreeBSD implementations do update the state in this case. +For portability, when passing a NULL destination argument, it is best to pass +a pointer to a temporary copy of the conversion state. @end itemize