- s = *src;
- nbytes = 0;
-
- if (dst == NULL) {
- for (;;) {
- if ((nb = (int)wcrtomb(buf, *s, NULL)) < 0)
- /* Invalid character - wcrtomb() sets errno. */
- return ((size_t)-1);
- else if (*s == L'\0')
- return (nbytes + nb - 1);
- s++;
- nbytes += nb;
- }
- /*NOTREACHED*/
- }
-
- while (len > 0) {
- if (len > (size_t)MB_CUR_MAX) {
- /* Enough space to translate in-place. */
- if ((nb = (int)wcrtomb(dst, *s, NULL)) < 0) {
- *src = s;
- return ((size_t)-1);
- }
- } else {
- /* May not be enough space; use temp. buffer. */
- if ((nb = (int)wcrtomb(buf, *s, NULL)) < 0) {
- *src = s;
- return ((size_t)-1);
- }
- if (nb > (int)len)
- /* MB sequence for character won't fit. */
- break;
- memcpy(dst, buf, nb);
- }
- if (*s == L'\0') {
- *src = NULL;
- return (nbytes + nb - 1);
- }
- s++;
- dst += nb;
- len -= nb;
- nbytes += nb;
- }
- *src = s;
- return (nbytes);
+ if (ps == NULL)
+ ps = &mbs;
+ return (__wcsnrtombs(dst, src, SIZE_T_MAX, len, ps));