2 * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
9 #ifdef __UCLIBC_HAS_LFS__
34 extern int __glob_is_literal(const char *p, int useesc) attribute_hidden;
35 extern int __glob_append(struct match **tail, const char *name, size_t len, int mark) attribute_hidden;
36 extern int __glob_ignore_err(const char *path, int err) attribute_hidden;
37 extern void __glob_freelist(struct match *head) attribute_hidden;
38 extern int __glob_sort(const void *a, const void *b) attribute_hidden;
39 extern int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) attribute_hidden;
42 #ifdef __UCLIBC_HAS_LFS__
44 # define readdir_r readdir64_r
45 # define dirent dirent64
46 # define struct_stat struct stat64
48 # define struct_stat struct stat
51 /* keep only one copy of these */
57 int __glob_is_literal(const char *p, int useesc)
71 if (bracket) return 0;
81 int __glob_append(struct match **tail, const char *name, size_t len, int mark)
83 struct match *new = malloc(sizeof(struct match) + len + 1);
87 strcpy(new->name, name);
88 if (mark) strcat(new->name, "/");
96 int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
99 long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
101 char pat[strlen(p)+1];
103 size_t l = strlen(d);
105 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
108 if ((p2 = strchr(p, '/'))) {
111 for (; *p2 == '/'; p2++);
114 literal = __glob_is_literal(p, !(flags & GLOB_NOESCAPE));
115 if (*d == '/' && !*(d+1)) l = 0;
117 /* rely on opendir failing for nondirectory objects */
118 dir = opendir(*d ? d : ".");
121 /* this is not an error -- we let opendir call stat for us */
122 if (error == ENOTDIR) return 0;
123 if (error == EACCES && !*p) {
125 if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
126 if (__glob_append(tail, d, l, l))
131 if (errfunc(d, error) || (flags & GLOB_ERR))
136 error = __glob_append(tail, d, l, l) ? GLOB_NOSPACE : 0;
140 while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
141 char namebuf[l+de->d_reclen+2], *name = namebuf;
142 if (!literal && fnmatch(p, de->d_name, fnm_flags))
144 if (literal && strcmp(p, de->d_name))
146 if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
151 strcpy(name+l+1, de->d_name);
156 if ((error = __glob_match_in_dir(name, p2, flags, errfunc, tail))) {
162 if (flags & GLOB_MARK) {
164 mark = S_ISDIR(de->d_type<<12);
168 mark = S_ISDIR(st.st_mode);
171 if (__glob_append(tail, name, l+de->d_reclen+1, mark)) {
178 if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
183 # ifndef BUILD_GLOB64
186 int __glob_ignore_err(const char * path attribute_unused,
187 int err attribute_unused)
192 # ifndef BUILD_GLOB64
195 void __glob_freelist(struct match *head)
197 struct match *match, *next;
198 for (match=head->next; match; match=next) {
204 # ifndef BUILD_GLOB64
207 int __glob_sort(const void *a, const void *b)
209 return strcmp(*(const char **)a, *(const char **)b);
211 #endif /* !__GLOB64 */
213 int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
215 const char *p=pat, *d;
216 struct match head = { .next = NULL }, *tail = &head;
218 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
222 for (; *p == '/'; p++);
228 if (!errfunc) errfunc = __glob_ignore_err;
230 if (!(flags & GLOB_APPEND)) {
236 if (*p) error = __glob_match_in_dir(d, p, flags, errfunc, &tail);
237 if (error == GLOB_NOSPACE) {
238 __glob_freelist(&head);
242 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
244 if (flags & GLOB_NOCHECK) {
246 if (__glob_append(&tail, pat, strlen(pat), 0))
253 if (flags & GLOB_APPEND) {
254 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
256 __glob_freelist(&head);
262 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
264 __glob_freelist(&head);
267 for (i=0; i<offs; i++)
268 g->gl_pathv[i] = NULL;
270 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
271 g->gl_pathv[offs + i] = tail->name;
272 g->gl_pathv[offs + i] = NULL;
275 if (!(flags & GLOB_NOSORT))
276 qsort(g->gl_pathv+offs, cnt, sizeof(char *), __glob_sort);
281 libc_hidden_def(glob64)
283 libc_hidden_def(glob)
286 void globfree(glob_t *g)
289 for (i=0; i<g->gl_pathc; i++)
290 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
296 libc_hidden_def(globfree64)
298 libc_hidden_def(globfree)