Lines Matching +full:dc +full:- +full:current +full:- +full:limit
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
41 * glob(3) -- a superset of the one defined in POSIX 1003.2.
62 * Number of matches in the current invocation of glob.
67 * 1. Patterns with illegal byte sequences match nothing - even if
70 * single-byte characters with a values of such bytes of the sequence
72 * 3. State-dependent encodings are not currently supported.
96 * also CVE-2010-2632
118 #define RANGE L'-'
141 #define M_RNG META(L'-')
186 struct glob_limit limit = { 0, 0, 0, 0, 0 }; in glob() local
196 pglob->gl_pathc = 0; in glob()
197 pglob->gl_pathv = NULL; in glob()
199 pglob->gl_offs = 0; in glob()
202 limit.l_path_lim = pglob->gl_matchc; in glob()
203 if (limit.l_path_lim == 0) in glob()
204 limit.l_path_lim = GLOB_LIMIT_PATH; in glob()
206 pglob->gl_flags = flags & ~GLOB_MAGCHAR; in glob()
207 pglob->gl_errfunc = errfunc; in glob()
208 pglob->gl_matchc = 0; in glob()
211 bufend = bufnext + MAXPATHLEN - 1; in glob()
217 if (clen == (size_t)-1 || clen == (size_t)-2) in glob()
218 return (err_nomatch(pglob, &limit, pattern)); in glob()
239 if (clen == (size_t)-1 || clen == (size_t)-2) in glob()
240 return (err_nomatch(pglob, &limit, pattern)); in glob()
250 return (err_nomatch(pglob, &limit, pattern)); in glob()
254 return (globexp0(patbuf, pglob, &limit, pattern)); in glob()
256 return (glob0(patbuf, pglob, &limit, pattern)); in glob()
260 globexp0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, in globexp0() argument
267 if ((pglob->gl_flags & GLOB_LIMIT) && in globexp0()
268 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { in globexp0()
272 return (glob0(pattern, pglob, limit, origpat)); in globexp0()
275 oldpathc = pglob->gl_pathc; in globexp0()
277 if ((rv = globexp1(pattern, pglob, limit)) != 0) in globexp0()
280 return (globfinal(pglob, limit, oldpathc, origpat)); in globexp0()
289 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) in globexp1() argument
294 if ((pglob->gl_flags & GLOB_LIMIT) && in globexp1()
295 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) { in globexp1()
299 return (globexp2(ptr, pattern, pglob, limit)); in globexp1()
302 return (glob0(pattern, pglob, limit, NULL)); in globexp1()
313 struct glob_limit *limit) in globexp2() argument
345 i--; in globexp2()
350 return (glob0(pattern, pglob, limit, NULL)); in globexp2()
373 i--; in globexp2()
381 /* Append the current string */ in globexp2()
391 /* Expand the current pattern */ in globexp2()
395 rv = globexp1(patbuf, pglob, limit); in globexp2()
424 wchar_t *wbufend, *dc; in globtilde() local
429 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) in globtilde()
435 eb = &patbuf[patbuf_len - 1]; in globtilde()
456 h = pwd->pw_dir; in globtilde()
470 h = pwd->pw_dir; in globtilde()
474 dc = wbuf; in globtilde()
476 wbufend = wbuf + MAXPATHLEN - 1; in globtilde()
479 while (dc <= wbufend) { in globtilde()
481 if (clen == (size_t)-1 || clen == (size_t)-2) { in globtilde()
487 if ((*dc++ = wc) == EOS) { in globtilde()
496 dc = wbuf; in globtilde()
497 for (b = patbuf; b < eb && *dc != EOS; *b++ = *dc++ | M_PROTECT) in globtilde()
499 if (*dc != EOS) in globtilde()
527 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit, in glob0() argument
539 oldpathc = pglob->gl_pathc; in glob0()
553 --qpatnext; in glob0()
569 pglob->gl_flags |= GLOB_MAGCHAR; in glob0()
573 pglob->gl_flags |= GLOB_MAGCHAR; in glob0()
577 pglob->gl_flags |= GLOB_MAGCHAR; in glob0()
582 if (bufnext == patbuf || bufnext[-1] != M_ALL) in glob0()
595 if ((err = glob1(patbuf, pglob, limit)) != 0) in glob0()
599 return (globfinal(pglob, limit, oldpathc, origpat)); in glob0()
605 globfinal(glob_t *pglob, struct glob_limit *limit, size_t oldpathc, in globfinal() argument
607 if (pglob->gl_pathc == oldpathc) in globfinal()
608 return (err_nomatch(pglob, limit, origpat)); in globfinal()
610 if (!(pglob->gl_flags & GLOB_NOSORT)) in globfinal()
611 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, in globfinal()
612 pglob->gl_pathc - oldpathc, sizeof(char *), compare); in globfinal()
624 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) in glob1() argument
628 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ in glob1()
631 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, in glob1()
632 pattern, pglob, limit)); in glob1()
642 glob_t *pglob, struct glob_limit *limit) in glob2() argument
658 if ((pglob->gl_flags & GLOB_LIMIT) && in glob2()
659 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) { in glob2()
663 if ((pglob->gl_flags & GLOB_MARK) && in glob2()
664 UNPROT(pathend[-1]) != SEP && in glob2()
676 ++pglob->gl_matchc; in glob2()
677 return (globextend(pathbuf, pglob, limit, NULL)); in glob2()
705 p, pglob, limit)); in glob2()
713 glob_t *pglob, struct glob_limit *limit) in glob3() argument
718 char buf[MAXPATHLEN + MB_LEN_MAX - 1]; in glob3()
727 if (pglob->gl_errfunc != NULL && in glob3()
746 /* pglob->gl_readdir takes a void *, fix this manually */ in glob3()
747 if (pglob->gl_flags & GLOB_ALTDIRFUNC) in glob3()
748 readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir; in glob3()
756 Char *dc; in glob3() local
761 if ((pglob->gl_flags & GLOB_LIMIT) && in glob3()
762 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) { in glob3()
769 if (dp->d_name[0] == '.' && UNPROT(*pattern) != DOT) { in glob3()
774 dc = pathend; in glob3()
775 sc = dp->d_name; in glob3()
777 while (dc <= pathend_last) { in glob3()
779 if (clen == (size_t)-1 || clen == (size_t)-2) { in glob3()
785 if ((*dc++ = wc) == EOS) { in glob3()
803 err = glob2(pathbuf, --dc, pathend_last, restpattern, in glob3()
804 pglob, limit); in glob3()
811 if (pglob->gl_flags & GLOB_ALTDIRFUNC) in glob3()
812 (*pglob->gl_closedir)(dirp); in glob3()
835 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
845 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit, in globextend() argument
853 if ((pglob->gl_flags & GLOB_LIMIT) && in globextend()
854 pglob->gl_matchc > limit->l_path_lim) { in globextend()
859 newn = 2 + pglob->gl_pathc + pglob->gl_offs; in globextend()
861 pathv = reallocarray(pglob->gl_pathv, newn, sizeof(*pathv)); in globextend()
865 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { in globextend()
866 /* first time around -- clear initial gl_offs items */ in globextend()
867 pathv += pglob->gl_offs; in globextend()
868 for (i = pglob->gl_offs + 1; --i > 0; ) in globextend()
869 *--pathv = NULL; in globextend()
871 pglob->gl_pathv = pathv; in globextend()
878 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */ in globextend()
888 limit->l_string_cnt += strlen(copy) + 1; in globextend()
889 if ((pglob->gl_flags & GLOB_LIMIT) && in globextend()
890 limit->l_string_cnt >= GLOB_LIMIT_STRING) { in globextend()
895 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; in globextend()
897 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; in globextend()
910 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; in match()
925 nextp = pat - 1; in match()
940 if (table->__collate_load_error ? in match()
979 if (pglob->gl_pathv != NULL) { in globfree()
980 pp = pglob->gl_pathv + pglob->gl_offs; in globfree()
981 for (i = pglob->gl_pathc; i--; ++pp) in globfree()
984 free(pglob->gl_pathv); in globfree()
985 pglob->gl_pathv = NULL; in globfree()
992 char buf[MAXPATHLEN + MB_LEN_MAX - 1]; in g_opendir()
1003 if (pglob->gl_flags & GLOB_ALTDIRFUNC) in g_opendir()
1004 return ((*pglob->gl_opendir)(buf)); in g_opendir()
1012 char buf[MAXPATHLEN + MB_LEN_MAX - 1]; in g_lstat()
1016 return (-1); in g_lstat()
1018 if (pglob->gl_flags & GLOB_ALTDIRFUNC) in g_lstat()
1019 return((*pglob->gl_lstat)(buf, sb)); in g_lstat()
1026 char buf[MAXPATHLEN + MB_LEN_MAX - 1]; in g_stat()
1030 return (-1); in g_stat()
1032 if (pglob->gl_flags & GLOB_ALTDIRFUNC) in g_stat()
1033 return ((*pglob->gl_stat)(buf, sb)); in g_stat()
1057 if (clen == (size_t)-1) { in g_Ctoc()
1067 len -= clen; in g_Ctoc()
1073 err_nomatch(glob_t *pglob, struct glob_limit *limit, const char *origpat) { in err_nomatch() argument
1080 if ((pglob->gl_flags & GLOB_NOCHECK) || in err_nomatch()
1081 ((pglob->gl_flags & GLOB_NOMAGIC) && in err_nomatch()
1082 !(pglob->gl_flags & GLOB_MAGCHAR))) in err_nomatch()
1083 return (globextend(NULL, pglob, limit, origpat)); in err_nomatch()
1089 if ((pglob->gl_errfunc != NULL && pglob->gl_errfunc(buf, err)) || in err_aborted()
1090 (pglob->gl_flags & GLOB_ERR)) in err_aborted()