Commit | Line | Data |
---|---|---|
7a6dbc2f | 1 | /* Copyright (C) 1991-2018 Free Software Foundation, Inc. |
6ec2e0f5 SDJ |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 3 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public | |
15 | License along with the GNU C Library; if not, see | |
7a6dbc2f | 16 | <https://www.gnu.org/licenses/>. */ |
6ec2e0f5 SDJ |
17 | |
18 | #ifndef _LIBC | |
7a6dbc2f | 19 | |
6ec2e0f5 | 20 | /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc |
7a6dbc2f | 21 | optimizes away the pattern == NULL test below. */ |
6ec2e0f5 | 22 | # define _GL_ARG_NONNULL(params) |
7a6dbc2f | 23 | |
6ec2e0f5 | 24 | # include <config.h> |
7a6dbc2f | 25 | |
6ec2e0f5 SDJ |
26 | #endif |
27 | ||
28 | #include <glob.h> | |
29 | ||
30 | #include <errno.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <stdbool.h> | |
34 | #include <stddef.h> | |
35 | #include <stdint.h> | |
6ec2e0f5 | 36 | #include <assert.h> |
6ec2e0f5 | 37 | #include <unistd.h> |
6ec2e0f5 | 38 | |
7a6dbc2f | 39 | #if defined _WIN32 && ! defined __CYGWIN__ |
6ec2e0f5 SDJ |
40 | # define WINDOWS32 |
41 | #endif | |
42 | ||
43 | #ifndef WINDOWS32 | |
44 | # include <pwd.h> | |
45 | #endif | |
46 | ||
47 | #include <errno.h> | |
6ec2e0f5 SDJ |
48 | #include <dirent.h> |
49 | #include <stdlib.h> | |
50 | #include <string.h> | |
51 | #include <alloca.h> | |
52 | ||
53 | #ifdef _LIBC | |
54 | # undef strdup | |
55 | # define strdup(str) __strdup (str) | |
56 | # define sysconf(id) __sysconf (id) | |
57 | # define closedir(dir) __closedir (dir) | |
58 | # define opendir(name) __opendir (name) | |
59 | # define readdir(str) __readdir64 (str) | |
60 | # define getpwnam_r(name, bufp, buf, len, res) \ | |
7a6dbc2f SDJ |
61 | __getpwnam_r (name, bufp, buf, len, res) |
62 | # ifndef __lstat64 | |
63 | # define __lstat64(fname, buf) __lxstat64 (_STAT_VER, fname, buf) | |
64 | # endif | |
6ec2e0f5 SDJ |
65 | # ifndef __stat64 |
66 | # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf) | |
67 | # endif | |
68 | # define struct_stat64 struct stat64 | |
7a6dbc2f | 69 | # define FLEXIBLE_ARRAY_MEMBER |
6ec2e0f5 SDJ |
70 | #else /* !_LIBC */ |
71 | # define __getlogin_r(buf, len) getlogin_r (buf, len) | |
7a6dbc2f | 72 | # define __lstat64(fname, buf) lstat (fname, buf) |
6ec2e0f5 SDJ |
73 | # define __stat64(fname, buf) stat (fname, buf) |
74 | # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag) | |
75 | # define struct_stat64 struct stat | |
76 | # ifndef __MVS__ | |
77 | # define __alloca alloca | |
78 | # endif | |
79 | # define __readdir readdir | |
6ec2e0f5 SDJ |
80 | # define COMPILE_GLOB64 |
81 | #endif /* _LIBC */ | |
82 | ||
83 | #include <fnmatch.h> | |
84 | ||
7a6dbc2f SDJ |
85 | #include <flexmember.h> |
86 | #include <glob_internal.h> | |
87 | #include <scratch_buffer.h> | |
6ec2e0f5 SDJ |
88 | \f |
89 | static const char *next_brace_sub (const char *begin, int flags) __THROWNL; | |
90 | ||
7a6dbc2f SDJ |
91 | /* The type of ((struct dirent *) 0)->d_type is 'unsigned char' on most |
92 | platforms, but 'unsigned int' in the mingw from mingw.org. */ | |
93 | typedef uint_fast32_t dirent_type; | |
94 | ||
95 | #if !defined _LIBC && !defined HAVE_STRUCT_DIRENT_D_TYPE | |
96 | /* Any distinct values will do here. | |
97 | Undef any existing macros out of the way. */ | |
98 | # undef DT_UNKNOWN | |
99 | # undef DT_DIR | |
100 | # undef DT_LNK | |
101 | # define DT_UNKNOWN 0 | |
102 | # define DT_DIR 1 | |
103 | # define DT_LNK 2 | |
104 | #endif | |
105 | ||
6ec2e0f5 SDJ |
106 | /* A representation of a directory entry which does not depend on the |
107 | layout of struct dirent, or the size of ino_t. */ | |
108 | struct readdir_result | |
109 | { | |
110 | const char *name; | |
7a6dbc2f SDJ |
111 | #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE |
112 | dirent_type type; | |
113 | #endif | |
6ec2e0f5 SDJ |
114 | }; |
115 | ||
7a6dbc2f SDJ |
116 | /* Initialize and return type member of struct readdir_result. */ |
117 | static dirent_type | |
118 | readdir_result_type (struct readdir_result d) | |
6ec2e0f5 | 119 | { |
7a6dbc2f SDJ |
120 | #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE |
121 | # define D_TYPE_TO_RESULT(source) (source)->d_type, | |
122 | return d.type; | |
123 | #else | |
124 | # define D_TYPE_TO_RESULT(source) | |
125 | return DT_UNKNOWN; | |
126 | #endif | |
6ec2e0f5 SDJ |
127 | } |
128 | ||
6ec2e0f5 SDJ |
129 | /* Construct an initializer for a struct readdir_result object from a |
130 | struct dirent *. No copy of the name is made. */ | |
131 | #define READDIR_RESULT_INITIALIZER(source) \ | |
132 | { \ | |
133 | source->d_name, \ | |
134 | D_TYPE_TO_RESULT (source) \ | |
6ec2e0f5 SDJ |
135 | } |
136 | ||
6ec2e0f5 SDJ |
137 | /* Call gl_readdir on STREAM. This macro can be overridden to reduce |
138 | type safety if an old interface version needs to be supported. */ | |
139 | #ifndef GL_READDIR | |
140 | # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream)) | |
141 | #endif | |
142 | ||
143 | /* Extract name and type from directory entry. No copy of the name is | |
144 | made. If SOURCE is NULL, result name is NULL. Keep in sync with | |
145 | convert_dirent64 below. */ | |
146 | static struct readdir_result | |
147 | convert_dirent (const struct dirent *source) | |
148 | { | |
149 | if (source == NULL) | |
150 | { | |
151 | struct readdir_result result = { NULL, }; | |
152 | return result; | |
153 | } | |
154 | struct readdir_result result = READDIR_RESULT_INITIALIZER (source); | |
155 | return result; | |
156 | } | |
157 | ||
158 | #ifndef COMPILE_GLOB64 | |
159 | /* Like convert_dirent, but works on struct dirent64 instead. Keep in | |
160 | sync with convert_dirent above. */ | |
161 | static struct readdir_result | |
162 | convert_dirent64 (const struct dirent64 *source) | |
163 | { | |
164 | if (source == NULL) | |
165 | { | |
166 | struct readdir_result result = { NULL, }; | |
167 | return result; | |
168 | } | |
169 | struct readdir_result result = READDIR_RESULT_INITIALIZER (source); | |
170 | return result; | |
171 | } | |
172 | #endif | |
173 | ||
6ec2e0f5 SDJ |
174 | #ifndef _LIBC |
175 | /* The results of opendir() in this file are not used with dirfd and fchdir, | |
176 | and we do not leak fds to any single-threaded code that could use stdio, | |
177 | therefore save some unnecessary recursion in fchdir.c and opendir_safer.c. | |
178 | FIXME - if the kernel ever adds support for multi-thread safety for | |
179 | avoiding standard fds, then we should use opendir_safer. */ | |
180 | # ifdef GNULIB_defined_opendir | |
181 | # undef opendir | |
182 | # endif | |
183 | # ifdef GNULIB_defined_closedir | |
184 | # undef closedir | |
185 | # endif | |
186 | ||
187 | /* Just use malloc. */ | |
188 | # define __libc_use_alloca(n) false | |
189 | # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0) | |
190 | # define extend_alloca_account(buf, len, newlen, avar) \ | |
191 | ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0) | |
192 | #endif | |
193 | ||
6ec2e0f5 SDJ |
194 | /* Set *R = A + B. Return true if the answer is mathematically |
195 | incorrect due to overflow; in this case, *R is the low order | |
7a6dbc2f | 196 | bits of the correct answer. */ |
6ec2e0f5 SDJ |
197 | |
198 | static bool | |
199 | size_add_wrapv (size_t a, size_t b, size_t *r) | |
200 | { | |
7a6dbc2f | 201 | #if 5 <= __GNUC__ && !defined __ICC |
6ec2e0f5 SDJ |
202 | return __builtin_add_overflow (a, b, r); |
203 | #else | |
204 | *r = a + b; | |
205 | return *r < a; | |
206 | #endif | |
207 | } | |
208 | ||
209 | static bool | |
210 | glob_use_alloca (size_t alloca_used, size_t len) | |
211 | { | |
212 | size_t size; | |
213 | return (!size_add_wrapv (alloca_used, len, &size) | |
214 | && __libc_use_alloca (size)); | |
215 | } | |
216 | ||
217 | static int glob_in_dir (const char *pattern, const char *directory, | |
218 | int flags, int (*errfunc) (const char *, int), | |
219 | glob_t *pglob, size_t alloca_used); | |
6ec2e0f5 SDJ |
220 | static int prefix_array (const char *prefix, char **array, size_t n) __THROWNL; |
221 | static int collated_compare (const void *, const void *) __THROWNL; | |
222 | ||
223 | ||
7a6dbc2f SDJ |
224 | /* Return true if FILENAME is a directory or a symbolic link to a directory. |
225 | Use FLAGS and PGLOB to resolve the filename. */ | |
226 | static bool | |
227 | is_dir (char const *filename, int flags, glob_t const *pglob) | |
228 | { | |
229 | struct stat st; | |
230 | struct_stat64 st64; | |
231 | return (__glibc_unlikely (flags & GLOB_ALTDIRFUNC) | |
232 | ? pglob->gl_stat (filename, &st) == 0 && S_ISDIR (st.st_mode) | |
233 | : __stat64 (filename, &st64) == 0 && S_ISDIR (st64.st_mode)); | |
234 | } | |
235 | ||
6ec2e0f5 SDJ |
236 | /* Find the end of the sub-pattern in a brace expression. */ |
237 | static const char * | |
238 | next_brace_sub (const char *cp, int flags) | |
239 | { | |
240 | size_t depth = 0; | |
241 | while (*cp != '\0') | |
242 | if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\') | |
243 | { | |
244 | if (*++cp == '\0') | |
245 | break; | |
246 | ++cp; | |
247 | } | |
248 | else | |
249 | { | |
250 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) | |
251 | break; | |
252 | ||
253 | if (*cp++ == '{') | |
254 | depth++; | |
255 | } | |
256 | ||
257 | return *cp != '\0' ? cp : NULL; | |
258 | } | |
259 | ||
6ec2e0f5 SDJ |
260 | |
261 | /* Do glob searching for PATTERN, placing results in PGLOB. | |
262 | The bits defined above may be set in FLAGS. | |
263 | If a directory cannot be opened or read and ERRFUNC is not nil, | |
264 | it is called with the pathname that caused the error, and the | |
265 | 'errno' value from the failing call; if it returns non-zero | |
266 | 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored. | |
267 | If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned. | |
268 | Otherwise, 'glob' returns zero. */ | |
269 | int | |
270 | #ifdef GLOB_ATTRIBUTE | |
271 | GLOB_ATTRIBUTE | |
272 | #endif | |
273 | glob (const char *pattern, int flags, int (*errfunc) (const char *, int), | |
274 | glob_t *pglob) | |
275 | { | |
276 | const char *filename; | |
277 | char *dirname = NULL; | |
278 | size_t dirlen; | |
279 | int status; | |
280 | size_t oldcount; | |
281 | int meta; | |
282 | int dirname_modified; | |
283 | int malloc_dirname = 0; | |
284 | glob_t dirs; | |
285 | int retval = 0; | |
286 | size_t alloca_used = 0; | |
287 | ||
288 | if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) | |
289 | { | |
290 | __set_errno (EINVAL); | |
291 | return -1; | |
292 | } | |
293 | ||
294 | /* POSIX requires all slashes to be matched. This means that with | |
295 | a trailing slash we must match only directories. */ | |
296 | if (pattern[0] && pattern[strlen (pattern) - 1] == '/') | |
297 | flags |= GLOB_ONLYDIR; | |
298 | ||
299 | if (!(flags & GLOB_DOOFFS)) | |
300 | /* Have to do this so 'globfree' knows where to start freeing. It | |
301 | also makes all the code that uses gl_offs simpler. */ | |
302 | pglob->gl_offs = 0; | |
303 | ||
7a6dbc2f SDJ |
304 | if (!(flags & GLOB_APPEND)) |
305 | { | |
306 | pglob->gl_pathc = 0; | |
307 | if (!(flags & GLOB_DOOFFS)) | |
308 | pglob->gl_pathv = NULL; | |
309 | else | |
310 | { | |
311 | size_t i; | |
312 | ||
313 | if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *)) | |
314 | return GLOB_NOSPACE; | |
315 | ||
316 | pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1) | |
317 | * sizeof (char *)); | |
318 | if (pglob->gl_pathv == NULL) | |
319 | return GLOB_NOSPACE; | |
320 | ||
321 | for (i = 0; i <= pglob->gl_offs; ++i) | |
322 | pglob->gl_pathv[i] = NULL; | |
323 | } | |
324 | } | |
325 | ||
6ec2e0f5 SDJ |
326 | if (flags & GLOB_BRACE) |
327 | { | |
328 | const char *begin; | |
329 | ||
330 | if (flags & GLOB_NOESCAPE) | |
331 | begin = strchr (pattern, '{'); | |
332 | else | |
333 | { | |
334 | begin = pattern; | |
335 | while (1) | |
336 | { | |
337 | if (*begin == '\0') | |
338 | { | |
339 | begin = NULL; | |
340 | break; | |
341 | } | |
342 | ||
343 | if (*begin == '\\' && begin[1] != '\0') | |
344 | ++begin; | |
345 | else if (*begin == '{') | |
346 | break; | |
347 | ||
348 | ++begin; | |
349 | } | |
350 | } | |
351 | ||
352 | if (begin != NULL) | |
353 | { | |
354 | /* Allocate working buffer large enough for our work. Note that | |
7a6dbc2f | 355 | we have at least an opening and closing brace. */ |
6ec2e0f5 SDJ |
356 | size_t firstc; |
357 | char *alt_start; | |
358 | const char *p; | |
359 | const char *next; | |
360 | const char *rest; | |
361 | size_t rest_len; | |
362 | char *onealt; | |
363 | size_t pattern_len = strlen (pattern) - 1; | |
364 | int alloca_onealt = glob_use_alloca (alloca_used, pattern_len); | |
365 | if (alloca_onealt) | |
366 | onealt = alloca_account (pattern_len, alloca_used); | |
367 | else | |
368 | { | |
369 | onealt = malloc (pattern_len); | |
370 | if (onealt == NULL) | |
7a6dbc2f | 371 | return GLOB_NOSPACE; |
6ec2e0f5 SDJ |
372 | } |
373 | ||
374 | /* We know the prefix for all sub-patterns. */ | |
375 | alt_start = mempcpy (onealt, pattern, begin - pattern); | |
376 | ||
377 | /* Find the first sub-pattern and at the same time find the | |
378 | rest after the closing brace. */ | |
379 | next = next_brace_sub (begin + 1, flags); | |
380 | if (next == NULL) | |
381 | { | |
382 | /* It is an invalid expression. */ | |
383 | illegal_brace: | |
384 | if (__glibc_unlikely (!alloca_onealt)) | |
385 | free (onealt); | |
7a6dbc2f SDJ |
386 | flags &= ~GLOB_BRACE; |
387 | goto no_brace; | |
6ec2e0f5 SDJ |
388 | } |
389 | ||
390 | /* Now find the end of the whole brace expression. */ | |
391 | rest = next; | |
392 | while (*rest != '}') | |
393 | { | |
394 | rest = next_brace_sub (rest + 1, flags); | |
395 | if (rest == NULL) | |
396 | /* It is an illegal expression. */ | |
397 | goto illegal_brace; | |
398 | } | |
399 | /* Please note that we now can be sure the brace expression | |
400 | is well-formed. */ | |
401 | rest_len = strlen (++rest) + 1; | |
402 | ||
403 | /* We have a brace expression. BEGIN points to the opening {, | |
404 | NEXT points past the terminator of the first element, and END | |
405 | points past the final }. We will accumulate result names from | |
406 | recursive runs for each brace alternative in the buffer using | |
407 | GLOB_APPEND. */ | |
6ec2e0f5 SDJ |
408 | firstc = pglob->gl_pathc; |
409 | ||
410 | p = begin + 1; | |
411 | while (1) | |
412 | { | |
413 | int result; | |
414 | ||
415 | /* Construct the new glob expression. */ | |
416 | mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len); | |
417 | ||
418 | result = glob (onealt, | |
419 | ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC)) | |
420 | | GLOB_APPEND), errfunc, pglob); | |
421 | ||
422 | /* If we got an error, return it. */ | |
423 | if (result && result != GLOB_NOMATCH) | |
424 | { | |
425 | if (__glibc_unlikely (!alloca_onealt)) | |
426 | free (onealt); | |
427 | if (!(flags & GLOB_APPEND)) | |
428 | { | |
429 | globfree (pglob); | |
430 | pglob->gl_pathc = 0; | |
431 | } | |
432 | return result; | |
433 | } | |
434 | ||
435 | if (*next == '}') | |
436 | /* We saw the last entry. */ | |
437 | break; | |
438 | ||
439 | p = next + 1; | |
440 | next = next_brace_sub (p, flags); | |
441 | assert (next != NULL); | |
442 | } | |
443 | ||
444 | if (__glibc_unlikely (!alloca_onealt)) | |
445 | free (onealt); | |
446 | ||
447 | if (pglob->gl_pathc != firstc) | |
448 | /* We found some entries. */ | |
449 | return 0; | |
450 | else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) | |
451 | return GLOB_NOMATCH; | |
452 | } | |
453 | } | |
454 | ||
7a6dbc2f | 455 | no_brace: |
6ec2e0f5 SDJ |
456 | oldcount = pglob->gl_pathc + pglob->gl_offs; |
457 | ||
458 | /* Find the filename. */ | |
459 | filename = strrchr (pattern, '/'); | |
7a6dbc2f | 460 | |
6ec2e0f5 SDJ |
461 | #if defined __MSDOS__ || defined WINDOWS32 |
462 | /* The case of "d:pattern". Since ':' is not allowed in | |
463 | file names, we can safely assume that wherever it | |
464 | happens in pattern, it signals the filename part. This | |
465 | is so we could some day support patterns like "[a-z]:foo". */ | |
466 | if (filename == NULL) | |
467 | filename = strchr (pattern, ':'); | |
468 | #endif /* __MSDOS__ || WINDOWS32 */ | |
7a6dbc2f | 469 | |
6ec2e0f5 SDJ |
470 | dirname_modified = 0; |
471 | if (filename == NULL) | |
472 | { | |
473 | /* This can mean two things: a simple name or "~name". The latter | |
474 | case is nothing but a notation for a directory. */ | |
475 | if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~') | |
476 | { | |
477 | dirname = (char *) pattern; | |
478 | dirlen = strlen (pattern); | |
479 | ||
480 | /* Set FILENAME to NULL as a special flag. This is ugly but | |
481 | other solutions would require much more code. We test for | |
482 | this special case below. */ | |
483 | filename = NULL; | |
484 | } | |
485 | else | |
486 | { | |
487 | if (__glibc_unlikely (pattern[0] == '\0')) | |
488 | { | |
489 | dirs.gl_pathv = NULL; | |
490 | goto no_matches; | |
491 | } | |
492 | ||
493 | filename = pattern; | |
6ec2e0f5 | 494 | dirname = (char *) "."; |
6ec2e0f5 SDJ |
495 | dirlen = 0; |
496 | } | |
497 | } | |
498 | else if (filename == pattern | |
499 | || (filename == pattern + 1 && pattern[0] == '\\' | |
500 | && (flags & GLOB_NOESCAPE) == 0)) | |
501 | { | |
502 | /* "/pattern" or "\\/pattern". */ | |
503 | dirname = (char *) "/"; | |
504 | dirlen = 1; | |
505 | ++filename; | |
506 | } | |
507 | else | |
508 | { | |
509 | char *newp; | |
510 | dirlen = filename - pattern; | |
7a6dbc2f | 511 | |
6ec2e0f5 SDJ |
512 | #if defined __MSDOS__ || defined WINDOWS32 |
513 | if (*filename == ':' | |
514 | || (filename > pattern + 1 && filename[-1] == ':')) | |
515 | { | |
516 | char *drive_spec; | |
517 | ||
518 | ++dirlen; | |
519 | drive_spec = __alloca (dirlen + 1); | |
520 | *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0'; | |
521 | /* For now, disallow wildcards in the drive spec, to | |
522 | prevent infinite recursion in glob. */ | |
523 | if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE))) | |
524 | return GLOB_NOMATCH; | |
525 | /* If this is "d:pattern", we need to copy ':' to DIRNAME | |
526 | as well. If it's "d:/pattern", don't remove the slash | |
527 | from "d:/", since "d:" and "d:/" are not the same.*/ | |
528 | } | |
529 | #endif | |
7a6dbc2f | 530 | |
6ec2e0f5 SDJ |
531 | if (glob_use_alloca (alloca_used, dirlen + 1)) |
532 | newp = alloca_account (dirlen + 1, alloca_used); | |
533 | else | |
534 | { | |
535 | newp = malloc (dirlen + 1); | |
536 | if (newp == NULL) | |
537 | return GLOB_NOSPACE; | |
538 | malloc_dirname = 1; | |
539 | } | |
540 | *((char *) mempcpy (newp, pattern, dirlen)) = '\0'; | |
541 | dirname = newp; | |
542 | ++filename; | |
543 | ||
6ec2e0f5 | 544 | #if defined __MSDOS__ || defined WINDOWS32 |
7a6dbc2f SDJ |
545 | bool drive_root = (dirlen > 1 |
546 | && (dirname[dirlen - 1] == ':' | |
547 | || (dirlen > 2 && dirname[dirlen - 2] == ':' | |
548 | && dirname[dirlen - 1] == '/'))); | |
549 | #else | |
550 | bool drive_root = false; | |
6ec2e0f5 | 551 | #endif |
7a6dbc2f SDJ |
552 | |
553 | if (filename[0] == '\0' && dirlen > 1 && !drive_root) | |
6ec2e0f5 SDJ |
554 | /* "pattern/". Expand "pattern", appending slashes. */ |
555 | { | |
556 | int orig_flags = flags; | |
6ec2e0f5 SDJ |
557 | if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\') |
558 | { | |
559 | /* "pattern\\/". Remove the final backslash if it hasn't | |
560 | been quoted. */ | |
561 | char *p = (char *) &dirname[dirlen - 1]; | |
562 | ||
563 | while (p > dirname && p[-1] == '\\') --p; | |
564 | if ((&dirname[dirlen] - p) & 1) | |
565 | { | |
566 | *(char *) &dirname[--dirlen] = '\0'; | |
567 | flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); | |
568 | } | |
569 | } | |
7a6dbc2f | 570 | int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); |
6ec2e0f5 SDJ |
571 | if (val == 0) |
572 | pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK) | |
573 | | (flags & GLOB_MARK)); | |
574 | else if (val == GLOB_NOMATCH && flags != orig_flags) | |
575 | { | |
576 | /* Make sure globfree (&dirs); is a nop. */ | |
577 | dirs.gl_pathv = NULL; | |
578 | flags = orig_flags; | |
579 | oldcount = pglob->gl_pathc + pglob->gl_offs; | |
580 | goto no_matches; | |
581 | } | |
582 | retval = val; | |
583 | goto out; | |
584 | } | |
585 | } | |
586 | ||
587 | if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~') | |
588 | { | |
589 | if (dirname[1] == '\0' || dirname[1] == '/' | |
590 | || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\' | |
591 | && (dirname[2] == '\0' || dirname[2] == '/'))) | |
592 | { | |
593 | /* Look up home directory. */ | |
594 | char *home_dir = getenv ("HOME"); | |
595 | int malloc_home_dir = 0; | |
6ec2e0f5 SDJ |
596 | if (home_dir == NULL || home_dir[0] == '\0') |
597 | { | |
7a6dbc2f SDJ |
598 | #ifdef WINDOWS32 |
599 | /* Windows NT defines HOMEDRIVE and HOMEPATH. But give | |
600 | preference to HOME, because the user can change HOME. */ | |
6ec2e0f5 SDJ |
601 | const char *home_drive = getenv ("HOMEDRIVE"); |
602 | const char *home_path = getenv ("HOMEPATH"); | |
603 | ||
604 | if (home_drive != NULL && home_path != NULL) | |
605 | { | |
606 | size_t home_drive_len = strlen (home_drive); | |
607 | size_t home_path_len = strlen (home_path); | |
608 | char *mem = alloca (home_drive_len + home_path_len + 1); | |
609 | ||
610 | memcpy (mem, home_drive, home_drive_len); | |
611 | memcpy (mem + home_drive_len, home_path, home_path_len + 1); | |
612 | home_dir = mem; | |
613 | } | |
614 | else | |
615 | home_dir = "c:/users/default"; /* poor default */ | |
7a6dbc2f SDJ |
616 | #else |
617 | int err; | |
618 | struct passwd *p; | |
619 | struct passwd pwbuf; | |
620 | struct scratch_buffer s; | |
621 | scratch_buffer_init (&s); | |
622 | while (true) | |
6ec2e0f5 | 623 | { |
7a6dbc2f SDJ |
624 | p = NULL; |
625 | err = __getlogin_r (s.data, s.length); | |
626 | if (err == 0) | |
627 | { | |
628 | # if defined HAVE_GETPWNAM_R || defined _LIBC | |
629 | size_t ssize = strlen (s.data) + 1; | |
630 | char *sdata = s.data; | |
631 | err = getpwnam_r (sdata, &pwbuf, sdata + ssize, | |
632 | s.length - ssize, &p); | |
633 | # else | |
634 | p = getpwnam (s.data); | |
635 | if (p == NULL) | |
636 | err = errno; | |
637 | # endif | |
638 | } | |
639 | if (err != ERANGE) | |
640 | break; | |
641 | if (!scratch_buffer_grow (&s)) | |
6ec2e0f5 SDJ |
642 | { |
643 | retval = GLOB_NOSPACE; | |
644 | goto out; | |
645 | } | |
6ec2e0f5 | 646 | } |
7a6dbc2f | 647 | if (err == 0) |
6ec2e0f5 | 648 | { |
7a6dbc2f SDJ |
649 | home_dir = strdup (p->pw_dir); |
650 | malloc_home_dir = 1; | |
651 | } | |
652 | scratch_buffer_free (&s); | |
653 | if (err == 0 && home_dir == NULL) | |
654 | { | |
655 | retval = GLOB_NOSPACE; | |
656 | goto out; | |
6ec2e0f5 | 657 | } |
7a6dbc2f | 658 | #endif /* WINDOWS32 */ |
6ec2e0f5 SDJ |
659 | } |
660 | if (home_dir == NULL || home_dir[0] == '\0') | |
661 | { | |
7a6dbc2f SDJ |
662 | if (__glibc_unlikely (malloc_home_dir)) |
663 | free (home_dir); | |
6ec2e0f5 SDJ |
664 | if (flags & GLOB_TILDE_CHECK) |
665 | { | |
6ec2e0f5 SDJ |
666 | retval = GLOB_NOMATCH; |
667 | goto out; | |
668 | } | |
669 | else | |
7a6dbc2f SDJ |
670 | { |
671 | home_dir = (char *) "~"; /* No luck. */ | |
672 | malloc_home_dir = 0; | |
673 | } | |
6ec2e0f5 | 674 | } |
6ec2e0f5 SDJ |
675 | /* Now construct the full directory. */ |
676 | if (dirname[1] == '\0') | |
677 | { | |
678 | if (__glibc_unlikely (malloc_dirname)) | |
679 | free (dirname); | |
680 | ||
681 | dirname = home_dir; | |
682 | dirlen = strlen (dirname); | |
683 | malloc_dirname = malloc_home_dir; | |
684 | } | |
685 | else | |
686 | { | |
687 | char *newp; | |
688 | size_t home_len = strlen (home_dir); | |
689 | int use_alloca = glob_use_alloca (alloca_used, home_len + dirlen); | |
690 | if (use_alloca) | |
691 | newp = alloca_account (home_len + dirlen, alloca_used); | |
692 | else | |
693 | { | |
694 | newp = malloc (home_len + dirlen); | |
695 | if (newp == NULL) | |
696 | { | |
697 | if (__glibc_unlikely (malloc_home_dir)) | |
698 | free (home_dir); | |
699 | retval = GLOB_NOSPACE; | |
700 | goto out; | |
701 | } | |
702 | } | |
703 | ||
704 | mempcpy (mempcpy (newp, home_dir, home_len), | |
705 | &dirname[1], dirlen); | |
706 | ||
707 | if (__glibc_unlikely (malloc_dirname)) | |
708 | free (dirname); | |
709 | ||
710 | dirname = newp; | |
711 | dirlen += home_len - 1; | |
712 | malloc_dirname = !use_alloca; | |
7a6dbc2f SDJ |
713 | |
714 | if (__glibc_unlikely (malloc_home_dir)) | |
715 | free (home_dir); | |
6ec2e0f5 SDJ |
716 | } |
717 | dirname_modified = 1; | |
718 | } | |
6ec2e0f5 SDJ |
719 | else |
720 | { | |
7a6dbc2f | 721 | #ifndef WINDOWS32 |
6ec2e0f5 SDJ |
722 | char *end_name = strchr (dirname, '/'); |
723 | char *user_name; | |
724 | int malloc_user_name = 0; | |
725 | char *unescape = NULL; | |
726 | ||
727 | if (!(flags & GLOB_NOESCAPE)) | |
728 | { | |
729 | if (end_name == NULL) | |
730 | { | |
731 | unescape = strchr (dirname, '\\'); | |
732 | if (unescape) | |
733 | end_name = strchr (unescape, '\0'); | |
734 | } | |
735 | else | |
736 | unescape = memchr (dirname, '\\', end_name - dirname); | |
737 | } | |
738 | if (end_name == NULL) | |
739 | user_name = dirname + 1; | |
740 | else | |
741 | { | |
742 | char *newp; | |
743 | if (glob_use_alloca (alloca_used, end_name - dirname)) | |
744 | newp = alloca_account (end_name - dirname, alloca_used); | |
745 | else | |
746 | { | |
747 | newp = malloc (end_name - dirname); | |
748 | if (newp == NULL) | |
749 | { | |
750 | retval = GLOB_NOSPACE; | |
751 | goto out; | |
752 | } | |
753 | malloc_user_name = 1; | |
754 | } | |
755 | if (unescape != NULL) | |
756 | { | |
757 | char *p = mempcpy (newp, dirname + 1, | |
758 | unescape - dirname - 1); | |
759 | char *q = unescape; | |
7a6dbc2f | 760 | while (q != end_name) |
6ec2e0f5 SDJ |
761 | { |
762 | if (*q == '\\') | |
763 | { | |
7a6dbc2f | 764 | if (q + 1 == end_name) |
6ec2e0f5 SDJ |
765 | { |
766 | /* "~fo\\o\\" unescape to user_name "foo\\", | |
767 | but "~fo\\o\\/" unescape to user_name | |
768 | "foo". */ | |
769 | if (filename == NULL) | |
770 | *p++ = '\\'; | |
771 | break; | |
772 | } | |
773 | ++q; | |
774 | } | |
775 | *p++ = *q++; | |
776 | } | |
777 | *p = '\0'; | |
778 | } | |
779 | else | |
7a6dbc2f | 780 | *((char *) mempcpy (newp, dirname + 1, end_name - dirname - 1)) |
6ec2e0f5 SDJ |
781 | = '\0'; |
782 | user_name = newp; | |
783 | } | |
784 | ||
785 | /* Look up specific user's home directory. */ | |
786 | { | |
787 | struct passwd *p; | |
7a6dbc2f SDJ |
788 | struct scratch_buffer pwtmpbuf; |
789 | scratch_buffer_init (&pwtmpbuf); | |
790 | ||
6ec2e0f5 | 791 | # if defined HAVE_GETPWNAM_R || defined _LIBC |
6ec2e0f5 | 792 | struct passwd pwbuf; |
7a6dbc2f SDJ |
793 | |
794 | while (getpwnam_r (user_name, &pwbuf, | |
795 | pwtmpbuf.data, pwtmpbuf.length, &p) | |
796 | == ERANGE) | |
6ec2e0f5 | 797 | { |
7a6dbc2f | 798 | if (!scratch_buffer_grow (&pwtmpbuf)) |
6ec2e0f5 | 799 | { |
6ec2e0f5 SDJ |
800 | retval = GLOB_NOSPACE; |
801 | goto out; | |
802 | } | |
6ec2e0f5 SDJ |
803 | } |
804 | # else | |
805 | p = getpwnam (user_name); | |
806 | # endif | |
807 | ||
808 | if (__glibc_unlikely (malloc_user_name)) | |
809 | free (user_name); | |
810 | ||
811 | /* If we found a home directory use this. */ | |
812 | if (p != NULL) | |
813 | { | |
814 | size_t home_len = strlen (p->pw_dir); | |
815 | size_t rest_len = end_name == NULL ? 0 : strlen (end_name); | |
7a6dbc2f | 816 | char *d; |
6ec2e0f5 SDJ |
817 | |
818 | if (__glibc_unlikely (malloc_dirname)) | |
819 | free (dirname); | |
820 | malloc_dirname = 0; | |
821 | ||
822 | if (glob_use_alloca (alloca_used, home_len + rest_len + 1)) | |
823 | dirname = alloca_account (home_len + rest_len + 1, | |
824 | alloca_used); | |
825 | else | |
826 | { | |
827 | dirname = malloc (home_len + rest_len + 1); | |
828 | if (dirname == NULL) | |
829 | { | |
7a6dbc2f | 830 | scratch_buffer_free (&pwtmpbuf); |
6ec2e0f5 SDJ |
831 | retval = GLOB_NOSPACE; |
832 | goto out; | |
833 | } | |
834 | malloc_dirname = 1; | |
835 | } | |
7a6dbc2f SDJ |
836 | d = mempcpy (dirname, p->pw_dir, home_len); |
837 | if (end_name != NULL) | |
838 | d = mempcpy (d, end_name, rest_len); | |
839 | *d = '\0'; | |
6ec2e0f5 SDJ |
840 | |
841 | dirlen = home_len + rest_len; | |
842 | dirname_modified = 1; | |
6ec2e0f5 SDJ |
843 | } |
844 | else | |
845 | { | |
6ec2e0f5 | 846 | if (flags & GLOB_TILDE_CHECK) |
7a6dbc2f SDJ |
847 | { |
848 | /* We have to regard it as an error if we cannot find the | |
849 | home directory. */ | |
850 | retval = GLOB_NOMATCH; | |
851 | goto out; | |
852 | } | |
6ec2e0f5 | 853 | } |
7a6dbc2f | 854 | scratch_buffer_free (&pwtmpbuf); |
6ec2e0f5 | 855 | } |
7a6dbc2f | 856 | #endif /* !WINDOWS32 */ |
6ec2e0f5 | 857 | } |
6ec2e0f5 SDJ |
858 | } |
859 | ||
860 | /* Now test whether we looked for "~" or "~NAME". In this case we | |
861 | can give the answer now. */ | |
862 | if (filename == NULL) | |
863 | { | |
7a6dbc2f SDJ |
864 | size_t newcount = pglob->gl_pathc + pglob->gl_offs; |
865 | char **new_gl_pathv; | |
866 | ||
867 | if (newcount > SIZE_MAX / sizeof (char *) - 2) | |
6ec2e0f5 | 868 | { |
7a6dbc2f SDJ |
869 | nospace: |
870 | free (pglob->gl_pathv); | |
871 | pglob->gl_pathv = NULL; | |
872 | pglob->gl_pathc = 0; | |
873 | retval = GLOB_NOSPACE; | |
874 | goto out; | |
875 | } | |
6ec2e0f5 | 876 | |
7a6dbc2f SDJ |
877 | new_gl_pathv = realloc (pglob->gl_pathv, |
878 | (newcount + 2) * sizeof (char *)); | |
879 | if (new_gl_pathv == NULL) | |
880 | goto nospace; | |
881 | pglob->gl_pathv = new_gl_pathv; | |
6ec2e0f5 | 882 | |
7a6dbc2f SDJ |
883 | if (flags & GLOB_MARK && is_dir (dirname, flags, pglob)) |
884 | { | |
885 | char *p; | |
886 | pglob->gl_pathv[newcount] = malloc (dirlen + 2); | |
887 | if (pglob->gl_pathv[newcount] == NULL) | |
6ec2e0f5 | 888 | goto nospace; |
7a6dbc2f SDJ |
889 | p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen); |
890 | p[0] = '/'; | |
891 | p[1] = '\0'; | |
892 | if (__glibc_unlikely (malloc_dirname)) | |
893 | free (dirname); | |
894 | } | |
895 | else | |
896 | { | |
897 | if (__glibc_unlikely (malloc_dirname)) | |
898 | pglob->gl_pathv[newcount] = dirname; | |
6ec2e0f5 SDJ |
899 | else |
900 | { | |
901 | pglob->gl_pathv[newcount] = strdup (dirname); | |
902 | if (pglob->gl_pathv[newcount] == NULL) | |
903 | goto nospace; | |
904 | } | |
6ec2e0f5 | 905 | } |
7a6dbc2f SDJ |
906 | pglob->gl_pathv[++newcount] = NULL; |
907 | ++pglob->gl_pathc; | |
908 | pglob->gl_flags = flags; | |
6ec2e0f5 | 909 | |
7a6dbc2f | 910 | return 0; |
6ec2e0f5 SDJ |
911 | } |
912 | ||
913 | meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE)); | |
914 | /* meta is 1 if correct glob pattern containing metacharacters. | |
915 | If meta has bit (1 << 2) set, it means there was an unterminated | |
916 | [ which we handle the same, using fnmatch. Broken unterminated | |
917 | pattern bracket expressions ought to be rare enough that it is | |
918 | not worth special casing them, fnmatch will do the right thing. */ | |
7a6dbc2f | 919 | if (meta & (GLOBPAT_SPECIAL | GLOBPAT_BRACKET)) |
6ec2e0f5 SDJ |
920 | { |
921 | /* The directory name contains metacharacters, so we | |
922 | have to glob for the directory, and then glob for | |
923 | the pattern in each directory found. */ | |
924 | size_t i; | |
925 | ||
926 | if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\') | |
927 | { | |
928 | /* "foo\\/bar". Remove the final backslash from dirname | |
929 | if it has not been quoted. */ | |
930 | char *p = (char *) &dirname[dirlen - 1]; | |
931 | ||
932 | while (p > dirname && p[-1] == '\\') --p; | |
933 | if ((&dirname[dirlen] - p) & 1) | |
934 | *(char *) &dirname[--dirlen] = '\0'; | |
935 | } | |
936 | ||
937 | if (__glibc_unlikely ((flags & GLOB_ALTDIRFUNC) != 0)) | |
938 | { | |
939 | /* Use the alternative access functions also in the recursive | |
940 | call. */ | |
941 | dirs.gl_opendir = pglob->gl_opendir; | |
942 | dirs.gl_readdir = pglob->gl_readdir; | |
943 | dirs.gl_closedir = pglob->gl_closedir; | |
944 | dirs.gl_stat = pglob->gl_stat; | |
945 | dirs.gl_lstat = pglob->gl_lstat; | |
946 | } | |
947 | ||
948 | status = glob (dirname, | |
949 | ((flags & (GLOB_ERR | GLOB_NOESCAPE | |
950 | | GLOB_ALTDIRFUNC)) | |
951 | | GLOB_NOSORT | GLOB_ONLYDIR), | |
952 | errfunc, &dirs); | |
953 | if (status != 0) | |
954 | { | |
955 | if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH) | |
7a6dbc2f SDJ |
956 | { |
957 | retval = status; | |
958 | goto out; | |
959 | } | |
6ec2e0f5 SDJ |
960 | goto no_matches; |
961 | } | |
962 | ||
963 | /* We have successfully globbed the preceding directory name. | |
964 | For each name we found, call glob_in_dir on it and FILENAME, | |
965 | appending the results to PGLOB. */ | |
966 | for (i = 0; i < dirs.gl_pathc; ++i) | |
967 | { | |
968 | size_t old_pathc; | |
969 | ||
6ec2e0f5 SDJ |
970 | old_pathc = pglob->gl_pathc; |
971 | status = glob_in_dir (filename, dirs.gl_pathv[i], | |
972 | ((flags | GLOB_APPEND) | |
973 | & ~(GLOB_NOCHECK | GLOB_NOMAGIC)), | |
974 | errfunc, pglob, alloca_used); | |
975 | if (status == GLOB_NOMATCH) | |
976 | /* No matches in this directory. Try the next. */ | |
977 | continue; | |
978 | ||
979 | if (status != 0) | |
980 | { | |
981 | globfree (&dirs); | |
982 | globfree (pglob); | |
983 | pglob->gl_pathc = 0; | |
7a6dbc2f SDJ |
984 | retval = status; |
985 | goto out; | |
6ec2e0f5 SDJ |
986 | } |
987 | ||
988 | /* Stick the directory on the front of each name. */ | |
989 | if (prefix_array (dirs.gl_pathv[i], | |
990 | &pglob->gl_pathv[old_pathc + pglob->gl_offs], | |
991 | pglob->gl_pathc - old_pathc)) | |
992 | { | |
993 | globfree (&dirs); | |
994 | globfree (pglob); | |
995 | pglob->gl_pathc = 0; | |
7a6dbc2f SDJ |
996 | retval = GLOB_NOSPACE; |
997 | goto out; | |
6ec2e0f5 SDJ |
998 | } |
999 | } | |
1000 | ||
1001 | flags |= GLOB_MAGCHAR; | |
1002 | ||
1003 | /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls. | |
1004 | But if we have not found any matching entry and the GLOB_NOCHECK | |
1005 | flag was set we must return the input pattern itself. */ | |
1006 | if (pglob->gl_pathc + pglob->gl_offs == oldcount) | |
1007 | { | |
1008 | no_matches: | |
1009 | /* No matches. */ | |
1010 | if (flags & GLOB_NOCHECK) | |
1011 | { | |
1012 | size_t newcount = pglob->gl_pathc + pglob->gl_offs; | |
1013 | char **new_gl_pathv; | |
1014 | ||
1015 | if (newcount > SIZE_MAX / sizeof (char *) - 2) | |
1016 | { | |
1017 | nospace2: | |
1018 | globfree (&dirs); | |
7a6dbc2f SDJ |
1019 | retval = GLOB_NOSPACE; |
1020 | goto out; | |
6ec2e0f5 SDJ |
1021 | } |
1022 | ||
1023 | new_gl_pathv = realloc (pglob->gl_pathv, | |
1024 | (newcount + 2) * sizeof (char *)); | |
1025 | if (new_gl_pathv == NULL) | |
1026 | goto nospace2; | |
1027 | pglob->gl_pathv = new_gl_pathv; | |
1028 | ||
1029 | pglob->gl_pathv[newcount] = strdup (pattern); | |
1030 | if (pglob->gl_pathv[newcount] == NULL) | |
1031 | { | |
1032 | globfree (&dirs); | |
1033 | globfree (pglob); | |
1034 | pglob->gl_pathc = 0; | |
7a6dbc2f SDJ |
1035 | retval = GLOB_NOSPACE; |
1036 | goto out; | |
6ec2e0f5 SDJ |
1037 | } |
1038 | ||
1039 | ++pglob->gl_pathc; | |
1040 | ++newcount; | |
1041 | ||
1042 | pglob->gl_pathv[newcount] = NULL; | |
1043 | pglob->gl_flags = flags; | |
1044 | } | |
1045 | else | |
1046 | { | |
1047 | globfree (&dirs); | |
7a6dbc2f SDJ |
1048 | retval = GLOB_NOMATCH; |
1049 | goto out; | |
6ec2e0f5 SDJ |
1050 | } |
1051 | } | |
1052 | ||
1053 | globfree (&dirs); | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | size_t old_pathc = pglob->gl_pathc; | |
1058 | int orig_flags = flags; | |
1059 | ||
7a6dbc2f | 1060 | if (meta & GLOBPAT_BACKSLASH) |
6ec2e0f5 SDJ |
1061 | { |
1062 | char *p = strchr (dirname, '\\'), *q; | |
1063 | /* We need to unescape the dirname string. It is certainly | |
1064 | allocated by alloca, as otherwise filename would be NULL | |
1065 | or dirname wouldn't contain backslashes. */ | |
1066 | q = p; | |
1067 | do | |
1068 | { | |
1069 | if (*p == '\\') | |
1070 | { | |
1071 | *q = *++p; | |
1072 | --dirlen; | |
1073 | } | |
1074 | else | |
1075 | *q = *p; | |
1076 | ++q; | |
1077 | } | |
1078 | while (*p++ != '\0'); | |
1079 | dirname_modified = 1; | |
1080 | } | |
1081 | if (dirname_modified) | |
1082 | flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); | |
1083 | status = glob_in_dir (filename, dirname, flags, errfunc, pglob, | |
1084 | alloca_used); | |
1085 | if (status != 0) | |
1086 | { | |
1087 | if (status == GLOB_NOMATCH && flags != orig_flags | |
1088 | && pglob->gl_pathc + pglob->gl_offs == oldcount) | |
1089 | { | |
1090 | /* Make sure globfree (&dirs); is a nop. */ | |
1091 | dirs.gl_pathv = NULL; | |
1092 | flags = orig_flags; | |
1093 | goto no_matches; | |
1094 | } | |
7a6dbc2f SDJ |
1095 | retval = status; |
1096 | goto out; | |
6ec2e0f5 SDJ |
1097 | } |
1098 | ||
1099 | if (dirlen > 0) | |
1100 | { | |
1101 | /* Stick the directory on the front of each name. */ | |
1102 | if (prefix_array (dirname, | |
1103 | &pglob->gl_pathv[old_pathc + pglob->gl_offs], | |
1104 | pglob->gl_pathc - old_pathc)) | |
1105 | { | |
1106 | globfree (pglob); | |
1107 | pglob->gl_pathc = 0; | |
7a6dbc2f SDJ |
1108 | retval = GLOB_NOSPACE; |
1109 | goto out; | |
6ec2e0f5 SDJ |
1110 | } |
1111 | } | |
1112 | } | |
1113 | ||
1114 | if (flags & GLOB_MARK) | |
1115 | { | |
1116 | /* Append slashes to directory names. */ | |
1117 | size_t i; | |
6ec2e0f5 SDJ |
1118 | |
1119 | for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i) | |
7a6dbc2f | 1120 | if (is_dir (pglob->gl_pathv[i], flags, pglob)) |
6ec2e0f5 SDJ |
1121 | { |
1122 | size_t len = strlen (pglob->gl_pathv[i]) + 2; | |
1123 | char *new = realloc (pglob->gl_pathv[i], len); | |
1124 | if (new == NULL) | |
1125 | { | |
1126 | globfree (pglob); | |
1127 | pglob->gl_pathc = 0; | |
7a6dbc2f SDJ |
1128 | retval = GLOB_NOSPACE; |
1129 | goto out; | |
6ec2e0f5 SDJ |
1130 | } |
1131 | strcpy (&new[len - 2], "/"); | |
1132 | pglob->gl_pathv[i] = new; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | if (!(flags & GLOB_NOSORT)) | |
1137 | { | |
1138 | /* Sort the vector. */ | |
1139 | qsort (&pglob->gl_pathv[oldcount], | |
1140 | pglob->gl_pathc + pglob->gl_offs - oldcount, | |
1141 | sizeof (char *), collated_compare); | |
1142 | } | |
1143 | ||
1144 | out: | |
1145 | if (__glibc_unlikely (malloc_dirname)) | |
1146 | free (dirname); | |
1147 | ||
1148 | return retval; | |
1149 | } | |
1150 | #if defined _LIBC && !defined glob | |
1151 | libc_hidden_def (glob) | |
1152 | #endif | |
1153 | ||
1154 | ||
6ec2e0f5 SDJ |
1155 | /* Do a collated comparison of A and B. */ |
1156 | static int | |
1157 | collated_compare (const void *a, const void *b) | |
1158 | { | |
1159 | char *const *ps1 = a; char *s1 = *ps1; | |
1160 | char *const *ps2 = b; char *s2 = *ps2; | |
1161 | ||
1162 | if (s1 == s2) | |
1163 | return 0; | |
1164 | if (s1 == NULL) | |
1165 | return 1; | |
1166 | if (s2 == NULL) | |
1167 | return -1; | |
1168 | return strcoll (s1, s2); | |
1169 | } | |
1170 | ||
1171 | ||
1172 | /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's | |
1173 | elements in place. Return nonzero if out of memory, zero if successful. | |
1174 | A slash is inserted between DIRNAME and each elt of ARRAY, | |
1175 | unless DIRNAME is just "/". Each old element of ARRAY is freed. */ | |
1176 | static int | |
1177 | prefix_array (const char *dirname, char **array, size_t n) | |
1178 | { | |
1179 | size_t i; | |
1180 | size_t dirlen = strlen (dirname); | |
7a6dbc2f | 1181 | char dirsep_char = '/'; |
6ec2e0f5 SDJ |
1182 | |
1183 | if (dirlen == 1 && dirname[0] == '/') | |
1184 | /* DIRNAME is just "/", so normal prepending would get us "//foo". | |
1185 | We want "/foo" instead, so don't prepend any chars from DIRNAME. */ | |
1186 | dirlen = 0; | |
7a6dbc2f | 1187 | |
6ec2e0f5 | 1188 | #if defined __MSDOS__ || defined WINDOWS32 |
7a6dbc2f | 1189 | if (dirlen > 1) |
6ec2e0f5 SDJ |
1190 | { |
1191 | if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':') | |
1192 | /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */ | |
1193 | --dirlen; | |
1194 | else if (dirname[dirlen - 1] == ':') | |
1195 | { | |
1196 | /* DIRNAME is "d:". Use ':' instead of '/'. */ | |
1197 | --dirlen; | |
7a6dbc2f | 1198 | dirsep_char = ':'; |
6ec2e0f5 SDJ |
1199 | } |
1200 | } | |
1201 | #endif | |
1202 | ||
1203 | for (i = 0; i < n; ++i) | |
1204 | { | |
1205 | size_t eltlen = strlen (array[i]) + 1; | |
1206 | char *new = malloc (dirlen + 1 + eltlen); | |
1207 | if (new == NULL) | |
1208 | { | |
1209 | while (i > 0) | |
1210 | free (array[--i]); | |
1211 | return 1; | |
1212 | } | |
1213 | ||
1214 | { | |
1215 | char *endp = mempcpy (new, dirname, dirlen); | |
7a6dbc2f | 1216 | *endp++ = dirsep_char; |
6ec2e0f5 SDJ |
1217 | mempcpy (endp, array[i], eltlen); |
1218 | } | |
1219 | free (array[i]); | |
1220 | array[i] = new; | |
1221 | } | |
1222 | ||
1223 | return 0; | |
1224 | } | |
1225 | ||
6ec2e0f5 SDJ |
1226 | /* Like 'glob', but PATTERN is a final pathname component, |
1227 | and matches are searched for in DIRECTORY. | |
1228 | The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. | |
1229 | The GLOB_APPEND flag is assumed to be set (always appends). */ | |
1230 | static int | |
1231 | glob_in_dir (const char *pattern, const char *directory, int flags, | |
1232 | int (*errfunc) (const char *, int), | |
1233 | glob_t *pglob, size_t alloca_used) | |
1234 | { | |
1235 | size_t dirlen = strlen (directory); | |
1236 | void *stream = NULL; | |
7a6dbc2f SDJ |
1237 | # define GLOBNAMES_MEMBERS(nnames) \ |
1238 | struct globnames *next; size_t count; char *name[nnames]; | |
1239 | struct globnames { GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER) }; | |
1240 | struct { GLOBNAMES_MEMBERS (64) } init_names_buf; | |
1241 | struct globnames *init_names = (struct globnames *) &init_names_buf; | |
1242 | struct globnames *names = init_names; | |
1243 | struct globnames *names_alloca = init_names; | |
6ec2e0f5 SDJ |
1244 | size_t nfound = 0; |
1245 | size_t cur = 0; | |
1246 | int meta; | |
1247 | int save; | |
1248 | int result; | |
1249 | ||
7a6dbc2f | 1250 | alloca_used += sizeof init_names_buf; |
6ec2e0f5 | 1251 | |
7a6dbc2f SDJ |
1252 | init_names->next = NULL; |
1253 | init_names->count = ((sizeof init_names_buf | |
1254 | - offsetof (struct globnames, name)) | |
1255 | / sizeof init_names->name[0]); | |
6ec2e0f5 SDJ |
1256 | |
1257 | meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE)); | |
7a6dbc2f | 1258 | if (meta == GLOBPAT_NONE && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) |
6ec2e0f5 SDJ |
1259 | { |
1260 | /* We need not do any tests. The PATTERN contains no meta | |
1261 | characters and we must not return an error therefore the | |
1262 | result will always contain exactly one name. */ | |
1263 | flags |= GLOB_NOCHECK; | |
1264 | } | |
7a6dbc2f | 1265 | else if (meta == GLOBPAT_NONE) |
6ec2e0f5 | 1266 | { |
6ec2e0f5 SDJ |
1267 | union |
1268 | { | |
1269 | struct stat st; | |
1270 | struct_stat64 st64; | |
1271 | } ust; | |
1272 | size_t patlen = strlen (pattern); | |
1273 | size_t fullsize; | |
1274 | bool alloca_fullname | |
1275 | = (! size_add_wrapv (dirlen + 1, patlen + 1, &fullsize) | |
1276 | && glob_use_alloca (alloca_used, fullsize)); | |
1277 | char *fullname; | |
1278 | if (alloca_fullname) | |
1279 | fullname = alloca_account (fullsize, alloca_used); | |
1280 | else | |
1281 | { | |
1282 | fullname = malloc (fullsize); | |
1283 | if (fullname == NULL) | |
1284 | return GLOB_NOSPACE; | |
1285 | } | |
1286 | ||
1287 | mempcpy (mempcpy (mempcpy (fullname, directory, dirlen), | |
1288 | "/", 1), | |
1289 | pattern, patlen + 1); | |
7a6dbc2f SDJ |
1290 | if (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) |
1291 | ? (*pglob->gl_lstat) (fullname, &ust.st) | |
1292 | : __lstat64 (fullname, &ust.st64)) | |
1293 | == 0) | |
1294 | || errno == EOVERFLOW) | |
6ec2e0f5 SDJ |
1295 | /* We found this file to be existing. Now tell the rest |
1296 | of the function to copy this name into the result. */ | |
1297 | flags |= GLOB_NOCHECK; | |
1298 | ||
1299 | if (__glibc_unlikely (!alloca_fullname)) | |
1300 | free (fullname); | |
1301 | } | |
1302 | else | |
1303 | { | |
1304 | stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) | |
1305 | ? (*pglob->gl_opendir) (directory) | |
1306 | : opendir (directory)); | |
1307 | if (stream == NULL) | |
1308 | { | |
1309 | if (errno != ENOTDIR | |
1310 | && ((errfunc != NULL && (*errfunc) (directory, errno)) | |
1311 | || (flags & GLOB_ERR))) | |
1312 | return GLOB_ABORTED; | |
1313 | } | |
1314 | else | |
1315 | { | |
6ec2e0f5 | 1316 | int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
7a6dbc2f | 1317 | | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)); |
6ec2e0f5 SDJ |
1318 | flags |= GLOB_MAGCHAR; |
1319 | ||
1320 | while (1) | |
1321 | { | |
1322 | struct readdir_result d; | |
1323 | { | |
1324 | if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) | |
1325 | d = convert_dirent (GL_READDIR (pglob, stream)); | |
1326 | else | |
1327 | { | |
1328 | #ifdef COMPILE_GLOB64 | |
1329 | d = convert_dirent (__readdir (stream)); | |
1330 | #else | |
1331 | d = convert_dirent64 (__readdir64 (stream)); | |
1332 | #endif | |
1333 | } | |
1334 | } | |
1335 | if (d.name == NULL) | |
1336 | break; | |
6ec2e0f5 SDJ |
1337 | |
1338 | /* If we shall match only directories use the information | |
1339 | provided by the dirent call if possible. */ | |
7a6dbc2f SDJ |
1340 | if (flags & GLOB_ONLYDIR) |
1341 | switch (readdir_result_type (d)) | |
1342 | { | |
1343 | case DT_DIR: case DT_LNK: case DT_UNKNOWN: break; | |
1344 | default: continue; | |
1345 | } | |
6ec2e0f5 SDJ |
1346 | |
1347 | if (fnmatch (pattern, d.name, fnm_flags) == 0) | |
1348 | { | |
7a6dbc2f | 1349 | if (cur == names->count) |
6ec2e0f5 | 1350 | { |
7a6dbc2f SDJ |
1351 | struct globnames *newnames; |
1352 | size_t count = names->count * 2; | |
1353 | size_t nameoff = offsetof (struct globnames, name); | |
1354 | size_t size = FLEXSIZEOF (struct globnames, name, | |
1355 | count * sizeof (char *)); | |
1356 | if ((SIZE_MAX - nameoff) / 2 / sizeof (char *) | |
1357 | < names->count) | |
6ec2e0f5 | 1358 | goto memory_error; |
7a6dbc2f SDJ |
1359 | if (glob_use_alloca (alloca_used, size)) |
1360 | newnames = names_alloca | |
1361 | = alloca_account (size, alloca_used); | |
1362 | else if ((newnames = malloc (size)) | |
1363 | == NULL) | |
6ec2e0f5 | 1364 | goto memory_error; |
7a6dbc2f SDJ |
1365 | newnames->count = count; |
1366 | newnames->next = names; | |
1367 | names = newnames; | |
1368 | cur = 0; | |
6ec2e0f5 | 1369 | } |
7a6dbc2f SDJ |
1370 | names->name[cur] = strdup (d.name); |
1371 | if (names->name[cur] == NULL) | |
1372 | goto memory_error; | |
1373 | ++cur; | |
1374 | ++nfound; | |
1375 | if (SIZE_MAX - pglob->gl_offs <= nfound) | |
1376 | goto memory_error; | |
6ec2e0f5 SDJ |
1377 | } |
1378 | } | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | if (nfound == 0 && (flags & GLOB_NOCHECK)) | |
1383 | { | |
1384 | size_t len = strlen (pattern); | |
1385 | nfound = 1; | |
1386 | names->name[cur] = malloc (len + 1); | |
1387 | if (names->name[cur] == NULL) | |
1388 | goto memory_error; | |
1389 | *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0'; | |
1390 | } | |
1391 | ||
1392 | result = GLOB_NOMATCH; | |
1393 | if (nfound != 0) | |
1394 | { | |
1395 | char **new_gl_pathv; | |
1396 | result = 0; | |
1397 | ||
1398 | if (SIZE_MAX / sizeof (char *) - pglob->gl_pathc | |
1399 | < pglob->gl_offs + nfound + 1) | |
1400 | goto memory_error; | |
1401 | ||
1402 | new_gl_pathv | |
1403 | = realloc (pglob->gl_pathv, | |
1404 | (pglob->gl_pathc + pglob->gl_offs + nfound + 1) | |
7a6dbc2f | 1405 | * sizeof (char *)); |
6ec2e0f5 SDJ |
1406 | |
1407 | if (new_gl_pathv == NULL) | |
1408 | { | |
1409 | memory_error: | |
1410 | while (1) | |
1411 | { | |
1412 | struct globnames *old = names; | |
7a6dbc2f | 1413 | for (size_t i = 0; i < cur; ++i) |
6ec2e0f5 SDJ |
1414 | free (names->name[i]); |
1415 | names = names->next; | |
1416 | /* NB: we will not leak memory here if we exit without | |
1417 | freeing the current block assigned to OLD. At least | |
1418 | the very first block is always allocated on the stack | |
1419 | and this is the block assigned to OLD here. */ | |
1420 | if (names == NULL) | |
1421 | { | |
7a6dbc2f | 1422 | assert (old == init_names); |
6ec2e0f5 SDJ |
1423 | break; |
1424 | } | |
1425 | cur = names->count; | |
1426 | if (old == names_alloca) | |
1427 | names_alloca = names; | |
1428 | else | |
1429 | free (old); | |
1430 | } | |
1431 | result = GLOB_NOSPACE; | |
1432 | } | |
1433 | else | |
1434 | { | |
1435 | while (1) | |
1436 | { | |
1437 | struct globnames *old = names; | |
7a6dbc2f | 1438 | for (size_t i = 0; i < cur; ++i) |
6ec2e0f5 SDJ |
1439 | new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++] |
1440 | = names->name[i]; | |
1441 | names = names->next; | |
1442 | /* NB: we will not leak memory here if we exit without | |
1443 | freeing the current block assigned to OLD. At least | |
1444 | the very first block is always allocated on the stack | |
1445 | and this is the block assigned to OLD here. */ | |
1446 | if (names == NULL) | |
1447 | { | |
7a6dbc2f | 1448 | assert (old == init_names); |
6ec2e0f5 SDJ |
1449 | break; |
1450 | } | |
1451 | cur = names->count; | |
1452 | if (old == names_alloca) | |
1453 | names_alloca = names; | |
1454 | else | |
1455 | free (old); | |
1456 | } | |
1457 | ||
1458 | pglob->gl_pathv = new_gl_pathv; | |
1459 | ||
1460 | pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
1461 | ||
1462 | pglob->gl_flags = flags; | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | if (stream != NULL) | |
1467 | { | |
1468 | save = errno; | |
1469 | if (__glibc_unlikely (flags & GLOB_ALTDIRFUNC)) | |
1470 | (*pglob->gl_closedir) (stream); | |
1471 | else | |
1472 | closedir (stream); | |
1473 | __set_errno (save); | |
1474 | } | |
1475 | ||
1476 | return result; | |
1477 | } |