3 <<wcsrtombs>>, <<wcsnrtombs>>---convert a wide-character string to a character string
16 size_t wcsrtombs(char *__restrict <[dst]>,
17 const wchar_t **__restrict <[src]>, size_t <[len]>,
18 mbstate_t *__restrict <[ps]>);
21 size_t _wcsrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
22 const wchar_t **<[src]>, size_t <[len]>,
26 size_t wcsnrtombs(char *__restrict <[dst]>,
27 const wchar_t **__restrict <[src]>,
28 size_t <[nwc]>, size_t <[len]>,
29 mbstate_t *__restrict <[ps]>);
32 size_t _wcsnrtombs_r(struct _reent *<[ptr]>, char *<[dst]>,
33 const wchar_t **<[src]>, size_t <[nwc]>,
34 size_t <[len]>, mbstate_t *<[ps]>);
37 The <<wcsrtombs>> function converts a string of wide characters indirectly
38 pointed to by <[src]> to a corresponding multibyte character string stored in
39 the array pointed to by <[dst]>. No more than <[len]> bytes are written to
42 If <[dst]> is NULL, no characters are stored.
44 If <[dst]> is not NULL, the pointer pointed to by <[src]> is updated to point
45 to the character after the one that conversion stopped at. If conversion
46 stops because a null character is encountered, *<[src]> is set to NULL.
48 The mbstate_t argument, <[ps]>, is used to keep track of the shift state. If
49 it is NULL, <<wcsrtombs>> uses an internal, static mbstate_t object, which
50 is initialized to the initial conversion state at program startup.
52 The <<wcsnrtombs>> function behaves identically to <<wcsrtombs>>, except that
53 conversion stops after reading at most <[nwc]> characters from the buffer
54 pointed to by <[src]>.
57 The <<wcsrtombs>> and <<wcsnrtombs>> functions return the number of bytes
58 stored in the array pointed to by <[dst]> (not including any terminating
59 null), if successful, otherwise it returns (size_t)-1.
62 <<wcsrtombs>> is defined by C99 standard.
63 <<wcsnrtombs>> is defined by the POSIX.1-2008 standard.
73 #include "../locale/setlocale.h"
75 #ifdef _REENT_THREAD_LOCAL
76 _Thread_local _mbstate_t _tls_wcsrtombs_state
;
80 _wcsnrtombs_l (struct _reent
*r
, char *dst
, const wchar_t **src
, size_t nwc
,
81 size_t len
, mbstate_t *ps
, struct __locale_t
*loc
)
93 ps
= &(_REENT_WCSRTOMBS_STATE(r
));
97 /* If no dst pointer, treat len as maximum possible value. */
102 pwcs
= (wchar_t *)(*src
);
104 while (n
< len
&& nwc
-- > 0)
106 int count
= ps
->__count
;
107 wint_t wch
= ps
->__value
.__wch
;
108 int bytes
= loc
->wctomb (r
, buff
, *pwcs
, ps
);
111 _REENT_ERRNO(r
) = EILSEQ
;
115 if (n
+ bytes
<= len
)
120 for (i
= 0; i
< bytes
; ++i
)
134 /* not enough room, we must back up state to before __WCTOMB call */
136 ps
->__value
.__wch
= wch
;
145 _wcsnrtombs_r (struct _reent
*r
,
152 return _wcsnrtombs_l (_REENT
, dst
, src
, nwc
, len
, ps
,
153 __get_current_locale ());
158 wcsnrtombs (char *__restrict dst
,
159 const wchar_t **__restrict src
,
162 mbstate_t *__restrict ps
)
164 return _wcsnrtombs_l (_REENT
, dst
, src
, nwc
, len
, ps
,
165 __get_current_locale ());
167 #endif /* !_REENT_ONLY */