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) | FNM_PERIOD;
64 if ((p2 = strchr(p, '/'))) {
67 for (; *p2 == '/'; p2++);
70 literal = is_literal(p, !(flags & GLOB_NOESCAPE));
71 if (*d == '/' && !*(d+1)) l = 0;
73 /* rely on opendir failing for nondirectory objects */
74 dir = opendir(*d ? d : ".");
77 /* this is not an error -- we let opendir call stat for us */
78 if (error == ENOTDIR) return 0;
79 if (error == EACCES && !*p) {
81 if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
82 if (append(tail, d, l, l))
87 if (errfunc(d, error) || (flags & GLOB_ERR))
92 error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
96 while (!(error = readdir_r(dir, &de_buf, &de)) && de) {
97 char namebuf[l+de->d_reclen+2], *name = namebuf;
98 if (!literal && fnmatch(p, de->d_name, fnm_flags))
100 if (literal && strcmp(p, de->d_name))
102 if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
107 strcpy(name+l+1, de->d_name);
112 if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
118 if (flags & GLOB_MARK) {
120 mark = S_ISDIR(de->d_type<<12);
124 mark = S_ISDIR(st.st_mode);
127 if (append(tail, name, l+de->d_reclen+1, mark)) {
134 if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
139 static int ignore_err(const char *path, int err)
144 static void freelist(struct match *head)
146 struct match *match, *next;
147 for (match=head->next; match; match=next) {
153 static int sort(const void *a, const void *b)
155 return strcmp(*(const char **)a, *(const char **)b);
158 int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
160 const char *p=pat, *d;
161 struct match head = { .next = NULL }, *tail = &head;
163 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
167 for (; *p == '/'; p++);
173 if (strlen(p) > PATH_MAX) return GLOB_NOSPACE;
175 if (!errfunc) errfunc = ignore_err;
177 if (!(flags & GLOB_APPEND)) {
183 if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
184 if (error == GLOB_NOSPACE) {
189 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
191 if (flags & GLOB_NOCHECK) {
193 if (append(&tail, pat, strlen(pat), 0))
200 if (flags & GLOB_APPEND) {
201 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
209 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
214 for (i=0; i<offs; i++)
215 g->gl_pathv[i] = NULL;
217 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
218 g->gl_pathv[offs + i] = tail->name;
219 g->gl_pathv[offs + i] = NULL;
222 if (!(flags & GLOB_NOSORT))
223 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
228 void globfree(glob_t *g)
231 for (i=0; i<g->gl_pathc; i++)
232 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));