20 static int is_literal(const char *p, int useesc)
34 if (bracket) return 0;
41 static int append(struct match **tail, const char *name, size_t len, int mark)
43 struct match *new = malloc(sizeof(struct match) + len + 1);
47 strcpy(new->name, name);
48 if (mark) strcat(new->name, "/");
53 static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
56 long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
58 char pat[strlen(p)+1];
62 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
65 if ((p2 = strchr(p, '/'))) {
68 for (; *p2 == '/'; p2++);
71 literal = is_literal(p, !(flags & GLOB_NOESCAPE));
72 if (*d == '/' && !*(d+1)) l = 0;
74 /* rely on opendir failing for nondirectory objects */
75 dir = opendir(*d ? d : ".");
78 /* this is not an error -- we let opendir call stat for us */
79 if (error == ENOTDIR) return 0;
80 if (error == EACCES && !*p) {
82 if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
83 if (append(tail, d, l, l))
88 if (errfunc(d, error) || (flags & GLOB_ERR))
93 error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
97 while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
98 char namebuf[l+de->d_reclen+2], *name = namebuf;
99 if (!literal && fnmatch(p, de->d_name, fnm_flags))
101 if (literal && strcmp(p, de->d_name))
103 if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
108 strcpy(name+l+1, de->d_name);
113 if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
119 if (flags & GLOB_MARK) {
121 mark = S_ISDIR(de->d_type<<12);
125 mark = S_ISDIR(st.st_mode);
128 if (append(tail, name, l+de->d_reclen+1, mark)) {
135 if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
140 static int ignore_err(const char *path, int err)
145 static void freelist(struct match *head)
147 struct match *match, *next;
148 for (match=head->next; match; match=next) {
154 static int sort(const void *a, const void *b)
156 return strcmp(*(const char **)a, *(const char **)b);
159 int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
161 const char *p=pat, *d;
162 struct match head = { .next = NULL }, *tail = &head;
164 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
168 for (; *p == '/'; p++);
174 if (!errfunc) errfunc = ignore_err;
176 if (!(flags & GLOB_APPEND)) {
182 if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
183 if (error == GLOB_NOSPACE) {
188 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
190 if (flags & GLOB_NOCHECK) {
192 if (append(&tail, pat, strlen(pat), 0))
199 if (flags & GLOB_APPEND) {
200 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
208 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
213 for (i=0; i<offs; i++)
214 g->gl_pathv[i] = NULL;
216 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
217 g->gl_pathv[offs + i] = tail->name;
218 g->gl_pathv[offs + i] = NULL;
221 if (!(flags & GLOB_NOSORT))
222 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
227 void globfree(glob_t *g)
230 for (i=0; i<g->gl_pathc; i++)
231 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));