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 struct dirent de_buf, *de;
57 char pat[strlen(p)+1];
61 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
62 | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
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, &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) {
120 if (de->d_type && !S_ISLNK(de->d_type<<12))
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 *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict 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 (strlen(p) > PATH_MAX) return GLOB_NOSPACE;
176 if (!errfunc) errfunc = ignore_err;
178 if (!(flags & GLOB_APPEND)) {
184 if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
185 if (error == GLOB_NOSPACE) {
190 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
192 if (flags & GLOB_NOCHECK) {
194 if (append(&tail, pat, strlen(pat), 0))
201 if (flags & GLOB_APPEND) {
202 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
210 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
215 for (i=0; i<offs; i++)
216 g->gl_pathv[i] = NULL;
218 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
219 g->gl_pathv[offs + i] = tail->name;
220 g->gl_pathv[offs + i] = NULL;
223 if (!(flags & GLOB_NOSORT))
224 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
229 void globfree(glob_t *g)
232 for (i=0; i<g->gl_pathc; i++)
233 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));