| 1 | #define _BSD_SOURCE |
| 2 | #include <glob.h> |
| 3 | #include <fnmatch.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <dirent.h> |
| 6 | #include <limits.h> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <errno.h> |
| 10 | #include <stddef.h> |
| 11 | #include <unistd.h> |
| 12 | #ifdef __wasilibc_unmodified_upstream // WASI has no usernames |
| 13 | #include <pwd.h> |
| 14 | #endif |
| 15 | |
| 16 | struct match |
| 17 | { |
| 18 | 	struct match *next; |
| 19 | 	char name[]; |
| 20 | }; |
| 21 | |
| 22 | static int append(struct match **tail, const char *name, size_t len, int mark) |
| 23 | { |
| 24 | 	struct match *new = malloc(sizeof(struct match) + len + 2); |
| 25 | 	if (!new) return -1; |
| 26 | 	(*tail)->next = new; |
| 27 | 	new->next = NULL; |
| 28 | 	memcpy(new->name, name, len+1); |
| 29 | 	if (mark && len && name[len-1]!='/') { |
| 30 | 		new->name[len] = '/'; |
| 31 | 		new->name[len+1] = 0; |
| 32 | 	} |
| 33 | 	*tail = new; |
| 34 | 	return 0; |
| 35 | } |
| 36 | |
| 37 | static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*errfunc)(const char *path, int err), struct match **tail) |
| 38 | { |
| 39 | 	/* If GLOB_MARK is unused, we don't care about type. */ |
| 40 | 	if (!type && !(flags & GLOB_MARK)) type = DT_REG; |
| 41 | |
| 42 | 	/* Special-case the remaining pattern being all slashes, in |
| 43 | 	 * which case we can use caller-passed type if it's a dir. */ |
| 44 | 	if (*pat && type!=DT_DIR) type = 0; |
| 45 | 	while (pos+1 < PATH_MAX && *pat=='/') buf[pos++] = *pat++; |
| 46 | |
| 47 | 	/* Consume maximal [escaped-]literal prefix of pattern, copying |
| 48 | 	 * and un-escaping it to the running buffer as we go. */ |
| 49 | 	ptrdiff_t i=0, j=0; |
| 50 | 	int in_bracket = 0, overflow = 0; |
| 51 | 	for (; pat[i]!='*' && pat[i]!='?' && (!in_bracket || pat[i]!=']'); i++) { |
| 52 | 		if (!pat[i]) { |
| 53 | 			if (overflow) return 0; |
| 54 | 			pat += i; |
| 55 | 			pos += j; |
| 56 | 			i = j = 0; |
| 57 | 			break; |
| 58 | 		} else if (pat[i] == '[') { |
| 59 | 			in_bracket = 1; |
| 60 | 		} else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) { |
| 61 | 			/* Backslashes inside a bracket are (at least by |
| 62 | 			 * our interpretation) non-special, so if next |
| 63 | 			 * char is ']' we have a complete expression. */ |
| 64 | 			if (in_bracket && pat[i+1]==']') break; |
| 65 | 			/* Unpaired final backslash never matches. */ |
| 66 | 			if (!pat[i+1]) return 0; |
| 67 | 			i++; |
| 68 | 		} |
| 69 | 		if (pat[i] == '/') { |
| 70 | 			if (overflow) return 0; |
| 71 | 			in_bracket = 0; |
| 72 | 			pat += i+1; |
| 73 | 			i = -1; |
| 74 | 			pos += j+1; |
| 75 | 			j = -1; |
| 76 | 		} |
| 77 | 		/* Only store a character if it fits in the buffer, but if |
| 78 | 		 * a potential bracket expression is open, the overflow |
| 79 | 		 * must be remembered and handled later only if the bracket |
| 80 | 		 * is unterminated (and thereby a literal), so as not to |
| 81 | 		 * disallow long bracket expressions with short matches. */ |
| 82 | 		if (pos+(j+1) < PATH_MAX) { |
| 83 | 			buf[pos+j++] = pat[i]; |
| 84 | 		} else if (in_bracket) { |
| 85 | 			overflow = 1; |
| 86 | 		} else { |
| 87 | 			return 0; |
| 88 | 		} |
| 89 | 		/* If we consume any new components, the caller-passed type |
| 90 | 		 * or dummy type from above is no longer valid. */ |
| 91 | 		type = 0; |
| 92 | 	} |
| 93 | 	buf[pos] = 0; |
| 94 | 	if (!*pat) { |
| 95 | 		/* If we consumed any components above, or if GLOB_MARK is |
| 96 | 		 * requested and we don't yet know if the match is a dir, |
| 97 | 		 * we must confirm the file exists and/or determine its type. |
| 98 | 		 * |
| 99 | 		 * If marking dirs, symlink type is inconclusive; we need the |
| 100 | 		 * type for the symlink target, and therefore must try stat |
| 101 | 		 * first unless type is known not to be a symlink. Otherwise, |
| 102 | 		 * or if that fails, use lstat for determining existence to |
| 103 | 		 * avoid false negatives in the case of broken symlinks. */ |
| 104 | 		struct stat st; |
| 105 | 		if ((flags & GLOB_MARK) && (!type||type==DT_LNK) && !stat(buf, &st)) { |
| 106 | 			if (S_ISDIR(st.st_mode)) type = DT_DIR; |
| 107 | 			else type = DT_REG; |
| 108 | 		} |
| 109 | 		if (!type && lstat(buf, &st)) { |
| 110 | 			if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR))) |
| 111 | 				return GLOB_ABORTED; |
| 112 | 			return 0; |
| 113 | 		} |
| 114 | 		if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR)) |
| 115 | 			return GLOB_NOSPACE; |
| 116 | 		return 0; |
| 117 | 	} |
| 118 | 	char *p2 = strchr(pat, '/'), saved_sep = '/'; |
| 119 | 	/* Check if the '/' was escaped and, if so, remove the escape char |
| 120 | 	 * so that it will not be unpaired when passed to fnmatch. */ |
| 121 | 	if (p2 && !(flags & GLOB_NOESCAPE)) { |
| 122 | 		char *p; |
| 123 | 		for (p=p2; p>pat && p[-1]=='\\'; p--); |
| 124 | 		if ((p2-p)%2) { |
| 125 | 			p2--; |
| 126 | 			saved_sep = '\\'; |
| 127 | 		} |
| 128 | 	} |
| 129 | 	DIR *dir = opendir(pos ? buf : "."); |
| 130 | 	if (!dir) { |
| 131 | 		if (errfunc(buf, errno) || (flags & GLOB_ERR)) |
| 132 | 			return GLOB_ABORTED; |
| 133 | 		return 0; |
| 134 | 	} |
| 135 | 	int old_errno = errno; |
| 136 | 	struct dirent *de; |
| 137 | 	while (errno=0, de=readdir(dir)) { |
| 138 | 		/* Quickly skip non-directories when there's pattern left. */ |
| 139 | 		if (p2 && de->d_type && de->d_type!=DT_DIR && de->d_type!=DT_LNK) |
| 140 | 			continue; |
| 141 | |
| 142 | 		size_t l = strlen(de->d_name); |
| 143 | 		if (l >= PATH_MAX-pos) continue; |
| 144 | |
| 145 | 		if (p2) *p2 = 0; |
| 146 | |
| 147 | 		int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) |
| 148 | 			| ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); |
| 149 | |
| 150 | 		if (fnmatch(pat, de->d_name, fnm_flags)) |
| 151 | 			continue; |
| 152 | |
| 153 | 		/* With GLOB_PERIOD, don't allow matching . or .. unless |
| 154 | 		 * fnmatch would match them with FNM_PERIOD rules in effect. */ |
| 155 | 		if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.' |
| 156 | 		 && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2]) |
| 157 | 		 && fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD)) |
| 158 | 			continue; |
| 159 | |
| 160 | 		memcpy(buf+pos, de->d_name, l+1); |
| 161 | 		if (p2) *p2 = saved_sep; |
| 162 | 		int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail); |
| 163 | 		if (r) { |
| 164 | 			closedir(dir); |
| 165 | 			return r; |
| 166 | 		} |
| 167 | 	} |
| 168 | 	int readerr = errno; |
| 169 | 	if (p2) *p2 = saved_sep; |
| 170 | 	closedir(dir); |
| 171 | 	if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR))) |
| 172 | 		return GLOB_ABORTED; |
| 173 | 	errno = old_errno; |
| 174 | 	return 0; |
| 175 | } |
| 176 | |
| 177 | static int ignore_err(const char *path, int err) |
| 178 | { |
| 179 | 	return 0; |
| 180 | } |
| 181 | |
| 182 | static void freelist(struct match *head) |
| 183 | { |
| 184 | 	struct match *match, *next; |
| 185 | 	for (match=head->next; match; match=next) { |
| 186 | 		next = match->next; |
| 187 | 		free(match); |
| 188 | 	} |
| 189 | } |
| 190 | |
| 191 | static int sort(const void *a, const void *b) |
| 192 | { |
| 193 | 	return strcmp(*(const char **)a, *(const char **)b); |
| 194 | } |
| 195 | |
| 196 | #ifdef __wasilibc_unmodified_upstream // WASI has no usernames |
| 197 | static int expand_tilde(char **pat, char *buf, size_t *pos) |
| 198 | { |
| 199 | 	char *p = *pat + 1; |
| 200 | 	size_t i = 0; |
| 201 | |
| 202 | 	char delim, *name_end = __strchrnul(p, '/'); |
| 203 | 	if ((delim = *name_end)) *name_end++ = 0; |
| 204 | 	*pat = name_end; |
| 205 | |
| 206 | 	char *home = *p ? NULL : getenv("HOME"); |
| 207 | 	if (!home) { |
| 208 | 		struct passwd pw, *res; |
| 209 | 		switch (*p ? getpwnam_r(p, &pw, buf, PATH_MAX, &res) |
| 210 | 			 : getpwuid_r(getuid(), &pw, buf, PATH_MAX, &res)) { |
| 211 | 		case ENOMEM: |
| 212 | 			return GLOB_NOSPACE; |
| 213 | 		case 0: |
| 214 | 			if (!res) |
| 215 | 		default: |
| 216 | 				return GLOB_NOMATCH; |
| 217 | 		} |
| 218 | 		home = pw.pw_dir; |
| 219 | 	} |
| 220 | 	while (i < PATH_MAX - 2 && *home) |
| 221 | 		buf[i++] = *home++; |
| 222 | 	if (*home) |
| 223 | 		return GLOB_NOMATCH; |
| 224 | 	if ((buf[i] = delim)) |
| 225 | 		buf[++i] = 0; |
| 226 | 	*pos = i; |
| 227 | 	return 0; |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g) |
| 232 | { |
| 233 | 	struct match head = { .next = NULL }, *tail = &head; |
| 234 | 	size_t cnt, i; |
| 235 | 	size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; |
| 236 | 	int error = 0; |
| 237 | 	char buf[PATH_MAX]; |
| 238 | 	 |
| 239 | 	if (!errfunc) errfunc = ignore_err; |
| 240 | |
| 241 | 	if (!(flags & GLOB_APPEND)) { |
| 242 | 		g->gl_offs = offs; |
| 243 | 		g->gl_pathc = 0; |
| 244 | 		g->gl_pathv = NULL; |
| 245 | 	} |
| 246 | |
| 247 | 	if (*pat) { |
| 248 | 		char *p = strdup(pat); |
| 249 | 		if (!p) return GLOB_NOSPACE; |
| 250 | 		buf[0] = 0; |
| 251 | 		size_t pos = 0; |
| 252 | 		char *s = p; |
| 253 | #ifdef __wasilibc_unmodified_upstream // WASI has no usernames |
| 254 | 		if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~') |
| 255 | 			error = expand_tilde(&s, buf, &pos); |
| 256 | #endif |
| 257 | 		if (!error) |
| 258 | 			error = do_glob(buf, pos, 0, s, flags, errfunc, &tail); |
| 259 | 		free(p); |
| 260 | 	} |
| 261 | |
| 262 | 	if (error == GLOB_NOSPACE) { |
| 263 | 		freelist(&head); |
| 264 | 		return error; |
| 265 | 	} |
| 266 | 	 |
| 267 | 	for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++); |
| 268 | 	if (!cnt) { |
| 269 | 		if (flags & GLOB_NOCHECK) { |
| 270 | 			tail = &head; |
| 271 | 			if (append(&tail, pat, strlen(pat), 0)) |
| 272 | 				return GLOB_NOSPACE; |
| 273 | 			cnt++; |
| 274 | 		} else if (!error) |
| 275 | 			return GLOB_NOMATCH; |
| 276 | 	} |
| 277 | |
| 278 | 	if (flags & GLOB_APPEND) { |
| 279 | 		char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); |
| 280 | 		if (!pathv) { |
| 281 | 			freelist(&head); |
| 282 | 			return GLOB_NOSPACE; |
| 283 | 		} |
| 284 | 		g->gl_pathv = pathv; |
| 285 | 		offs += g->gl_pathc; |
| 286 | 	} else { |
| 287 | 		g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); |
| 288 | 		if (!g->gl_pathv) { |
| 289 | 			freelist(&head); |
| 290 | 			return GLOB_NOSPACE; |
| 291 | 		} |
| 292 | 		for (i=0; i<offs; i++) |
| 293 | 			g->gl_pathv[i] = NULL; |
| 294 | 	} |
| 295 | 	for (i=0, tail=head.next; i<cnt; tail=tail->next, i++) |
| 296 | 		g->gl_pathv[offs + i] = tail->name; |
| 297 | 	g->gl_pathv[offs + i] = NULL; |
| 298 | 	g->gl_pathc += cnt; |
| 299 | |
| 300 | 	if (!(flags & GLOB_NOSORT)) |
| 301 | 		qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort); |
| 302 | 	 |
| 303 | 	return error; |
| 304 | } |
| 305 | |
| 306 | void globfree(glob_t *g) |
| 307 | { |
| 308 | 	size_t i; |
| 309 | 	for (i=0; i<g->gl_pathc; i++) |
| 310 | 		free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); |
| 311 | 	free(g->gl_pathv); |
| 312 | 	g->gl_pathc = 0; |
| 313 | 	g->gl_pathv = NULL; |
| 314 | } |
| 315 | |
| 316 | weak_alias(glob, glob64); |
| 317 | weak_alias(globfree, globfree64); |