- const char *e;
- rune_t r;
-
- if (s == NULL) {
- pwc = NULL;
- s = "";
- n = 1;
- }
-
- if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE) {
- /*
- * The design of sgetrune() doesn't give us any way to tell
- * between incomplete and invalid multibyte sequences.
- */
-
- if (n >= (size_t)MB_CUR_MAX) {
- /*
- * If we have been supplied with at least MB_CUR_MAX
- * bytes and still cannot find a valid character, the
- * data must be invalid.
- */
- errno = EILSEQ;
- return ((size_t)-1);
- }
-
- /*
- * .. otherwise, it's an incomplete character or an invalid
- * character we cannot detect yet.
- */
- return ((size_t)-2);
- }
-
- if (pwc != NULL)
- *pwc = (wchar_t)r;
+ NORMALIZE_LOCALE(loc);
+ if (ps == NULL)
+ ps = &loc->__mbs_mbrtowc;
+ return (loc->__lc_ctype->__mbrtowc(pwc, s, n, ps, loc));
+}