int mblen_l(const char *s, size_t n, locale_t loc) { mbstate_t mbs = { 0 }; size_t rval; if (s == NULL) { /* No support for state dependent encodings. */ return (0); } rval = mbrtowc_l(NULL, s, n, &mbs, loc); if (rval == (size_t)-1 || rval == (size_t)-2) return (-1); return ((int)rval); }
wint_t btowc_l(int c, locale_t loc) { static const mbstate_t initial = { 0 }; mbstate_t mbs = initial; char cc; wchar_t wc; if (c == EOF) return (WEOF); /* * We expect mbrtowc() to return 0 or 1, hence the check for n > 1 * which detects error return values as well as "impossible" byte * counts. */ cc = (char)c; if (mbrtowc_l(&wc, &cc, 1, &mbs, loc) > 1) return (WEOF); return (wc); }
static int __glob(const char *pattern, glob_t *pglob) { const char *patnext; struct glob_limit limit = { 0, 0, 0, 0 }; Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot; mbstate_t mbs; wchar_t wc; size_t clen; locale_t loc = __current_locale(); int mb_cur_max = MB_CUR_MAX_L(loc); patnext = pattern; if (!(pglob->gl_flags & GLOB_APPEND)) { pglob->gl_pathc = 0; pglob->gl_pathv = NULL; if (!(pglob->gl_flags & GLOB_DOOFFS)) pglob->gl_offs = 0; } pglob->gl_matchc = 0; bufnext = patbuf; bufend = bufnext + MAXPATHLEN - 1; if (pglob->gl_flags & GLOB_NOESCAPE) { memset(&mbs, 0, sizeof(mbs)); while (bufend - bufnext >= mb_cur_max) { clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc); if (clen == (size_t)-1 || clen == (size_t)-2) return (GLOB_NOMATCH); else if (clen == 0) break; *bufnext++ = wc; patnext += clen; } } else { /* Protect the quoted characters. */ memset(&mbs, 0, sizeof(mbs)); while (bufend - bufnext >= mb_cur_max) { if (*patnext == QUOTE) { if (*++patnext == EOS) { *bufnext++ = QUOTE | M_PROTECT; continue; } prot = M_PROTECT; } else prot = 0; clen = mbrtowc_l(&wc, patnext, MB_LEN_MAX, &mbs, loc); if (clen == (size_t)-1 || clen == (size_t)-2) return (GLOB_NOMATCH); else if (clen == 0) break; *bufnext++ = wc | prot; patnext += clen; } } *bufnext = EOS; if (pglob->gl_flags & GLOB_BRACE) return globexp1(patbuf, pglob, &limit, loc); else return glob0(patbuf, pglob, &limit, loc); }