20 static int append(struct match **tail, const char *name, size_t len, int mark)
22 struct match *new = malloc(sizeof(struct match) + len + 2);
26 memcpy(new->name, name, len+1);
27 if (mark && len && name[len-1]!='/') {
35 static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
37 /* If GLOB_MARK is unused, we don't care about type. */
38 if (!type && !(flags & GLOB_MARK)) type = DT_REG;
40 /* Special-case the remaining pattern being all slashes, in
41 * which case we can use caller-passed type if it's a dir. */
42 if (*pat && type!=DT_DIR) type = 0;
43 while (pos+1 < PATH_MAX && *pat=='/') buf[pos++] = *pat++;
45 /* Consume maximal [escaped-]literal prefix of pattern, copying
46 * and un-escaping it to the running buffer as we go. */
48 int in_bracket = 0, overflow = 0;
49 for (; pat[i]!='*' && pat[i]!='?' && (!in_bracket || pat[i]!=']'); i++) {
51 if (overflow) return 0;
56 } else if (pat[i] == '[') {
58 } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) {
59 /* Backslashes inside a bracket are (at least by
60 * our interpretation) non-special, so if next
61 * char is ']' we have a complete expression. */
62 if (in_bracket && pat[i+1]==']') break;
63 /* Unpaired final backslash never matches. */
64 if (!pat[i+1]) return 0;
68 if (overflow) return 0;
75 /* Only store a character if it fits in the buffer, but if
76 * a potential bracket expression is open, the overflow
77 * must be remembered and handled later only if the bracket
78 * is unterminated (and thereby a literal), so as not to
79 * disallow long bracket expressions with short matches. */
80 if (pos+(j+1) < PATH_MAX) {
81 buf[pos+j++] = pat[i];
82 } else if (in_bracket) {
87 /* If we consume any new components, the caller-passed type
88 * or dummy type from above is no longer valid. */
93 /* If we consumed any components above, or if GLOB_MARK is
94 * requested and we don't yet know if the match is a dir,
95 * we must call stat to confirm the file exists and/or
96 * determine its type. */
98 if ((flags & GLOB_MARK) && type==DT_LNK) type = 0;
99 if (!type && stat(buf, &st)) {
100 if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR)))
104 if (!type && S_ISDIR(st.st_mode)) type = DT_DIR;
105 if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR))
109 char *p2 = strchr(pat, '/'), saved_sep = '/';
110 /* Check if the '/' was escaped and, if so, remove the escape char
111 * so that it will not be unpaired when passed to fnmatch. */
112 if (p2 && !(flags & GLOB_NOESCAPE)) {
114 for (p=p2; p>pat && p[-1]=='\\'; p--);
120 DIR *dir = opendir(pos ? buf : ".");
122 if (errfunc(buf, errno) || (flags & GLOB_ERR))
126 int old_errno = errno;
128 while (errno=0, de=readdir(dir)) {
129 /* Quickly skip non-directories when there's pattern left. */
130 if (p2 && de->d_type && de->d_type!=DT_DIR && de->d_type!=DT_LNK)
133 size_t l = strlen(de->d_name);
134 if (l >= PATH_MAX-pos) continue;
138 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
139 | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
141 if (fnmatch(pat, de->d_name, fnm_flags))
144 /* With GLOB_PERIOD, don't allow matching . or .. unless
145 * fnmatch would match them with FNM_PERIOD rules in effect. */
146 if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
147 && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
148 && fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD))
151 memcpy(buf+pos, de->d_name, l+1);
152 if (p2) *p2 = saved_sep;
153 int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail);
160 if (p2) *p2 = saved_sep;
162 if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR)))
168 static int ignore_err(const char *path, int err)
173 static void freelist(struct match *head)
175 struct match *match, *next;
176 for (match=head->next; match; match=next) {
182 static int sort(const void *a, const void *b)
184 return strcmp(*(const char **)a, *(const char **)b);
187 static int expand_tilde(char **pat, char *buf, size_t *pos)
192 char delim, *name_end = __strchrnul(p, '/');
193 if ((delim = *name_end)) *name_end++ = 0;
196 char *home = *p ? NULL : getenv("HOME");
198 struct passwd pw, *res;
199 switch (*p ? getpwnam_r(p, &pw, buf, PATH_MAX, &res)
200 : getpwuid_r(getuid(), &pw, buf, PATH_MAX, &res)) {
210 while (i < PATH_MAX - 2 && *home)
214 if ((buf[i] = delim))
220 int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
222 struct match head = { .next = NULL }, *tail = &head;
224 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
228 if (!errfunc) errfunc = ignore_err;
230 if (!(flags & GLOB_APPEND)) {
237 char *p = strdup(pat);
238 if (!p) return GLOB_NOSPACE;
242 if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~')
243 error = expand_tilde(&s, buf, &pos);
245 error = do_glob(buf, pos, 0, s, flags, errfunc, &tail);
249 if (error == GLOB_NOSPACE) {
254 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
256 if (flags & GLOB_NOCHECK) {
258 if (append(&tail, pat, strlen(pat), 0))
265 if (flags & GLOB_APPEND) {
266 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
274 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
279 for (i=0; i<offs; i++)
280 g->gl_pathv[i] = NULL;
282 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
283 g->gl_pathv[offs + i] = tail->name;
284 g->gl_pathv[offs + i] = NULL;
287 if (!(flags & GLOB_NOSORT))
288 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
293 void globfree(glob_t *g)
296 for (i=0; i<g->gl_pathc; i++)
297 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
303 weak_alias(glob, glob64);
304 weak_alias(globfree, globfree64);