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 confirm the file exists and/or determine its type.
97 * If marking dirs, symlink type is inconclusive; we need the
98 * type for the symlink target, and therefore must try stat
99 * first unless type is known not to be a symlink. Otherwise,
100 * or if that fails, use lstat for determining existence to
101 * avoid false negatives in the case of broken symlinks. */
103 if ((flags
& GLOB_MARK
) && (!type
||type
==DT_LNK
) && !stat(buf
, &st
)) {
104 if (S_ISDIR(st
.st_mode
)) type
= DT_DIR
;
107 if (!type
&& lstat(buf
, &st
)) {
108 if (errno
!=ENOENT
&& (errfunc(buf
, errno
) || (flags
& GLOB_ERR
)))
112 if (append(tail
, buf
, pos
, (flags
& GLOB_MARK
) && type
==DT_DIR
))
116 char *p2
= strchr(pat
, '/'), saved_sep
= '/';
117 /* Check if the '/' was escaped and, if so, remove the escape char
118 * so that it will not be unpaired when passed to fnmatch. */
119 if (p2
&& !(flags
& GLOB_NOESCAPE
)) {
121 for (p
=p2
; p
>pat
&& p
[-1]=='\\'; p
--);
127 DIR *dir
= opendir(pos
? buf
: ".");
129 if (errfunc(buf
, errno
) || (flags
& GLOB_ERR
))
133 int old_errno
= errno
;
135 while (errno
=0, de
=readdir(dir
)) {
136 /* Quickly skip non-directories when there's pattern left. */
137 if (p2
&& de
->d_type
&& de
->d_type
!=DT_DIR
&& de
->d_type
!=DT_LNK
)
140 size_t l
= strlen(de
->d_name
);
141 if (l
>= PATH_MAX
-pos
) continue;
145 int fnm_flags
= ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
146 | ((!(flags
& GLOB_PERIOD
)) ? FNM_PERIOD
: 0);
148 if (fnmatch(pat
, de
->d_name
, fnm_flags
))
151 /* With GLOB_PERIOD, don't allow matching . or .. unless
152 * fnmatch would match them with FNM_PERIOD rules in effect. */
153 if (p2
&& (flags
& GLOB_PERIOD
) && de
->d_name
[0]=='.'
154 && (!de
->d_name
[1] || de
->d_name
[1]=='.' && !de
->d_name
[2])
155 && fnmatch(pat
, de
->d_name
, fnm_flags
| FNM_PERIOD
))
158 memcpy(buf
+pos
, de
->d_name
, l
+1);
159 if (p2
) *p2
= saved_sep
;
160 int r
= do_glob(buf
, pos
+l
, de
->d_type
, p2
? p2
: "", flags
, errfunc
, tail
);
167 if (p2
) *p2
= saved_sep
;
169 if (readerr
&& (errfunc(buf
, errno
) || (flags
& GLOB_ERR
)))
175 static int ignore_err(const char *path
, int err
)
180 static void freelist(struct match
*head
)
182 struct match
*match
, *next
;
183 for (match
=head
->next
; match
; match
=next
) {
189 static int sort(const void *a
, const void *b
)
191 return strcmp(*(const char **)a
, *(const char **)b
);
194 static int expand_tilde(char **pat
, char *buf
, size_t *pos
)
199 char delim
, *name_end
= __strchrnul(p
, '/');
200 if ((delim
= *name_end
)) *name_end
++ = 0;
203 char *home
= *p
? NULL
: getenv("HOME");
205 struct passwd pw
, *res
;
206 switch (*p
? getpwnam_r(p
, &pw
, buf
, PATH_MAX
, &res
)
207 : getpwuid_r(getuid(), &pw
, buf
, PATH_MAX
, &res
)) {
217 while (i
< PATH_MAX
- 2 && *home
)
221 if ((buf
[i
] = delim
))
227 int glob(const char *restrict pat
, int flags
, int (*errfunc
)(const char *path
, int err
), glob_t
*restrict g
)
229 struct match head
= { .next
= NULL
}, *tail
= &head
;
231 size_t offs
= (flags
& GLOB_DOOFFS
) ? g
->gl_offs
: 0;
235 if (!errfunc
) errfunc
= ignore_err
;
237 if (!(flags
& GLOB_APPEND
)) {
244 char *p
= strdup(pat
);
245 if (!p
) return GLOB_NOSPACE
;
249 if ((flags
& (GLOB_TILDE
| GLOB_TILDE_CHECK
)) && *p
== '~')
250 error
= expand_tilde(&s
, buf
, &pos
);
252 error
= do_glob(buf
, pos
, 0, s
, flags
, errfunc
, &tail
);
256 if (error
== GLOB_NOSPACE
) {
261 for (cnt
=0, tail
=head
.next
; tail
; tail
=tail
->next
, cnt
++);
263 if (flags
& GLOB_NOCHECK
) {
265 if (append(&tail
, pat
, strlen(pat
), 0))
272 if (flags
& GLOB_APPEND
) {
273 char **pathv
= realloc(g
->gl_pathv
, (offs
+ g
->gl_pathc
+ cnt
+ 1) * sizeof(char *));
281 g
->gl_pathv
= malloc((offs
+ cnt
+ 1) * sizeof(char *));
286 for (i
=0; i
<offs
; i
++)
287 g
->gl_pathv
[i
] = NULL
;
289 for (i
=0, tail
=head
.next
; i
<cnt
; tail
=tail
->next
, i
++)
290 g
->gl_pathv
[offs
+ i
] = tail
->name
;
291 g
->gl_pathv
[offs
+ i
] = NULL
;
294 if (!(flags
& GLOB_NOSORT
))
295 qsort(g
->gl_pathv
+offs
, cnt
, sizeof(char *), sort
);
300 void globfree(glob_t
*g
)
303 for (i
=0; i
<g
->gl_pathc
; i
++)
304 free(g
->gl_pathv
[g
->gl_offs
+ i
] - offsetof(struct match
, name
));