The last function in this series is 'wcsnrtombs'.
2008-12-21 Bruno Haible <br...@clisp.org> New module 'wcsnrtombs'. * lib/wchar.in.h (wcsnrtombs): New declaration. * lib/wcsnrtombs.c: New file. * lib/wcsrtombs-state.c: New file. * lib/wcsrtombs.c: Refer to _gl_wcsrtombs_state. (internal_state): Remove variable. * m4/wcsnrtombs.m4: New file. * m4/wcsrtombs.m4 (gl_FUNC_WCSRTOMBS): Add wcsrtombs-state.c to the compilation units. * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCSNRTOMBS, HAVE_WCSNRTOMBS. * modules/wchar (Makefile.am): Substitute GNULIB_WCSNRTOMBS, HAVE_WCSNRTOMBS. * modules/wcsnrtombs: New file. * modules/wcsrtombs (Files): Add lib/wcsrtombs-state.c. * doc/posix-functions/wcsnrtombs.texi: Mention the new module. ============================== lib/wcsnrtombs.c ============================== /* Convert wide string to 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 <stdlib.h> # include <string.h> extern mbstate_t _gl_wcsrtombs_state; size_t wcsnrtombs (char *dest, const wchar_t **srcp, size_t srclen, size_t len, mbstate_t *ps) { if (ps == NULL) ps = &_gl_wcsrtombs_state; { const wchar_t *src = *srcp; size_t cur_max = MB_CUR_MAX; char buf[64]; if (!(cur_max <= sizeof (buf))) abort (); if (dest != NULL) { char *destptr = dest; for (; srclen > 0 && len > 0; src++, srclen--) { wchar_t wc = *src; size_t ret = wcrtomb (len >= cur_max ? destptr : buf, wc, ps); if (ret == (size_t)(-1)) goto bad_input; if (!(ret <= cur_max)) abort (); if (len < ret) break; if (len < cur_max) memcpy (destptr, buf, ret); if (wc == 0) { src = NULL; /* Here mbsinit (ps). */ break; } destptr += ret; len -= 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; src++, srclen--) { wchar_t wc = *src; size_t ret = wcrtomb (buf, wc, &state); if (ret == (size_t)(-1)) goto bad_input2; if (wc == 0) { /* Here mbsinit (&state). */ break; } totalcount += ret; } return totalcount; } bad_input: *srcp = src; bad_input2: errno = EILSEQ; return (size_t)(-1); } } #endif ============================ lib/wcsrtombs-state.c =========================== /* Convert wide string to 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 wcsrtombs() and wcsnrtombs(). */ mbstate_t _gl_wcsrtombs_state; ============================== m4/wcsnrtombs.m4 ============================== # wcsnrtombs.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_WCSNRTOMBS], [ AC_REQUIRE([gl_WCHAR_H_DEFAULTS]) AC_REQUIRE([AC_TYPE_MBSTATE_T]) AC_CHECK_FUNCS_ONCE([wcsnrtombs]) if test $ac_cv_func_wcsnrtombs = no; then HAVE_WCSNRTOMBS=0 gl_REPLACE_WCHAR_H AC_LIBOBJ([wcsnrtombs]) AC_LIBOBJ([wcsrtombs-state]) gl_PREREQ_WCSNRTOMBS fi ]) # Prerequisites of lib/wcsnrtombs.c. AC_DEFUN([gl_PREREQ_WCSNRTOMBS], [ : ]) ============================= modules/wcsnrtombs ============================= Description: wcsnrtombs() function: convert wide string to string. Files: lib/wcsnrtombs.c lib/wcsrtombs-state.c m4/wcsnrtombs.m4 m4/mbstate_t.m4 Depends-on: wchar wcrtomb configure.ac: gl_FUNC_WCSNRTOMBS gl_WCHAR_MODULE_INDICATOR([wcsnrtombs]) Makefile.am: Include: <wchar.h> License: LGPL Maintainer: Bruno Haible ============================================================================== --- lib/wchar.in.h.orig 2008-12-22 00:33:12.000000000 +0100 +++ lib/wchar.in.h 2008-12-22 00:05:57.000000000 +0100 @@ -241,6 +241,20 @@ #endif +/* Convert a wide string to a string. */ +#if @GNULIB_WCSNRTOMBS@ +# if !...@have_wcsnrtombs@ +extern size_t wcsnrtombs (char *dest, const wchar_t **srcp, size_t srclen, size_t len, mbstate_t *ps); +# endif +#elif defined GNULIB_POSIXCHECK +# undef wcsnrtombs +# define wcsnrtombs(d,s,n,l,p) \ + (GL_LINK_WARNING ("wcsnrtombs is unportable - " \ + "use gnulib module wcsnrtombs for portability"), \ + wcsnrtombs (d, s, n, l, p)) +#endif + + /* Return the number of screen columns needed for WC. */ #if @GNULIB_WCWIDTH@ # if @REPLACE_WCWIDTH@ --- lib/wcsrtombs.c.orig 2008-12-22 00:33:12.000000000 +0100 +++ lib/wcsrtombs.c 2008-12-22 00:11:08.000000000 +0100 @@ -20,7 +20,7 @@ /* Specification. */ #include <wchar.h> -static mbstate_t internal_state; +extern mbstate_t _gl_wcsrtombs_state; #if HAVE_WCSRTOMBS && !WCSRTOMBS_TERMINATION_BUG /* Override the system's wcsrtombs() function. */ @@ -31,7 +31,7 @@ rpl_wcsrtombs (char *dest, const wchar_t **srcp, size_t len, mbstate_t *ps) { if (ps == NULL) - ps = &internal_state; + ps = &_gl_wcsrtombs_state; # if WCSRTOMBS_NULL_ARG_BUG if (dest == NULL) { @@ -55,7 +55,7 @@ wcsrtombs (char *dest, const wchar_t **srcp, size_t len, mbstate_t *ps) { if (ps == NULL) - ps = &internal_state; + ps = &_gl_wcsrtombs_state; { const wchar_t *src = *srcp; size_t cur_max = MB_CUR_MAX; --- m4/wcsrtombs.m4.orig 2008-12-22 00:33:12.000000000 +0100 +++ m4/wcsrtombs.m4 2008-12-22 00:17:03.000000000 +0100 @@ -1,4 +1,4 @@ -# wcsrtombs.m4 serial 1 +# wcsrtombs.m4 serial 2 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, @@ -31,6 +31,7 @@ if test $HAVE_WCSRTOMBS = 0 || test $REPLACE_WCSRTOMBS = 1; then gl_REPLACE_WCHAR_H AC_LIBOBJ([wcsrtombs]) + AC_LIBOBJ([wcsrtombs-state]) gl_PREREQ_WCSRTOMBS fi ]) --- m4/wchar.m4.orig 2008-12-22 00:33:12.000000000 +0100 +++ m4/wchar.m4 2008-12-22 00:06:32.000000000 +0100 @@ -7,7 +7,7 @@ dnl Written by Eric Blake. -# wchar.m4 serial 20 +# wchar.m4 serial 21 AC_DEFUN([gl_WCHAR_H], [ @@ -70,6 +70,7 @@ GNULIB_MBSNRTOWCS=0; AC_SUBST([GNULIB_MBSNRTOWCS]) GNULIB_WCRTOMB=0; AC_SUBST([GNULIB_WCRTOMB]) GNULIB_WCSRTOMBS=0; AC_SUBST([GNULIB_WCSRTOMBS]) + GNULIB_WCSNRTOMBS=0; AC_SUBST([GNULIB_WCSNRTOMBS]) GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC]) @@ -80,6 +81,7 @@ HAVE_MBSNRTOWCS=1; AC_SUBST([HAVE_MBSNRTOWCS]) HAVE_WCRTOMB=1; AC_SUBST([HAVE_WCRTOMB]) HAVE_WCSRTOMBS=1; AC_SUBST([HAVE_WCSRTOMBS]) + HAVE_WCSNRTOMBS=1; AC_SUBST([HAVE_WCSNRTOMBS]) 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]) --- modules/wchar.orig 2008-12-22 00:33:12.000000000 +0100 +++ modules/wchar 2008-12-22 00:07:03.000000000 +0100 @@ -34,6 +34,7 @@ -e 's|@''GNULIB_MBSNRTOWCS''@|$(GNULIB_MBSNRTOWCS)|g' \ -e 's|@''GNULIB_WCRTOMB''@|$(GNULIB_WCRTOMB)|g' \ -e 's|@''GNULIB_WCSRTOMBS''@|$(GNULIB_WCSRTOMBS)|g' \ + -e 's|@''GNULIB_WCSNRTOMBS''@|$(GNULIB_WCSNRTOMBS)|g' \ -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \ -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \ -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \ @@ -44,6 +45,7 @@ -e 's|@''HAVE_MBSNRTOWCS''@|$(HAVE_MBSNRTOWCS)|g' \ -e 's|@''HAVE_WCRTOMB''@|$(HAVE_WCRTOMB)|g' \ -e 's|@''HAVE_WCSRTOMBS''@|$(HAVE_WCSRTOMBS)|g' \ + -e 's|@''HAVE_WCSNRTOMBS''@|$(HAVE_WCSNRTOMBS)|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' \ --- modules/wcsrtombs.orig 2008-12-22 00:33:12.000000000 +0100 +++ modules/wcsrtombs 2008-12-22 00:09:08.000000000 +0100 @@ -3,6 +3,7 @@ Files: lib/wcsrtombs.c +lib/wcsrtombs-state.c m4/wcsrtombs.m4 m4/mbstate_t.m4 m4/locale-fr.m4 --- doc/posix-functions/wcsnrtombs.texi.orig 2008-12-22 00:33:12.000000000 +0100 +++ doc/posix-functions/wcsnrtombs.texi 2008-12-22 00:07:38.000000000 +0100 @@ -4,15 +4,15 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wcsnrtombs.html} -Gnulib module: --- +Gnulib module: wcsnrtombs 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. @end itemize