- const char *e;
- int cnt;
- rune_t r;
-
- if (s == NULL) {
- errno = EINVAL;
- return (-1);
- }
-
- if (pwcs == NULL) {
- /* Convert and count only, do not store. */
- cnt = 0;
- while ((r = sgetrune(s, MB_LEN_MAX, &e)) != _INVALID_RUNE &&
- r != 0) {
- s = e;
- cnt++;
- }
- if (r == _INVALID_RUNE) {
- errno = EILSEQ;
- return (-1);
- }
- return (cnt);
- }
-
- /* Convert, store and count characters. */
- cnt = 0;
- while (n-- > 0) {
- *pwcs = sgetrune(s, MB_LEN_MAX, &e);
- if (*pwcs == _INVALID_RUNE) {
- errno = EILSEQ;
- return (-1);
- }
- if (*pwcs++ == L'\0')
- break;
- s = e;
- ++cnt;
- }