18 static int append(struct match **tail, const char *name, size_t len, int mark)
20 struct match *new = malloc(sizeof(struct match) + len + 2);
24 memcpy(new->name, name, len+1);
25 if (mark && len && name[len-1]!='/') {
33 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)
35 /* If GLOB_MARK is unused, we don't care about type. */
36 if (!type && !(flags & GLOB_MARK)) type = DT_REG;
38 /* Special-case the remaining pattern being all slashes, in
39 * which case we can use caller-passed type if it's a dir. */
40 if (*pat && type!=DT_DIR) type = 0;
41 while (pos+1 < PATH_MAX && *pat=='/') buf[pos++] = *pat++;
43 /* Consume maximal [escaped-]literal prefix of pattern, copying
44 * and un-escaping it to the running buffer as we go. */
46 int in_bracket = 0, overflow = 0;
47 for (; pat[i]!='*' && pat[i]!='?' && (!in_bracket || pat[i]!=']'); i++) {
49 if (overflow) return 0;
54 } else if (pat[i] == '[') {
56 } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) {
57 /* Backslashes inside a bracket are (at least by
58 * our interpretation) non-special, so if next
59 * char is ']' we have a complete expression. */
60 if (in_bracket && pat[i+1]==']') break;
61 /* Unpaired final backslash never matches. */
62 if (!pat[i+1]) return 0;
66 if (overflow) return 0;
73 /* Only store a character if it fits in the buffer, but if
74 * a potential bracket expression is open, the overflow
75 * must be remembered and handled later only if the bracket
76 * is unterminated (and thereby a literal), so as not to
77 * disallow long bracket expressions with short matches. */
78 if (pos+(j+1) < PATH_MAX) {
79 buf[pos+j++] = pat[i];
80 } else if (in_bracket) {
85 /* If we consume any new components, the caller-passed type
86 * or dummy type from above is no longer valid. */
91 /* If we consumed any components above, or if GLOB_MARK is
92 * requested and we don't yet know if the match is a dir,
93 * we must call stat to confirm the file exists and/or
94 * determine its type. */
96 if ((flags & GLOB_MARK) && type==DT_LNK) type = 0;
97 if (!type && stat(buf, &st)) {
98 if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR)))
102 if (!type && S_ISDIR(st.st_mode)) type = DT_DIR;
103 if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR))
107 char *p2 = strchr(pat, '/'), saved_sep = '/';
108 /* Check if the '/' was escaped and, if so, remove the escape char
109 * so that it will not be unpaired when passed to fnmatch. */
110 if (p2 && !(flags & GLOB_NOESCAPE)) {
112 for (p=p2; p>pat && p[-1]=='\\'; p--);
118 DIR *dir = opendir(pos ? buf : ".");
120 if (errfunc(buf, errno) || (flags & GLOB_ERR))
124 int old_errno = errno;
126 while (errno=0, de=readdir(dir)) {
127 /* Quickly skip non-directories when there's pattern left. */
128 if (p2 && de->d_type && de->d_type!=DT_DIR && de->d_type!=DT_LNK)
131 size_t l = strlen(de->d_name);
132 if (l >= PATH_MAX-pos) continue;
136 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
137 | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
139 if (fnmatch(pat, de->d_name, fnm_flags))
142 /* With GLOB_PERIOD, don't allow matching . or .. unless
143 * fnmatch would match them with FNM_PERIOD rules in effect. */
144 if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
145 && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
146 && fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD))
149 memcpy(buf+pos, de->d_name, l+1);
150 if (p2) *p2 = saved_sep;
151 int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail);
158 if (p2) *p2 = saved_sep;
160 if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR)))
166 static int ignore_err(const char *path, int err)
171 static void freelist(struct match *head)
173 struct match *match, *next;
174 for (match=head->next; match; match=next) {
180 static int sort(const void *a, const void *b)
182 return strcmp(*(const char **)a, *(const char **)b);
185 int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
187 struct match head = { .next = NULL }, *tail = &head;
189 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
193 if (!errfunc) errfunc = ignore_err;
195 if (!(flags & GLOB_APPEND)) {
202 char *p = strdup(pat);
203 if (!p) return GLOB_NOSPACE;
205 error = do_glob(buf, 0, 0, p, flags, errfunc, &tail);
209 if (error == GLOB_NOSPACE) {
214 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
216 if (flags & GLOB_NOCHECK) {
218 if (append(&tail, pat, strlen(pat), 0))
225 if (flags & GLOB_APPEND) {
226 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
234 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
239 for (i=0; i<offs; i++)
240 g->gl_pathv[i] = NULL;
242 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
243 g->gl_pathv[offs + i] = tail->name;
244 g->gl_pathv[offs + i] = NULL;
247 if (!(flags & GLOB_NOSORT))
248 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
253 void globfree(glob_t *g)
256 for (i=0; i<g->gl_pathc; i++)
257 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
263 weak_alias(glob, glob64);
264 weak_alias(globfree, globfree64);