[PATCH] mbsnrtowcs/wcsnrtombs
Jeff Johnston
jjohnstn@redhat.com
Thu Feb 19 09:35:00 GMT 2009
Corinna, I have made the patch mentioned below to mbtowc_r.c so you can
just check in your changes. I also made some fixes to
open_memstream.c. The size value for open_wmemstream was not being
treated as number of wide chars by the internal static function and the
initialized buffer should have a wide char null when "wide" is set to
1. Please check that I have done this as expected. I also added some
include statements to wcstod.c to remove warnings.
Regards,
-- Jeff J.
Jeff Johnston wrote:
> Corinna Vinschen wrote:
>> On Feb 14 10:21, Corinna Vinschen wrote:
>>
>>> On Feb 13 19:35, Corinna Vinschen wrote:
>>>
>>>> Hi,
>>>>
>>>> the below patch adds the two functions mbsnrtowcs and wcsnrtombs, as
>>>> defined in BSD and POSIX.1-2008, [...]
>>>>
>>
>> Here's the entire patch again. I now added documentation for the
>> wcsrtombs, wcsnrtombs, mbsrtowcs and mbsnrtowcs functions using the
>> FreeBSD man pages as template.
>>
>>
> Corinna, the patch requires one fix to work properly. The _mbtowc_r
> function has a bug that you will trigger. It adds one to the n value
> if we are part-way through a conversion. It does so so that it may
> check for a hard-coded minimum size value and account for any bytes
> already processed (e.g. if n < 2). In your patch for mbsrtowc.c, you
> are now passing (size_t)-1 which the algorithm does not handle. It
> may add one to end up with 0 and then it will fail subsequent tests
> which is wrong. The simple fix is to add a check for n < (size_t)-1
> before ever incrementing n. With that, go ahead and check in the patch.
>
> -- Jeff J.
>
>> Corinna
>>
>>
>> * libc/include/wchar.h (mbsnrtowcs): Declare.
>> (_mbsnrtowcs_r): Declare.
>> (wcsnrtombs): Declare.
>> (_wcsnrtombs_r): Declare.
>> * libc/stdlib/Makefile.am (ELIX_2_SOURCES): Add mbsnrtowcs.c
>> and wcsnrtombs.c.
>> * libc/stdlib/Makefile.in: Regenerate.
>> * libc/stdlib/mbsnrtowcs.c: New file, implementing _mbsnrtowcs_r
>> and mbsnrtowcs. Document mbsnrtowcs and mbsrtowcs.
>> * libc/stdlib/mbsrtowcs.c (_mbsrtowcs_r): Just call _mbsnrtowcs_r.
>> (mbsrtowcs): Ditto.
>> * libc/stdlib/wcsnrtombs.c: New file, implementing _wcsnrtombs_r
>> and wcsnrtombs. Document wcsrtombs and wcsnrtombs.
>> * libc/stdlib/wcsrtombs.c (_wcsrtombs_r): Just call _wcsnrtombs_r.
>> (wcsrtombs): Ditto.
>> * libc/stdlib/stdlib.tex: Accommodate new documentation.
>>
>>
>> Index: libc/include/wchar.h
>> ===================================================================
>> RCS file: /cvs/src/src/newlib/libc/include/wchar.h,v
>> retrieving revision 1.20
>> diff -u -p -r1.20 wchar.h
>> --- libc/include/wchar.h 12 Feb 2009 23:10:01 -0000 1.20
>> +++ libc/include/wchar.h 16 Feb 2009 15:19:50 -0000
>> @@ -47,10 +47,18 @@ size_t _EXFUN(mbrtowc, (wchar_t * , cons
>> size_t _EXFUN(_mbrtowc_r, (struct _reent *, wchar_t * , const
>> char * , size_t, mbstate_t *));
>> int _EXFUN(mbsinit, (const mbstate_t *));
>> +size_t _EXFUN(mbsnrtowcs, (wchar_t * , const char ** , size_t,
>> size_t,
>> + mbstate_t *));
>> +size_t _EXFUN(_mbsnrtowcs_r, (struct _reent *, wchar_t * , const
>> char ** ,
>> + size_t, size_t, mbstate_t *));
>> size_t _EXFUN(mbsrtowcs, (wchar_t * , const char ** , size_t,
>> mbstate_t *));
>> size_t _EXFUN(_mbsrtowcs_r, (struct _reent *, wchar_t * , const
>> char ** , size_t, mbstate_t *));
>> size_t _EXFUN(wcrtomb, (char * , wchar_t, mbstate_t *));
>> size_t _EXFUN(_wcrtomb_r, (struct _reent *, char * , wchar_t,
>> mbstate_t *));
>> +size_t _EXFUN(wcsnrtombs, (char * , const wchar_t ** , size_t,
>> size_t,
>> + mbstate_t *));
>> +size_t _EXFUN(_wcsnrtombs_r, (struct _reent *, char * , const
>> wchar_t ** , + size_t, size_t, mbstate_t *));
>> size_t _EXFUN(wcsrtombs, (char * , const wchar_t ** , size_t,
>> mbstate_t *));
>> size_t _EXFUN(_wcsrtombs_r, (struct _reent *, char * , const
>> wchar_t ** , size_t, mbstate_t *));
>> Index: libc/stdlib/Makefile.am
>> ===================================================================
>> RCS file: /cvs/src/src/newlib/libc/stdlib/Makefile.am,v
>> retrieving revision 1.26
>> diff -u -p -r1.26 Makefile.am
>> --- libc/stdlib/Makefile.am 12 Feb 2009 23:10:01 -0000 1.26
>> +++ libc/stdlib/Makefile.am 16 Feb 2009 15:19:50 -0000
>> @@ -98,10 +98,12 @@ ELIX_2_SOURCES = \
>> mbrlen.c \
>> mbrtowc.c \
>> mbsinit.c \
>> + mbsnrtowcs.c \
>> mbsrtowcs.c \
>> on_exit.c \
>> valloc.c \
>> wcrtomb.c \
>> + wcsnrtombs.c \
>> wcsrtombs.c \
>> wctob.c
>>
>> Index: libc/stdlib/mbsnrtowcs.c
>> ===================================================================
>> RCS file: libc/stdlib/mbsnrtowcs.c
>> diff -N libc/stdlib/mbsnrtowcs.c
>> --- /dev/null 1 Jan 1970 00:00:00 -0000
>> +++ libc/stdlib/mbsnrtowcs.c 16 Feb 2009 15:19:50 -0000
>> @@ -0,0 +1,179 @@
>> +/*
>> +FUNCTION
>> +<<mbsrtowcs>>, <<mbsnrtowcs>>---convert a character string to a
>> wide-character string
>> +
>> +INDEX
>> + mbsrtowcs
>> +INDEX
>> + _mbsrtowcs_r
>> +INDEX
>> + mbsnrtowcs
>> +INDEX
>> + _mbsnrtowcs_r
>> +
>> +ANSI_SYNOPSIS
>> + #include <wchar.h>
>> + size_t mbsrtowcs(wchar_t *<[dst]>, const char **<[src]>, size_t
>> <[len]>,
>> + mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t _mbsrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
>> + const char **<[src]>, size_t <[len]>,
>> + mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t mbsnrtowcs(wchar_t *<[dst]>, const char **<[src]>,
>> + size_t <[nms]>, size_t <[len]>, mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t _mbsnrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
>> + const char **<[src]>, size_t <[nms]>,
>> + size_t <[len]>, mbstate_t *<[ps]>);
>> +
>> +TRAD_SYNOPSIS
>> + #include <wchar.h>
>> + size_t mbsrtowcs(<[dst]>, <[src]>, <[len]>, <[ps]>)
>> + wchar_t *<[dst]>;
>> + const char **<[src]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t _mbsrtowcs_r(<[ptr]>, <[dst]>, <[src]>, <[len]>, <[ps]>)
>> + struct _reent *<[ptr]>;
>> + wchar_t *<[dst]>;
>> + const char **<[src]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t mbsnrtowcs(<[dst]>, <[src]>, <[nms]>, <[len]>, <[ps]>)
>> + wchar_t *<[dst]>;
>> + const char **<[src]>;
>> + size_t <[nms]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t _mbsnrtowcs_r(<[ptr]>, <[dst]>, <[src]>, <[nms]>,
>> <[len]>, <[ps]>)
>> + struct _reent *<[ptr]>;
>> + wchar_t *<[dst]>;
>> + const char **<[src]>;
>> + size_t <[nms]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> +DESCRIPTION
>> +The <<mbsrtowcs>> function converts a sequence of multibyte characters
>> +pointed to indirectly by <[src]> into a sequence of corresponding wide
>> +characters and stores at most <[len]> of them in the wchar_t array
>> pointed
>> +to by <[dst]>, until it encounters a terminating null character ('\0').
>> +
>> +If <[dst]> is NULL, no characters are stored.
>> +
>> +If <[dst]> is not NULL, the pointer pointed to by <[src]> is updated
>> to point
>> +to the character after the one that conversion stopped at. If
>> conversion
>> +stops because a null character is encountered, *<[src]> is set to NULL.
>> +
>> +The mbstate_t argument, <[ps]>, is used to keep track of the shift
>> state. If
>> +it is NULL, <<mbsrtowcs>> uses an internal, static mbstate_t object,
>> which
>> +is initialized to the initial conversion state at program startup.
>> +
>> +The <<mbsnrtowcs>> function behaves identically to <<mbsrtowcs>>,
>> except that
>> +conversion stops after reading at most <[nms]> bytes from the buffer
>> pointed
>> +to by <[src]>.
>> +
>> +RETURNS
>> +The <<mbsrtowcs>> and <<mbsnrtowcs>> functions return the number of
>> wide
>> +characters stored in the array pointed to by <[dst]> if successful,
>> otherwise
>> +it returns (size_t)-1.
>> +
>> +PORTABILITY
>> +<<mbsrtowcs>> is defined by the C99 standard.
>> +<<mbsnrtowcs>> is defined by the POSIX.1-2008 standard.
>> +*/
>> +
>> +#include <reent.h>
>> +#include <newlib.h>
>> +#include <wchar.h>
>> +#include <stdlib.h>
>> +#include <stdio.h>
>> +#include <errno.h>
>> +
>> +size_t
>> +_DEFUN (_mbsnrtowcs_r, (r, dst, src, nms, len, ps), + struct
>> _reent *r _AND
>> + wchar_t *dst _AND
>> + const char **src _AND
>> + size_t nms _AND
>> + size_t len _AND
>> + mbstate_t *ps)
>> +{
>> + wchar_t *ptr = dst;
>> + const char *tmp_src;
>> + size_t max;
>> + size_t count = 0;
>> + int bytes;
>> +
>> +#ifdef _MB_CAPABLE
>> + if (ps == NULL)
>> + {
>> + _REENT_CHECK_MISC(r);
>> + ps = &(_REENT_MBSRTOWCS_STATE(r));
>> + }
>> +#endif
>> +
>> + if (dst == NULL)
>> + {
>> + /* Ignore original len value and do not alter src pointer if the
>> + dst pointer is NULL. */
>> + len = (size_t)-1;
>> + tmp_src = *src;
>> + src = &tmp_src;
>> + } + + max = len;
>> + while (len > 0)
>> + {
>> + bytes = _mbrtowc_r (r, ptr, *src, nms, ps);
>> + if (bytes > 0)
>> + {
>> + *src += bytes;
>> + nms -= bytes;
>> + ++count;
>> + ptr = (dst == NULL) ? NULL : ptr + 1;
>> + --len;
>> + }
>> + else if (bytes == -2)
>> + {
>> + *src += nms;
>> + return count;
>> + }
>> + else if (bytes == 0)
>> + {
>> + *src = NULL;
>> + return count;
>> + }
>> + else
>> + {
>> + ps->__count = 0;
>> + r->_errno = EILSEQ;
>> + return (size_t)-1;
>> + }
>> + }
>> +
>> + return (size_t)max;
>> +}
>> +
>> +#ifndef _REENT_ONLY
>> +size_t
>> +_DEFUN (mbsnrtowcs, (dst, src, nms, len, ps),
>> + wchar_t *dst _AND
>> + const char **src _AND
>> + size_t nms _AND
>> + size_t len _AND
>> + mbstate_t *ps)
>> +{
>> + return _mbsnrtowcs_r (_REENT, dst, src, nms, len, ps);
>> +}
>> +#endif /* !_REENT_ONLY */
>> Index: libc/stdlib/mbsrtowcs.c
>> ===================================================================
>> RCS file: /cvs/src/src/newlib/libc/stdlib/mbsrtowcs.c,v
>> retrieving revision 1.5
>> diff -u -p -r1.5 mbsrtowcs.c
>> --- libc/stdlib/mbsrtowcs.c 23 Apr 2004 21:44:22 -0000 1.5
>> +++ libc/stdlib/mbsrtowcs.c 16 Feb 2009 15:19:50 -0000
>> @@ -6,65 +6,14 @@
>> #include <errno.h>
>>
>> size_t
>> -_DEFUN (_mbsrtowcs_r, (r, dst, src, n, ps), +_DEFUN (_mbsrtowcs_r,
>> (r, dst, src, len, ps), struct _reent *r _AND
>> wchar_t *dst _AND
>> const char **src _AND
>> - size_t n _AND
>> + size_t len _AND
>> mbstate_t *ps)
>> {
>> - wchar_t *ptr = dst;
>> - const char *tmp_src;
>> - size_t max;
>> - size_t count = 0;
>> - int bytes;
>> -
>> -#ifdef _MB_CAPABLE
>> - if (ps == NULL)
>> - {
>> - _REENT_CHECK_MISC(r);
>> - ps = &(_REENT_MBSRTOWCS_STATE(r));
>> - }
>> -#endif
>> -
>> - if (dst == NULL)
>> - {
>> - /* Ignore original n value and do not alter src pointer if the
>> - dst pointer is NULL. */
>> - n = (size_t)-1;
>> - tmp_src = *src;
>> - src = &tmp_src;
>> - } - - max = n;
>> - while (n > 0)
>> - {
>> - bytes = _mbrtowc_r (r, ptr, *src, MB_CUR_MAX, ps);
>> - if (bytes > 0)
>> - {
>> - *src += bytes;
>> - ++count;
>> - ptr = (dst == NULL) ? NULL : ptr + 1;
>> - --n;
>> - }
>> - else if (bytes == -2)
>> - {
>> - *src += MB_CUR_MAX;
>> - }
>> - else if (bytes == 0)
>> - {
>> - *src = NULL;
>> - return count;
>> - }
>> - else
>> - {
>> - ps->__count = 0;
>> - r->_errno = EILSEQ;
>> - return (size_t)-1;
>> - }
>> - }
>> -
>> - return (size_t)max;
>> + return _mbsnrtowcs_r (r, dst, src, (size_t) -1, len, ps);
>> }
>>
>> #ifndef _REENT_ONLY
>> @@ -75,6 +24,6 @@ _DEFUN (mbsrtowcs, (dst, src, len, ps),
>> size_t len _AND
>> mbstate_t *ps)
>> {
>> - return _mbsrtowcs_r (_REENT, dst, src, len, ps);
>> + return _mbsnrtowcs_r (_REENT, dst, src, (size_t) -1, len, ps);
>> }
>> #endif /* !_REENT_ONLY */
>> Index: libc/stdlib/wcsnrtombs.c
>> ===================================================================
>> RCS file: libc/stdlib/wcsnrtombs.c
>> diff -N libc/stdlib/wcsnrtombs.c
>> --- /dev/null 1 Jan 1970 00:00:00 -0000
>> +++ libc/stdlib/wcsnrtombs.c 16 Feb 2009 15:19:50 -0000
>> @@ -0,0 +1,184 @@
>> +/*
>> +FUNCTION
>> +<<wcsrtombs>>, <<wcsnrtombs>>---convert a wide-character string to a
>> character string
>> +
>> +INDEX
>> + wcsrtombs
>> +INDEX
>> + _wcsrtombs_r
>> +INDEX
>> + wcsnrtombs
>> +INDEX
>> + _wcsnrtombs_r
>> +
>> +ANSI_SYNOPSIS
>> + #include <wchar.h>
>> + size_t wcsrtombs(char *<[dst]>, const wchar_t **<[src]>, size_t
>> <[len]>,
>> + mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t _wcsrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
>> + const wchar_t **<[src]>, size_t <[len]>,
>> + mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t wcsnrtombs(char *<[dst]>, const wchar_t **<[src]>,
>> + size_t <[nwc]>, size_t <[len]>, mbstate_t *<[ps]>);
>> +
>> + #include <wchar.h>
>> + size_t _wcsnrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
>> + const wchar_t **<[src]>, size_t <[nwc]>,
>> + size_t <[len]>, mbstate_t *<[ps]>);
>> +
>> +TRAD_SYNOPSIS
>> + #include <wchar.h>
>> + size_t wcsrtombs(<[dst]>, <[src]>, <[len]>, <[ps]>)
>> + char *<[dst]>;
>> + const wchar_t **<[src]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t _wcsrtombs_r(<[ptr]>, <[dst]>, <[src]>, <[len]>, <[ps]>)
>> + struct _rent *<[ptr]>;
>> + char *<[dst]>;
>> + const wchar_t **<[src]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t wcsnrtombs(<[dst]>, <[src]>, <[nwc]>, <[len]>, <[ps]>)
>> + char *<[dst]>;
>> + const wchar_t **<[src]>;
>> + size_t <[nwc]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> + #include <wchar.h>
>> + size_t _wcsnrtombs_r(<[ptr]>, <[dst]>, <[src]>, <[nwc]>,
>> <[len]>, <[ps]>)
>> + struct _rent *<[ptr]>;
>> + char *<[dst]>;
>> + const wchar_t **<[src]>;
>> + size_t <[nwc]>;
>> + size_t <[len]>;
>> + mbstate_t *<[ps]>;
>> +
>> +DESCRIPTION
>> +The <<wcsrtombs>> function converts a string of wide characters
>> indirectly
>> +pointed to by <[src]> to a corresponding multibyte character string
>> stored in
>> +the array pointed to by <[dst}>. No more than <[len]> bytes are
>> written to
>> +<[dst}>.
>> +
>> +If <[dst}> is NULL, no characters are stored.
>> +
>> +If <[dst}> is not NULL, the pointer pointed to by <[src]> is updated
>> to point
>> +to the character after the one that conversion stopped at. If
>> conversion
>> +stops because a null character is encountered, *<[src]> is set to NULL.
>> +
>> +The mbstate_t argument, <[ps]>, is used to keep track of the shift
>> state. If
>> +it is NULL, <<wcsrtombs>> uses an internal, static mbstate_t object,
>> which
>> +is initialized to the initial conversion state at program startup.
>> +
>> +The <<wcsnrtombs>> function behaves identically to <<wcsrtombs>>,
>> except that
>> +conversion stops after reading at most <[nwc]> characters from the
>> buffer
>> +pointed to by <[src]>.
>> +
>> +RETURNS
>> +The <<wcsrtombs>> and <<wcsnrtombs>> functions return the number of
>> bytes
>> +stored in the array pointed to by <[dst]> (not including any
>> terminating
>> +null), if successful, otherwise it returns (size_t)-1.
>> +
>> +PORTABILITY
>> +<<wcsrtombs>> is defined by C99 standard.
>> +<<wcsnrtombs>> is defined by the POSIX.1-2008 standard.
>> +*/
>> +
>> +#include <reent.h>
>> +#include <newlib.h>
>> +#include <wchar.h>
>> +#include <stdlib.h>
>> +#include <stdio.h>
>> +#include <errno.h>
>> +
>> +size_t
>> +_DEFUN (_wcsnrtombs_r, (r, dst, src, nwc, len, ps),
>> + struct _reent *r _AND
>> + char *dst _AND
>> + const wchar_t **src _AND
>> + size_t nwc _AND
>> + size_t len _AND
>> + mbstate_t *ps)
>> +{
>> + char *ptr = dst;
>> + char buff[10];
>> + wchar_t *pwcs;
>> + size_t n;
>> + int i;
>> +
>> +#ifdef _MB_CAPABLE
>> + if (ps == NULL)
>> + {
>> + _REENT_CHECK_MISC(r);
>> + ps = &(_REENT_WCSRTOMBS_STATE(r));
>> + }
>> +#endif
>> +
>> + /* If no dst pointer, treat len as maximum possible value. */
>> + if (dst == NULL)
>> + len = (size_t)-1;
>> +
>> + n = 0;
>> + pwcs = (wchar_t *)(*src);
>> +
>> + while (n < len && nwc-- > 0)
>> + {
>> + int count = ps->__count;
>> + wint_t wch = ps->__value.__wch;
>> + int bytes = _wcrtomb_r (r, buff, *pwcs, ps);
>> + if (bytes == -1)
>> + {
>> + r->_errno = EILSEQ;
>> + ps->__count = 0;
>> + return (size_t)-1;
>> + }
>> + if (n + bytes <= len)
>> + {
>> + n += bytes;
>> + if (dst)
>> + {
>> + for (i = 0; i < bytes; ++i)
>> + *ptr++ = buff[i];
>> + ++(*src);
>> + }
>> + if (*pwcs++ == 0x00)
>> + {
>> + if (dst)
>> + *src = NULL;
>> + ps->__count = 0;
>> + return n - 1;
>> + }
>> + }
>> + else
>> + {
>> + /* not enough room, we must back up state to before _wctomb_r
>> call */
>> + ps->__count = count;
>> + ps->__value.__wch = wch;
>> + len = 0;
>> + }
>> + }
>> +
>> + return n;
>> +} +
>> +#ifndef _REENT_ONLY
>> +size_t
>> +_DEFUN (wcsnrtombs, (dst, src, nwc, len, ps),
>> + char *dst _AND
>> + const wchar_t **src _AND
>> + size_t nwc _AND
>> + size_t len _AND
>> + mbstate_t *ps)
>> +{
>> + return _wcsnrtombs_r (_REENT, dst, src, nwc, len, ps);
>> +}
>> +#endif /* !_REENT_ONLY */
>> Index: libc/stdlib/wcsrtombs.c
>> ===================================================================
>> RCS file: /cvs/src/src/newlib/libc/stdlib/wcsrtombs.c,v
>> retrieving revision 1.8
>> diff -u -p -r1.8 wcsrtombs.c
>> --- libc/stdlib/wcsrtombs.c 28 Aug 2008 17:36:49 -0000 1.8
>> +++ libc/stdlib/wcsrtombs.c 16 Feb 2009 15:19:50 -0000
>> @@ -1,9 +1,6 @@
>> #include <reent.h>
>> #include <newlib.h>
>> #include <wchar.h>
>> -#include <stdlib.h>
>> -#include <stdio.h>
>> -#include <errno.h>
>>
>> size_t
>> _DEFUN (_wcsrtombs_r, (r, dst, src, len, ps),
>> @@ -13,65 +10,7 @@ _DEFUN (_wcsrtombs_r, (r, dst, src, len,
>> size_t len _AND
>> mbstate_t *ps)
>> {
>> - char *ptr = dst;
>> - char buff[10];
>> - wchar_t *pwcs;
>> - size_t n;
>> - int i;
>> -
>> -#ifdef _MB_CAPABLE
>> - if (ps == NULL)
>> - {
>> - _REENT_CHECK_MISC(r);
>> - ps = &(_REENT_WCSRTOMBS_STATE(r));
>> - }
>> -#endif
>> -
>> - /* If no dst pointer, treat len as maximum possible value. */
>> - if (dst == NULL)
>> - len = (size_t)-1;
>> -
>> - n = 0;
>> - pwcs = (wchar_t *)(*src);
>> -
>> - while (n < len)
>> - {
>> - int count = ps->__count;
>> - wint_t wch = ps->__value.__wch;
>> - int bytes = _wcrtomb_r (r, buff, *pwcs, ps);
>> - if (bytes == -1)
>> - {
>> - r->_errno = EILSEQ;
>> - ps->__count = 0;
>> - return (size_t)-1;
>> - }
>> - if (n + bytes <= len)
>> - {
>> - n += bytes;
>> - if (dst)
>> - {
>> - for (i = 0; i < bytes; ++i)
>> - *ptr++ = buff[i];
>> - ++(*src);
>> - }
>> - if (*pwcs++ == 0x00)
>> - {
>> - if (dst)
>> - *src = NULL;
>> - ps->__count = 0;
>> - return n - 1;
>> - }
>> - }
>> - else
>> - {
>> - /* not enough room, we must back up state to before _wctomb_r
>> call */
>> - ps->__count = count;
>> - ps->__value.__wch = wch;
>> - len = 0;
>> - }
>> - }
>> -
>> - return n;
>> + return _wcsnrtombs_r (r, dst, src, (size_t) -1, len, ps);
>> }
>> #ifndef _REENT_ONLY
>> @@ -82,6 +21,6 @@ _DEFUN (wcsrtombs, (dst, src, len, ps),
>> size_t len _AND
>> mbstate_t *ps)
>> {
>> - return _wcsrtombs_r (_REENT, dst, src, len, ps);
>> + return _wcsnrtombs_r (_REENT, dst, src, (size_t) -1, len, ps);
>> }
>> #endif /* !_REENT_ONLY */
>> Index: libc/stdlib/stdlib.tex
>> ===================================================================
>> RCS file: /cvs/src/src/newlib/libc/stdlib/stdlib.tex,v
>> retrieving revision 1.8
>> diff -u -p -r1.8 stdlib.tex
>> --- libc/stdlib/stdlib.tex 12 Feb 2009 23:10:01 -0000 1.8
>> +++ libc/stdlib/stdlib.tex 16 Feb 2009 15:19:50 -0000
>> @@ -29,6 +29,7 @@ The corresponding declarations are in th
>> * malloc:: Allocate and manage memory (malloc, realloc, free)
>> * mallinfo:: Get information about allocated memory
>> * __malloc_lock:: Lock memory pool for malloc and free
>> +* mbsnrtowcs:: Convert a character string to a wide-character string
>> * mbstowcs:: Minimal multibyte string to wide string converter
>> * mblen:: Minimal multibyte length
>> * mbtowc:: Minimal multibyte to wide character converter
>> @@ -40,6 +41,7 @@ The corresponding declarations are in th
>> * strtoll:: String to long long
>> * strtoul:: String to unsigned long
>> * strtoull:: String to unsigned long long
>> +* wcsnrtombs:: Convert a wide-character string to a character string
>> * wcstod:: Wide string to double or float
>> * wcstol:: Wide string to long
>> * wcstoll:: Wide string to long long
>> @@ -123,6 +125,9 @@ The corresponding declarations are in th
>> @include stdlib/mblen.def
>>
>> @page
>> +@include stdlib/mbsnrtowcs.def
>> +
>> +@page
>> @include stdlib/mbstowcs.def
>>
>> @page
>> @@ -153,6 +158,9 @@ The corresponding declarations are in th
>> @include stdlib/strtoull.def
>>
>> @page
>> +@include stdlib/wcsnrtombs.def
>> +
>> +@page
>> @include stdlib/wcstod.def
>>
>> @page
>>
>>
>>
>
More information about the Newlib
mailing list