Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #include <glob.h> |
| 2 | #include <fnmatch.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <dirent.h> |
| 5 | #include <limits.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <errno.h> |
| 9 | #include <stddef.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 10 | #include "libc.h" |
| 11 | |
| 12 | struct match |
| 13 | { |
| 14 | struct match *next; |
| 15 | char name[1]; |
| 16 | }; |
| 17 | |
| 18 | static int is_literal(const char *p, int useesc) |
| 19 | { |
| 20 | int bracket = 0; |
| 21 | for (; *p; p++) { |
| 22 | switch (*p) { |
| 23 | case '\\': |
| 24 | if (!useesc) break; |
| 25 | case '?': |
| 26 | case '*': |
| 27 | return 0; |
| 28 | case '[': |
| 29 | bracket = 1; |
| 30 | break; |
| 31 | case ']': |
| 32 | if (bracket) return 0; |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | static int append(struct match **tail, const char *name, size_t len, int mark) |
| 40 | { |
| 41 | struct match *new = malloc(sizeof(struct match) + len + 1); |
| 42 | if (!new) return -1; |
| 43 | (*tail)->next = new; |
| 44 | new->next = NULL; |
| 45 | strcpy(new->name, name); |
| 46 | if (mark) strcat(new->name, "/"); |
| 47 | *tail = new; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) |
| 52 | { |
| 53 | DIR *dir; |
Rich Felker | da88b16 | 2011-06-06 18:04:28 -0400 | [diff] [blame] | 54 | struct dirent de_buf, *de; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 55 | char pat[strlen(p)+1]; |
| 56 | char *p2; |
| 57 | size_t l = strlen(d); |
| 58 | int literal; |
Rich Felker | 787c264 | 2012-01-22 15:49:42 -0500 | [diff] [blame] | 59 | int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) |
| 60 | | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 61 | int error; |
| 62 | |
| 63 | if ((p2 = strchr(p, '/'))) { |
| 64 | strcpy(pat, p); |
| 65 | pat[p2-p] = 0; |
| 66 | for (; *p2 == '/'; p2++); |
| 67 | p = pat; |
| 68 | } |
| 69 | literal = is_literal(p, !(flags & GLOB_NOESCAPE)); |
| 70 | if (*d == '/' && !*(d+1)) l = 0; |
| 71 | |
| 72 | /* rely on opendir failing for nondirectory objects */ |
| 73 | dir = opendir(*d ? d : "."); |
| 74 | error = errno; |
| 75 | if (!dir) { |
| 76 | /* this is not an error -- we let opendir call stat for us */ |
| 77 | if (error == ENOTDIR) return 0; |
| 78 | if (error == EACCES && !*p) { |
| 79 | struct stat st; |
| 80 | if (!stat(d, &st) && S_ISDIR(st.st_mode)) { |
| 81 | if (append(tail, d, l, l)) |
| 82 | return GLOB_NOSPACE; |
| 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | if (errfunc(d, error) || (flags & GLOB_ERR)) |
| 87 | return GLOB_ABORTED; |
| 88 | return 0; |
| 89 | } |
| 90 | if (!*p) { |
| 91 | error = append(tail, d, l, l) ? GLOB_NOSPACE : 0; |
| 92 | closedir(dir); |
| 93 | return error; |
| 94 | } |
Rich Felker | da88b16 | 2011-06-06 18:04:28 -0400 | [diff] [blame] | 95 | while (!(error = readdir_r(dir, &de_buf, &de)) && de) { |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 96 | char namebuf[l+de->d_reclen+2], *name = namebuf; |
| 97 | if (!literal && fnmatch(p, de->d_name, fnm_flags)) |
| 98 | continue; |
| 99 | if (literal && strcmp(p, de->d_name)) |
| 100 | continue; |
| 101 | if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12)) |
| 102 | continue; |
Rich Felker | 8c4be3e | 2017-09-06 21:59:22 -0400 | [diff] [blame] | 103 | if (p2 && de->d_name[0]=='.' && !de->d_name[1]) |
| 104 | continue; |
| 105 | if (p2 && de->d_name[0]=='.' && de->d_name[1]=='.' && !de->d_name[2]) |
| 106 | continue; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 107 | if (*d) { |
| 108 | memcpy(name, d, l); |
| 109 | name[l] = '/'; |
| 110 | strcpy(name+l+1, de->d_name); |
| 111 | } else { |
| 112 | name = de->d_name; |
| 113 | } |
| 114 | if (p2) { |
| 115 | if ((error = match_in_dir(name, p2, flags, errfunc, tail))) { |
| 116 | closedir(dir); |
| 117 | return error; |
| 118 | } |
| 119 | } else { |
| 120 | int mark = 0; |
| 121 | if (flags & GLOB_MARK) { |
Rich Felker | d0678b5 | 2012-01-23 19:51:34 -0500 | [diff] [blame] | 122 | if (de->d_type && !S_ISLNK(de->d_type<<12)) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 123 | mark = S_ISDIR(de->d_type<<12); |
| 124 | else { |
| 125 | struct stat st; |
| 126 | stat(name, &st); |
| 127 | mark = S_ISDIR(st.st_mode); |
| 128 | } |
| 129 | } |
| 130 | if (append(tail, name, l+de->d_reclen+1, mark)) { |
| 131 | closedir(dir); |
| 132 | return GLOB_NOSPACE; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | closedir(dir); |
| 137 | if (error && (errfunc(d, error) || (flags & GLOB_ERR))) |
| 138 | return GLOB_ABORTED; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int ignore_err(const char *path, int err) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static void freelist(struct match *head) |
| 148 | { |
| 149 | struct match *match, *next; |
| 150 | for (match=head->next; match; match=next) { |
| 151 | next = match->next; |
| 152 | free(match); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static int sort(const void *a, const void *b) |
| 157 | { |
| 158 | return strcmp(*(const char **)a, *(const char **)b); |
| 159 | } |
| 160 | |
Rich Felker | 400c5e5 | 2012-09-06 22:44:55 -0400 | [diff] [blame] | 161 | int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 162 | { |
| 163 | const char *p=pat, *d; |
| 164 | struct match head = { .next = NULL }, *tail = &head; |
| 165 | size_t cnt, i; |
| 166 | size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; |
| 167 | int error = 0; |
| 168 | |
| 169 | if (*p == '/') { |
| 170 | for (; *p == '/'; p++); |
| 171 | d = "/"; |
| 172 | } else { |
| 173 | d = ""; |
| 174 | } |
| 175 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 176 | if (!errfunc) errfunc = ignore_err; |
| 177 | |
| 178 | if (!(flags & GLOB_APPEND)) { |
| 179 | g->gl_offs = offs; |
| 180 | g->gl_pathc = 0; |
| 181 | g->gl_pathv = NULL; |
| 182 | } |
| 183 | |
Rich Felker | 769f535 | 2017-01-02 19:47:12 -0500 | [diff] [blame] | 184 | if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE; |
| 185 | |
Rich Felker | 84eff79 | 2017-06-08 19:50:23 -0400 | [diff] [blame] | 186 | if (*pat) error = match_in_dir(d, p, flags, errfunc, &tail); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 187 | if (error == GLOB_NOSPACE) { |
| 188 | freelist(&head); |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++); |
| 193 | if (!cnt) { |
| 194 | if (flags & GLOB_NOCHECK) { |
| 195 | tail = &head; |
| 196 | if (append(&tail, pat, strlen(pat), 0)) |
| 197 | return GLOB_NOSPACE; |
| 198 | cnt++; |
| 199 | } else |
| 200 | return GLOB_NOMATCH; |
| 201 | } |
| 202 | |
| 203 | if (flags & GLOB_APPEND) { |
| 204 | char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); |
| 205 | if (!pathv) { |
| 206 | freelist(&head); |
| 207 | return GLOB_NOSPACE; |
| 208 | } |
| 209 | g->gl_pathv = pathv; |
| 210 | offs += g->gl_pathc; |
| 211 | } else { |
| 212 | g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); |
| 213 | if (!g->gl_pathv) { |
| 214 | freelist(&head); |
| 215 | return GLOB_NOSPACE; |
| 216 | } |
| 217 | for (i=0; i<offs; i++) |
| 218 | g->gl_pathv[i] = NULL; |
| 219 | } |
| 220 | for (i=0, tail=head.next; i<cnt; tail=tail->next, i++) |
| 221 | g->gl_pathv[offs + i] = tail->name; |
| 222 | g->gl_pathv[offs + i] = NULL; |
| 223 | g->gl_pathc += cnt; |
| 224 | |
| 225 | if (!(flags & GLOB_NOSORT)) |
| 226 | qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort); |
| 227 | |
| 228 | return error; |
| 229 | } |
| 230 | |
| 231 | void globfree(glob_t *g) |
| 232 | { |
| 233 | size_t i; |
| 234 | for (i=0; i<g->gl_pathc; i++) |
| 235 | free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); |
| 236 | free(g->gl_pathv); |
| 237 | g->gl_pathc = 0; |
| 238 | g->gl_pathv = NULL; |
| 239 | } |
| 240 | |
| 241 | LFS64(glob); |
| 242 | LFS64(globfree); |