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
] == '/') {
57 if (overflow
) return 0;
63 } else if (pat
[i
] == '\\' && !(flags
& GLOB_NOESCAPE
)) {
64 /* Backslashes inside a bracket are (at least by
65 * our interpretation) non-special, so if next
66 * char is ']' we have a complete expression. */
67 if (in_bracket
&& pat
[i
+1]==']') break;
68 /* Unpaired final backslash never matches. */
69 if (!pat
[i
+1] || pat
[i
+1]=='/') return 0;
72 /* Only store a character if it fits in the buffer, but if
73 * a potential bracket expression is open, the overflow
74 * must be remembered and handled later only if the bracket
75 * is unterminated (and thereby a literal), so as not to
76 * disallow long bracket expressions with short matches. */
77 if (pos
+(j
+1) < PATH_MAX
) {
78 buf
[pos
+j
++] = pat
[i
];
79 } else if (in_bracket
) {
84 /* If we consume any new components, the caller-passed type
85 * or dummy type from above is no longer valid. */
90 /* If we consumed any components above, or if GLOB_MARK is
91 * requested and we don't yet know if the match is a dir,
92 * we must call stat to confirm the file exists and/or
93 * determine its type. */
95 if ((flags
& GLOB_MARK
) && type
==DT_LNK
) type
= 0;
96 if (!type
&& stat(buf
, &st
)) {
97 if (errno
!=ENOENT
&& (errfunc(buf
, errno
) || (flags
& GLOB_ERR
)))
101 if (!type
&& S_ISDIR(st
.st_mode
)) type
= DT_DIR
;
102 if (append(tail
, buf
, pos
, (flags
& GLOB_MARK
) && type
==DT_DIR
))
106 char *p2
= strchr(pat
, '/');
107 DIR *dir
= opendir(pos
? buf
: ".");
109 if (errfunc(buf
, errno
) || (flags
& GLOB_ERR
))
113 int old_errno
= errno
;
115 while (errno
=0, de
=readdir(dir
)) {
116 /* Quickly skip non-directories when there's pattern left. */
117 if (p2
&& de
->d_type
&& de
->d_type
!=DT_DIR
&& de
->d_type
!=DT_LNK
)
120 size_t l
= strlen(de
->d_name
);
121 if (l
>= PATH_MAX
-pos
) continue;
125 int fnm_flags
= ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
126 | ((!(flags
& GLOB_PERIOD
)) ? FNM_PERIOD
: 0);
128 if (fnmatch(pat
, de
->d_name
, fnm_flags
))
131 /* With GLOB_PERIOD, don't allow matching . or .. unless
132 * fnmatch would match them with FNM_PERIOD rules in effect. */
133 if (p2
&& (flags
& GLOB_PERIOD
) && de
->d_name
[0]=='.'
134 && (!de
->d_name
[1] || de
->d_name
[1]=='.' && !de
->d_name
[2])
135 && fnmatch(pat
, de
->d_name
, fnm_flags
| FNM_PERIOD
))
138 memcpy(buf
+pos
, de
->d_name
, l
+1);
140 int r
= do_glob(buf
, pos
+l
, de
->d_type
, p2
? p2
: "", flags
, errfunc
, tail
);
149 if (readerr
&& (errfunc(buf
, errno
) || (flags
& GLOB_ERR
)))
155 static int ignore_err(const char *path
, int err
)
160 static void freelist(struct match
*head
)
162 struct match
*match
, *next
;
163 for (match
=head
->next
; match
; match
=next
) {
169 static int sort(const void *a
, const void *b
)
171 return strcmp(*(const char **)a
, *(const char **)b
);
174 int glob(const char *restrict pat
, int flags
, int (*errfunc
)(const char *path
, int err
), glob_t
*restrict g
)
176 struct match head
= { .next
= NULL
}, *tail
= &head
;
178 size_t offs
= (flags
& GLOB_DOOFFS
) ? g
->gl_offs
: 0;
182 if (!errfunc
) errfunc
= ignore_err
;
184 if (!(flags
& GLOB_APPEND
)) {
191 char *p
= strdup(pat
);
192 if (!p
) return GLOB_NOSPACE
;
194 error
= do_glob(buf
, 0, 0, p
, flags
, errfunc
, &tail
);
198 if (error
== GLOB_NOSPACE
) {
203 for (cnt
=0, tail
=head
.next
; tail
; tail
=tail
->next
, cnt
++);
205 if (flags
& GLOB_NOCHECK
) {
207 if (append(&tail
, pat
, strlen(pat
), 0))
214 if (flags
& GLOB_APPEND
) {
215 char **pathv
= realloc(g
->gl_pathv
, (offs
+ g
->gl_pathc
+ cnt
+ 1) * sizeof(char *));
223 g
->gl_pathv
= malloc((offs
+ cnt
+ 1) * sizeof(char *));
228 for (i
=0; i
<offs
; i
++)
229 g
->gl_pathv
[i
] = NULL
;
231 for (i
=0, tail
=head
.next
; i
<cnt
; tail
=tail
->next
, i
++)
232 g
->gl_pathv
[offs
+ i
] = tail
->name
;
233 g
->gl_pathv
[offs
+ i
] = NULL
;
236 if (!(flags
& GLOB_NOSORT
))
237 qsort(g
->gl_pathv
+offs
, cnt
, sizeof(char *), sort
);
242 void globfree(glob_t
*g
)
245 for (i
=0; i
<g
->gl_pathc
; i
++)
246 free(g
->gl_pathv
[g
->gl_offs
+ i
] - offsetof(struct match
, name
));
252 weak_alias(glob
, glob64
);
253 weak_alias(globfree
, globfree64
);