Commit | Line | Data |
---|---|---|
5e8754f9 | 1 | /* Copyright (C) 1991-2016 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 | |
5e8754f9 | 16 | <http://www.gnu.org/licenses/>. */ |
6ec2e0f5 SDJ |
17 | |
18 | #ifndef _LIBC | |
19 | /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc | |
5e8754f9 | 20 | optimizes away the pattern == NULL || pglob == NULL tests below. */ |
6ec2e0f5 SDJ |
21 | # define _GL_ARG_NONNULL(params) |
22 | # include <config.h> | |
23 | #endif | |
24 | ||
25 | #include <glob.h> | |
26 | ||
27 | #include <errno.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <stdbool.h> | |
31 | #include <stddef.h> | |
32 | #include <stdint.h> | |
5e8754f9 SDJ |
33 | |
34 | /* Outcomment the following line for production quality code. */ | |
35 | /* #define NDEBUG 1 */ | |
6ec2e0f5 | 36 | #include <assert.h> |
5e8754f9 SDJ |
37 | |
38 | #include <stdio.h> /* Needed on stupid SunOS for assert. */ | |
39 | ||
40 | #ifndef GLOB_ONLY_P | |
41 | ||
6ec2e0f5 | 42 | #include <unistd.h> |
5e8754f9 SDJ |
43 | #if !defined POSIX && defined _POSIX_VERSION |
44 | # define POSIX | |
45 | #endif | |
6ec2e0f5 | 46 | |
5e8754f9 | 47 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
6ec2e0f5 SDJ |
48 | # define WINDOWS32 |
49 | #endif | |
50 | ||
51 | #ifndef WINDOWS32 | |
52 | # include <pwd.h> | |
53 | #endif | |
54 | ||
55 | #include <errno.h> | |
5e8754f9 SDJ |
56 | #ifndef __set_errno |
57 | # define __set_errno(val) errno = (val) | |
58 | #endif | |
59 | ||
6ec2e0f5 SDJ |
60 | #include <dirent.h> |
61 | #include <stdlib.h> | |
62 | #include <string.h> | |
63 | #include <alloca.h> | |
64 | ||
65 | #ifdef _LIBC | |
66 | # undef strdup | |
67 | # define strdup(str) __strdup (str) | |
68 | # define sysconf(id) __sysconf (id) | |
69 | # define closedir(dir) __closedir (dir) | |
70 | # define opendir(name) __opendir (name) | |
71 | # define readdir(str) __readdir64 (str) | |
72 | # define getpwnam_r(name, bufp, buf, len, res) \ | |
5e8754f9 | 73 | __getpwnam_r (name, bufp, buf, len, res) |
6ec2e0f5 SDJ |
74 | # ifndef __stat64 |
75 | # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf) | |
76 | # endif | |
77 | # define struct_stat64 struct stat64 | |
78 | #else /* !_LIBC */ | |
79 | # define __getlogin_r(buf, len) getlogin_r (buf, len) | |
80 | # define __stat64(fname, buf) stat (fname, buf) | |
81 | # define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag) | |
82 | # define struct_stat64 struct stat | |
83 | # ifndef __MVS__ | |
84 | # define __alloca alloca | |
85 | # endif | |
86 | # define __readdir readdir | |
5e8754f9 | 87 | # define __glob_pattern_p glob_pattern_p |
6ec2e0f5 SDJ |
88 | # define COMPILE_GLOB64 |
89 | #endif /* _LIBC */ | |
90 | ||
91 | #include <fnmatch.h> | |
92 | ||
5e8754f9 | 93 | #include "flexmember.h" |
6ec2e0f5 | 94 | |
5e8754f9 SDJ |
95 | #ifdef _SC_GETPW_R_SIZE_MAX |
96 | # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX) | |
97 | #else | |
98 | # define GETPW_R_SIZE_MAX() (-1) | |
7a6dbc2f | 99 | #endif |
5e8754f9 SDJ |
100 | #ifdef _SC_LOGIN_NAME_MAX |
101 | # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) | |
102 | #else | |
103 | # define GET_LOGIN_NAME_MAX() (-1) | |
104 | #endif | |
105 | \f | |
106 | static const char *next_brace_sub (const char *begin, int flags) __THROWNL; | |
7a6dbc2f | 107 | |
6ec2e0f5 SDJ |
108 | /* A representation of a directory entry which does not depend on the |
109 | layout of struct dirent, or the size of ino_t. */ | |
110 | struct readdir_result | |
111 | { | |
112 | const char *name; | |
5e8754f9 SDJ |
113 | # if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE |
114 | uint8_t type; | |
115 | # endif | |
116 | bool skip_entry; | |
6ec2e0f5 SDJ |
117 | }; |
118 | ||
5e8754f9 SDJ |
119 | # if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE |
120 | /* Initializer based on the d_type member of struct dirent. */ | |
121 | # define D_TYPE_TO_RESULT(source) (source)->d_type, | |
122 | ||
123 | /* True if the directory entry D might be a symbolic link. */ | |
124 | static bool | |
125 | readdir_result_might_be_symlink (struct readdir_result d) | |
6ec2e0f5 | 126 | { |
5e8754f9 | 127 | return d.type == DT_UNKNOWN || d.type == DT_LNK; |
6ec2e0f5 SDJ |
128 | } |
129 | ||
5e8754f9 SDJ |
130 | /* True if the directory entry D might be a directory. */ |
131 | static bool | |
132 | readdir_result_might_be_dir (struct readdir_result d) | |
133 | { | |
134 | return d.type == DT_DIR || readdir_result_might_be_symlink (d); | |
135 | } | |
136 | # else /* defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE */ | |
137 | # define D_TYPE_TO_RESULT(source) | |
138 | ||
139 | /* If we do not have type information, symbolic links and directories | |
140 | are always a possibility. */ | |
141 | ||
142 | static bool | |
143 | readdir_result_might_be_symlink (struct readdir_result d) | |
144 | { | |
145 | return true; | |
146 | } | |
147 | ||
148 | static bool | |
149 | readdir_result_might_be_dir (struct readdir_result d) | |
150 | { | |
151 | return true; | |
152 | } | |
153 | ||
154 | # endif /* defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE */ | |
155 | ||
156 | # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__ | |
157 | /* Initializer for skip_entry. POSIX does not require that the d_ino | |
158 | field be present, and some systems do not provide it. */ | |
159 | # define D_INO_TO_RESULT(source) false, | |
160 | # else | |
161 | # define D_INO_TO_RESULT(source) (source)->d_ino == 0, | |
162 | # endif | |
163 | ||
6ec2e0f5 SDJ |
164 | /* Construct an initializer for a struct readdir_result object from a |
165 | struct dirent *. No copy of the name is made. */ | |
166 | #define READDIR_RESULT_INITIALIZER(source) \ | |
167 | { \ | |
168 | source->d_name, \ | |
169 | D_TYPE_TO_RESULT (source) \ | |
5e8754f9 | 170 | D_INO_TO_RESULT (source) \ |
6ec2e0f5 SDJ |
171 | } |
172 | ||
5e8754f9 SDJ |
173 | #endif /* !defined GLOB_ONLY_P */ |
174 | ||
6ec2e0f5 SDJ |
175 | /* Call gl_readdir on STREAM. This macro can be overridden to reduce |
176 | type safety if an old interface version needs to be supported. */ | |
177 | #ifndef GL_READDIR | |
178 | # define GL_READDIR(pglob, stream) ((pglob)->gl_readdir (stream)) | |
179 | #endif | |
180 | ||
181 | /* Extract name and type from directory entry. No copy of the name is | |
182 | made. If SOURCE is NULL, result name is NULL. Keep in sync with | |
183 | convert_dirent64 below. */ | |
184 | static struct readdir_result | |
185 | convert_dirent (const struct dirent *source) | |
186 | { | |
187 | if (source == NULL) | |
188 | { | |
189 | struct readdir_result result = { NULL, }; | |
190 | return result; | |
191 | } | |
192 | struct readdir_result result = READDIR_RESULT_INITIALIZER (source); | |
193 | return result; | |
194 | } | |
195 | ||
196 | #ifndef COMPILE_GLOB64 | |
197 | /* Like convert_dirent, but works on struct dirent64 instead. Keep in | |
198 | sync with convert_dirent above. */ | |
199 | static struct readdir_result | |
200 | convert_dirent64 (const struct dirent64 *source) | |
201 | { | |
202 | if (source == NULL) | |
203 | { | |
204 | struct readdir_result result = { NULL, }; | |
205 | return result; | |
206 | } | |
207 | struct readdir_result result = READDIR_RESULT_INITIALIZER (source); | |
208 | return result; | |
209 | } | |
210 | #endif | |
211 | ||
5e8754f9 SDJ |
212 | |
213 | #ifndef attribute_hidden | |
214 | # define attribute_hidden | |
215 | #endif | |
216 | ||
217 | #ifndef __attribute_noinline__ | |
218 | # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1) | |
219 | # define __attribute_noinline__ /* Ignore */ | |
220 | #else | |
221 | # define __attribute_noinline__ __attribute__ ((__noinline__)) | |
222 | # endif | |
223 | #endif | |
224 | ||
225 | #if ! defined __builtin_expect && __GNUC__ < 3 | |
226 | # define __builtin_expect(expr, expected) (expr) | |
227 | #endif | |
228 | ||
229 | #ifndef __glibc_unlikely | |
230 | # define __glibc_unlikely(expr) __builtin_expect (expr, 0) | |
231 | #endif | |
232 | ||
6ec2e0f5 SDJ |
233 | #ifndef _LIBC |
234 | /* The results of opendir() in this file are not used with dirfd and fchdir, | |
235 | and we do not leak fds to any single-threaded code that could use stdio, | |
236 | therefore save some unnecessary recursion in fchdir.c and opendir_safer.c. | |
237 | FIXME - if the kernel ever adds support for multi-thread safety for | |
238 | avoiding standard fds, then we should use opendir_safer. */ | |
239 | # ifdef GNULIB_defined_opendir | |
240 | # undef opendir | |
241 | # endif | |
242 | # ifdef GNULIB_defined_closedir | |
243 | # undef closedir | |
244 | # endif | |
245 | ||
246 | /* Just use malloc. */ | |
247 | # define __libc_use_alloca(n) false | |
248 | # define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0) | |
249 | # define extend_alloca_account(buf, len, newlen, avar) \ | |
250 | ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0) | |
251 | #endif | |
252 | ||
5e8754f9 SDJ |
253 | #ifndef __has_builtin |
254 | # define __has_builtin(x) 0 | |
255 | #endif | |
256 | ||
6ec2e0f5 SDJ |
257 | /* Set *R = A + B. Return true if the answer is mathematically |
258 | incorrect due to overflow; in this case, *R is the low order | |
5e8754f9 | 259 | bits of the correct answer.. */ |
6ec2e0f5 SDJ |
260 | |
261 | static bool | |
262 | size_add_wrapv (size_t a, size_t b, size_t *r) | |
263 | { | |
5e8754f9 | 264 | #if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow) |
6ec2e0f5 SDJ |
265 | return __builtin_add_overflow (a, b, r); |
266 | #else | |
267 | *r = a + b; | |
268 | return *r < a; | |
269 | #endif | |
270 | } | |
271 | ||
272 | static bool | |
273 | glob_use_alloca (size_t alloca_used, size_t len) | |
274 | { | |
275 | size_t size; | |
276 | return (!size_add_wrapv (alloca_used, len, &size) | |
277 | && __libc_use_alloca (size)); | |
278 | } | |
279 | ||
280 | static int glob_in_dir (const char *pattern, const char *directory, | |
281 | int flags, int (*errfunc) (const char *, int), | |
282 | glob_t *pglob, size_t alloca_used); | |
5e8754f9 SDJ |
283 | extern int __glob_pattern_type (const char *pattern, int quote) |
284 | attribute_hidden; | |
285 | ||
286 | #ifndef GLOB_ONLY_P | |
6ec2e0f5 SDJ |
287 | static int prefix_array (const char *prefix, char **array, size_t n) __THROWNL; |
288 | static int collated_compare (const void *, const void *) __THROWNL; | |
289 | ||
290 | ||
291 | /* Find the end of the sub-pattern in a brace expression. */ | |
292 | static const char * | |
293 | next_brace_sub (const char *cp, int flags) | |
294 | { | |
295 | size_t depth = 0; | |
296 | while (*cp != '\0') | |
297 | if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\') | |
298 | { | |
299 | if (*++cp == '\0') | |
300 | break; | |
301 | ++cp; | |
302 | } | |
303 | else | |
304 | { | |
305 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) | |
306 | break; | |
307 | ||
308 | if (*cp++ == '{') | |
309 | depth++; | |
310 | } | |
311 | ||
312 | return *cp != '\0' ? cp : NULL; | |
313 | } | |
314 | ||
5e8754f9 | 315 | #endif /* !defined GLOB_ONLY_P */ |
6ec2e0f5 SDJ |
316 | |
317 | /* Do glob searching for PATTERN, placing results in PGLOB. | |
318 | The bits defined above may be set in FLAGS. | |
319 | If a directory cannot be opened or read and ERRFUNC is not nil, | |
320 | it is called with the pathname that caused the error, and the | |
321 | 'errno' value from the failing call; if it returns non-zero | |
322 | 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored. | |
323 | If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned. | |
324 | Otherwise, 'glob' returns zero. */ | |
325 | int | |
326 | #ifdef GLOB_ATTRIBUTE | |
327 | GLOB_ATTRIBUTE | |
328 | #endif | |
329 | glob (const char *pattern, int flags, int (*errfunc) (const char *, int), | |
330 | glob_t *pglob) | |
331 | { | |
332 | const char *filename; | |
333 | char *dirname = NULL; | |
334 | size_t dirlen; | |
335 | int status; | |
336 | size_t oldcount; | |
337 | int meta; | |
338 | int dirname_modified; | |
339 | int malloc_dirname = 0; | |
340 | glob_t dirs; | |
341 | int retval = 0; | |
342 | size_t alloca_used = 0; | |
343 | ||
344 | if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) | |
345 | { | |
346 | __set_errno (EINVAL); | |
347 | return -1; | |
348 | } | |
349 | ||
350 | /* POSIX requires all slashes to be matched. This means that with | |
351 | a trailing slash we must match only directories. */ | |
352 | if (pattern[0] && pattern[strlen (pattern) - 1] == '/') | |
353 | flags |= GLOB_ONLYDIR; | |
354 | ||
355 | if (!(flags & GLOB_DOOFFS)) | |
356 | /* Have to do this so 'globfree' knows where to start freeing. It | |
357 | also makes all the code that uses gl_offs simpler. */ | |
358 | pglob->gl_offs = 0; | |
359 | ||
360 | if (flags & GLOB_BRACE) | |
361 | { | |
362 | const char *begin; | |
363 | ||
364 | if (flags & GLOB_NOESCAPE) | |
365 | begin = strchr (pattern, '{'); | |
366 | else | |
367 | { | |
368 | begin = pattern; | |
369 | while (1) | |
370 | { | |
371 | if (*begin == '\0') | |
372 | { | |
373 | begin = NULL; | |
374 | break; | |
375 | } | |
376 | ||
377 | if (*begin == '\\' && begin[1] != '\0') | |
378 | ++begin; | |
379 | else if (*begin == '{') | |
380 | break; | |
381 | ||
382 | ++begin; | |
383 | } | |
384 | } | |
385 | ||
386 | if (begin != NULL) | |
387 | { | |
388 | /* Allocate working buffer large enough for our work. Note that | |
5e8754f9 | 389 | we have at least an opening and closing brace. */ |
6ec2e0f5 SDJ |
390 | size_t firstc; |
391 | char *alt_start; | |
392 | const char *p; | |
393 | const char *next; | |
394 | const char *rest; | |
395 | size_t rest_len; | |
396 | char *onealt; | |
397 | size_t pattern_len = strlen (pattern) - 1; | |
398 | int alloca_onealt = glob_use_alloca (alloca_used, pattern_len); | |
399 | if (alloca_onealt) | |
400 | onealt = alloca_account (pattern_len, alloca_used); | |
401 | else | |
402 | { | |
403 | onealt = malloc (pattern_len); | |
404 | if (onealt == NULL) | |
5e8754f9 SDJ |
405 | { |
406 | if (!(flags & GLOB_APPEND)) | |
407 | { | |
408 | pglob->gl_pathc = 0; | |
409 | pglob->gl_pathv = NULL; | |
410 | } | |
411 | return GLOB_NOSPACE; | |
412 | } | |
6ec2e0f5 SDJ |
413 | } |
414 | ||
415 | /* We know the prefix for all sub-patterns. */ | |
416 | alt_start = mempcpy (onealt, pattern, begin - pattern); | |
417 | ||
418 | /* Find the first sub-pattern and at the same time find the | |
419 | rest after the closing brace. */ | |
420 | next = next_brace_sub (begin + 1, flags); | |
421 | if (next == NULL) | |
422 | { | |
423 | /* It is an invalid expression. */ | |
424 | illegal_brace: | |
425 | if (__glibc_unlikely (!alloca_onealt)) | |
426 | free (onealt); | |
5e8754f9 | 427 | return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); |
6ec2e0f5 SDJ |
428 | } |
429 | ||
430 | /* Now find the end of the whole brace expression. */ | |
431 | rest = next; | |
432 | while (*rest != '}') | |
433 | { | |
434 | rest = next_brace_sub (rest + 1, flags); | |
435 | if (rest == NULL) | |
436 | /* It is an illegal expression. */ | |
437 | goto illegal_brace; | |
438 | } | |
439 | /* Please note that we now can be sure the brace expression | |
440 | is well-formed. */ | |
441 | rest_len = strlen (++rest) + 1; | |
442 | ||
443 | /* We have a brace expression. BEGIN points to the opening {, | |
444 | NEXT points past the terminator of the first element, and END | |
445 | points past the final }. We will accumulate result names from | |
446 | recursive runs for each brace alternative in the buffer using | |
447 | GLOB_APPEND. */ | |
5e8754f9 SDJ |
448 | |
449 | if (!(flags & GLOB_APPEND)) | |
450 | { | |
451 | /* This call is to set a new vector, so clear out the | |
452 | vector so we can append to it. */ | |
453 | pglob->gl_pathc = 0; | |
454 | pglob->gl_pathv = NULL; | |
455 | } | |
6ec2e0f5 SDJ |
456 | firstc = pglob->gl_pathc; |
457 | ||
458 | p = begin + 1; | |
459 | while (1) | |
460 | { | |
461 | int result; | |
462 | ||
463 | /* Construct the new glob expression. */ | |
464 | mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len); | |
465 | ||
466 | result = glob (onealt, | |
467 | ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC)) | |
468 | | GLOB_APPEND), errfunc, pglob); | |
469 | ||
470 | /* If we got an error, return it. */ | |
471 | if (result && result != GLOB_NOMATCH) | |
472 | { | |
473 | if (__glibc_unlikely (!alloca_onealt)) | |
474 | free (onealt); | |
475 | if (!(flags & GLOB_APPEND)) | |
476 | { | |
477 | globfree (pglob); | |
478 | pglob->gl_pathc = 0; | |
479 | } | |
480 | return result; | |
481 | } | |
482 | ||
483 | if (*next == '}') | |
484 | /* We saw the last entry. */ | |
485 | break; | |
486 | ||
487 | p = next + 1; | |
488 | next = next_brace_sub (p, flags); | |
489 | assert (next != NULL); | |
490 | } | |
491 | ||
492 | if (__glibc_unlikely (!alloca_onealt)) | |
493 | free (onealt); | |
494 | ||
495 | if (pglob->gl_pathc != firstc) | |
496 | /* We found some entries. */ | |
497 | return 0; | |
498 | else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) | |
499 | return GLOB_NOMATCH; | |
500 | } | |
501 | } | |
502 | ||
5e8754f9 SDJ |
503 | if (!(flags & GLOB_APPEND)) |
504 | { | |
505 | pglob->gl_pathc = 0; | |
506 | if (!(flags & GLOB_DOOFFS)) | |
507 | pglob->gl_pathv = NULL; | |
508 | else | |
509 | { | |
510 | size_t i; | |
511 | ||
512 | if (pglob->gl_offs >= SIZE_MAX / sizeof (char *)) | |
513 | return GLOB_NOSPACE; | |
514 | ||
515 | pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *)); | |
516 | if (pglob->gl_pathv == NULL) | |
517 | return GLOB_NOSPACE; | |
518 | ||
519 | for (i = 0; i <= pglob->gl_offs; ++i) | |
520 | pglob->gl_pathv[i] = NULL; | |
521 | } | |
522 | } | |
523 | ||
6ec2e0f5 SDJ |
524 | oldcount = pglob->gl_pathc + pglob->gl_offs; |
525 | ||
526 | /* Find the filename. */ | |
527 | filename = strrchr (pattern, '/'); | |
528 | #if defined __MSDOS__ || defined WINDOWS32 | |
529 | /* The case of "d:pattern". Since ':' is not allowed in | |
530 | file names, we can safely assume that wherever it | |
531 | happens in pattern, it signals the filename part. This | |
532 | is so we could some day support patterns like "[a-z]:foo". */ | |
533 | if (filename == NULL) | |
534 | filename = strchr (pattern, ':'); | |
535 | #endif /* __MSDOS__ || WINDOWS32 */ | |
536 | dirname_modified = 0; | |
537 | if (filename == NULL) | |
538 | { | |
539 | /* This can mean two things: a simple name or "~name". The latter | |
540 | case is nothing but a notation for a directory. */ | |
541 | if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~') | |
542 | { | |
543 | dirname = (char *) pattern; | |
544 | dirlen = strlen (pattern); | |
545 | ||
546 | /* Set FILENAME to NULL as a special flag. This is ugly but | |
547 | other solutions would require much more code. We test for | |
548 | this special case below. */ | |
549 | filename = NULL; | |
550 | } | |
551 | else | |
552 | { | |
553 | if (__glibc_unlikely (pattern[0] == '\0')) | |
554 | { | |
555 | dirs.gl_pathv = NULL; | |
556 | goto no_matches; | |
557 | } | |
558 | ||
559 | filename = pattern; | |
5e8754f9 SDJ |
560 | #ifdef _AMIGA |
561 | dirname = (char *) ""; | |
562 | #else | |
6ec2e0f5 | 563 | dirname = (char *) "."; |
5e8754f9 | 564 | #endif |
6ec2e0f5 SDJ |
565 | dirlen = 0; |
566 | } | |
567 | } | |
568 | else if (filename == pattern | |
569 | || (filename == pattern + 1 && pattern[0] == '\\' | |
570 | && (flags & GLOB_NOESCAPE) == 0)) | |
571 | { | |
572 | /* "/pattern" or "\\/pattern". */ | |
573 | dirname = (char *) "/"; | |
574 | dirlen = 1; | |
575 | ++filename; | |
576 | } | |
577 | else | |
578 | { | |
579 | char *newp; | |
580 | dirlen = filename - pattern; | |
581 | #if defined __MSDOS__ || defined WINDOWS32 | |
582 | if (*filename == ':' | |
583 | || (filename > pattern + 1 && filename[-1] == ':')) | |
584 | { | |
585 | char *drive_spec; | |
586 | ||
587 | ++dirlen; | |
588 | drive_spec = __alloca (dirlen + 1); | |
589 | *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0'; | |
590 | /* For now, disallow wildcards in the drive spec, to | |
591 | prevent infinite recursion in glob. */ | |
592 | if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE))) | |
593 | return GLOB_NOMATCH; | |
594 | /* If this is "d:pattern", we need to copy ':' to DIRNAME | |
595 | as well. If it's "d:/pattern", don't remove the slash | |
596 | from "d:/", since "d:" and "d:/" are not the same.*/ | |
597 | } | |
598 | #endif | |
599 | if (glob_use_alloca (alloca_used, dirlen + 1)) | |
600 | newp = alloca_account (dirlen + 1, alloca_used); | |
601 | else | |
602 | { | |
603 | newp = malloc (dirlen + 1); | |
604 | if (newp == NULL) | |
605 | return GLOB_NOSPACE; | |
606 | malloc_dirname = 1; | |
607 | } | |
608 | *((char *) mempcpy (newp, pattern, dirlen)) = '\0'; | |
609 | dirname = newp; | |
610 | ++filename; | |
611 | ||
5e8754f9 | 612 | if (filename[0] == '\0' |
6ec2e0f5 | 613 | #if defined __MSDOS__ || defined WINDOWS32 |
5e8754f9 SDJ |
614 | && dirname[dirlen - 1] != ':' |
615 | && (dirlen < 3 || dirname[dirlen - 2] != ':' | |
616 | || dirname[dirlen - 1] != '/') | |
6ec2e0f5 | 617 | #endif |
5e8754f9 | 618 | && dirlen > 1) |
6ec2e0f5 SDJ |
619 | /* "pattern/". Expand "pattern", appending slashes. */ |
620 | { | |
621 | int orig_flags = flags; | |
5e8754f9 | 622 | int val; |
6ec2e0f5 SDJ |
623 | if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\') |
624 | { | |
625 | /* "pattern\\/". Remove the final backslash if it hasn't | |
626 | been quoted. */ | |
627 | char *p = (char *) &dirname[dirlen - 1]; | |
628 | ||
629 | while (p > dirname && p[-1] == '\\') --p; | |
630 | if ((&dirname[dirlen] - p) & 1) | |
631 | { | |
632 | *(char *) &dirname[--dirlen] = '\0'; | |
633 | flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); | |
634 | } | |
635 | } | |
5e8754f9 | 636 | val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); |
6ec2e0f5 SDJ |
637 | if (val == 0) |
638 | pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK) | |
639 | | (flags & GLOB_MARK)); | |
640 | else if (val == GLOB_NOMATCH && flags != orig_flags) | |
641 | { | |
642 | /* Make sure globfree (&dirs); is a nop. */ | |
643 | dirs.gl_pathv = NULL; | |
644 | flags = orig_flags; | |
645 | oldcount = pglob->gl_pathc + pglob->gl_offs; | |
646 | goto no_matches; | |
647 | } | |
648 | retval = val; | |
649 | goto out; | |
650 | } | |
651 | } | |
652 | ||
653 | if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~') | |
654 | { | |
655 | if (dirname[1] == '\0' || dirname[1] == '/' | |
656 | || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\' | |
657 | && (dirname[2] == '\0' || dirname[2] == '/'))) | |
658 | { | |
659 | /* Look up home directory. */ | |
660 | char *home_dir = getenv ("HOME"); | |
661 | int malloc_home_dir = 0; | |
5e8754f9 SDJ |
662 | # ifdef _AMIGA |
663 | if (home_dir == NULL || home_dir[0] == '\0') | |
664 | home_dir = "SYS:"; | |
665 | # else | |
666 | # ifdef WINDOWS32 | |
667 | /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference | |
668 | to HOME, because the user can change HOME. */ | |
6ec2e0f5 SDJ |
669 | if (home_dir == NULL || home_dir[0] == '\0') |
670 | { | |
671 | const char *home_drive = getenv ("HOMEDRIVE"); | |
672 | const char *home_path = getenv ("HOMEPATH"); | |
673 | ||
674 | if (home_drive != NULL && home_path != NULL) | |
675 | { | |
676 | size_t home_drive_len = strlen (home_drive); | |
677 | size_t home_path_len = strlen (home_path); | |
678 | char *mem = alloca (home_drive_len + home_path_len + 1); | |
679 | ||
680 | memcpy (mem, home_drive, home_drive_len); | |
681 | memcpy (mem + home_drive_len, home_path, home_path_len + 1); | |
682 | home_dir = mem; | |
683 | } | |
684 | else | |
685 | home_dir = "c:/users/default"; /* poor default */ | |
5e8754f9 SDJ |
686 | } |
687 | # else | |
688 | if (home_dir == NULL || home_dir[0] == '\0') | |
689 | { | |
690 | int success; | |
691 | char *name; | |
692 | int malloc_name = 0; | |
693 | size_t buflen = GET_LOGIN_NAME_MAX () + 1; | |
694 | ||
695 | if (buflen == 0) | |
696 | /* 'sysconf' does not support _SC_LOGIN_NAME_MAX. Try | |
697 | a moderate value. */ | |
698 | buflen = 20; | |
699 | if (glob_use_alloca (alloca_used, buflen)) | |
700 | name = alloca_account (buflen, alloca_used); | |
701 | else | |
6ec2e0f5 | 702 | { |
5e8754f9 SDJ |
703 | name = malloc (buflen); |
704 | if (name == NULL) | |
6ec2e0f5 SDJ |
705 | { |
706 | retval = GLOB_NOSPACE; | |
707 | goto out; | |
708 | } | |
5e8754f9 | 709 | malloc_name = 1; |
6ec2e0f5 | 710 | } |
5e8754f9 SDJ |
711 | |
712 | success = __getlogin_r (name, buflen) == 0; | |
713 | if (success) | |
7a6dbc2f | 714 | { |
5e8754f9 SDJ |
715 | struct passwd *p; |
716 | # if defined HAVE_GETPWNAM_R || defined _LIBC | |
717 | long int pwbuflenmax = GETPW_R_SIZE_MAX (); | |
718 | size_t pwbuflen = pwbuflenmax; | |
719 | char *pwtmpbuf; | |
720 | struct passwd pwbuf; | |
721 | char *malloc_pwtmpbuf = NULL; | |
722 | int save = errno; | |
723 | ||
724 | # ifndef _LIBC | |
725 | if (! (0 < pwbuflenmax && pwbuflenmax <= SIZE_MAX)) | |
726 | /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. | |
727 | Try a moderate value. */ | |
728 | pwbuflen = 1024; | |
729 | # endif | |
730 | if (glob_use_alloca (alloca_used, pwbuflen)) | |
731 | pwtmpbuf = alloca_account (pwbuflen, alloca_used); | |
732 | else | |
733 | { | |
734 | pwtmpbuf = malloc (pwbuflen); | |
735 | if (pwtmpbuf == NULL) | |
736 | { | |
8ae0786c GB |
737 | if (__glibc_unlikely (malloc_name)) |
738 | free (name); | |
5e8754f9 SDJ |
739 | retval = GLOB_NOSPACE; |
740 | goto out; | |
741 | } | |
742 | malloc_pwtmpbuf = pwtmpbuf; | |
743 | } | |
744 | ||
745 | while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p) | |
746 | != 0) | |
747 | { | |
748 | size_t newlen; | |
749 | bool v; | |
750 | if (errno != ERANGE) | |
751 | { | |
752 | p = NULL; | |
753 | break; | |
754 | } | |
755 | v = size_add_wrapv (pwbuflen, pwbuflen, &newlen); | |
756 | if (!v && malloc_pwtmpbuf == NULL | |
757 | && glob_use_alloca (alloca_used, newlen)) | |
758 | pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen, | |
759 | newlen, alloca_used); | |
760 | else | |
761 | { | |
762 | char *newp = (v ? NULL | |
763 | : realloc (malloc_pwtmpbuf, newlen)); | |
764 | if (newp == NULL) | |
765 | { | |
766 | free (malloc_pwtmpbuf); | |
8ae0786c GB |
767 | if (__glibc_unlikely (malloc_name)) |
768 | free (name); | |
5e8754f9 SDJ |
769 | retval = GLOB_NOSPACE; |
770 | goto out; | |
771 | } | |
772 | malloc_pwtmpbuf = pwtmpbuf = newp; | |
773 | } | |
774 | pwbuflen = newlen; | |
775 | __set_errno (save); | |
776 | } | |
777 | # else | |
778 | p = getpwnam (name); | |
779 | # endif | |
780 | if (__glibc_unlikely (malloc_name)) | |
781 | free (name); | |
782 | if (p != NULL) | |
783 | { | |
784 | if (malloc_pwtmpbuf == NULL) | |
785 | home_dir = p->pw_dir; | |
786 | else | |
787 | { | |
788 | size_t home_dir_len = strlen (p->pw_dir) + 1; | |
789 | if (glob_use_alloca (alloca_used, home_dir_len)) | |
790 | home_dir = alloca_account (home_dir_len, | |
791 | alloca_used); | |
792 | else | |
793 | { | |
794 | home_dir = malloc (home_dir_len); | |
795 | if (home_dir == NULL) | |
796 | { | |
797 | free (pwtmpbuf); | |
798 | retval = GLOB_NOSPACE; | |
799 | goto out; | |
800 | } | |
801 | malloc_home_dir = 1; | |
802 | } | |
803 | memcpy (home_dir, p->pw_dir, home_dir_len); | |
5e8754f9 SDJ |
804 | } |
805 | } | |
8ae0786c GB |
806 | free (malloc_pwtmpbuf); |
807 | } | |
808 | else | |
809 | { | |
810 | if (__glibc_unlikely (malloc_name)) | |
811 | free (name); | |
6ec2e0f5 SDJ |
812 | } |
813 | } | |
814 | if (home_dir == NULL || home_dir[0] == '\0') | |
815 | { | |
8ae0786c GB |
816 | if (__glibc_unlikely (malloc_home_dir)) |
817 | free (home_dir); | |
6ec2e0f5 SDJ |
818 | if (flags & GLOB_TILDE_CHECK) |
819 | { | |
6ec2e0f5 SDJ |
820 | retval = GLOB_NOMATCH; |
821 | goto out; | |
822 | } | |
823 | else | |
8ae0786c GB |
824 | { |
825 | home_dir = (char *) "~"; /* No luck. */ | |
826 | malloc_home_dir = 0; | |
827 | } | |
6ec2e0f5 | 828 | } |
5e8754f9 SDJ |
829 | # endif /* WINDOWS32 */ |
830 | # endif | |
6ec2e0f5 SDJ |
831 | /* Now construct the full directory. */ |
832 | if (dirname[1] == '\0') | |
833 | { | |
834 | if (__glibc_unlikely (malloc_dirname)) | |
835 | free (dirname); | |
836 | ||
837 | dirname = home_dir; | |
838 | dirlen = strlen (dirname); | |
839 | malloc_dirname = malloc_home_dir; | |
840 | } | |
841 | else | |
842 | { | |
843 | char *newp; | |
844 | size_t home_len = strlen (home_dir); | |
845 | int use_alloca = glob_use_alloca (alloca_used, home_len + dirlen); | |
846 | if (use_alloca) | |
847 | newp = alloca_account (home_len + dirlen, alloca_used); | |
848 | else | |
849 | { | |
850 | newp = malloc (home_len + dirlen); | |
851 | if (newp == NULL) | |
852 | { | |
853 | if (__glibc_unlikely (malloc_home_dir)) | |
854 | free (home_dir); | |
855 | retval = GLOB_NOSPACE; | |
856 | goto out; | |
857 | } | |
858 | } | |
859 | ||
860 | mempcpy (mempcpy (newp, home_dir, home_len), | |
861 | &dirname[1], dirlen); | |
862 | ||
863 | if (__glibc_unlikely (malloc_dirname)) | |
864 | free (dirname); | |
865 | ||
866 | dirname = newp; | |
867 | dirlen += home_len - 1; | |
868 | malloc_dirname = !use_alloca; | |
8ae0786c GB |
869 | |
870 | if (__glibc_unlikely (malloc_home_dir)) | |
871 | free (home_dir); | |
6ec2e0f5 SDJ |
872 | } |
873 | dirname_modified = 1; | |
874 | } | |
5e8754f9 | 875 | # if !defined _AMIGA && !defined WINDOWS32 |
6ec2e0f5 SDJ |
876 | else |
877 | { | |
878 | char *end_name = strchr (dirname, '/'); | |
879 | char *user_name; | |
880 | int malloc_user_name = 0; | |
881 | char *unescape = NULL; | |
882 | ||
883 | if (!(flags & GLOB_NOESCAPE)) | |
884 | { | |
885 | if (end_name == NULL) | |
886 | { | |
887 | unescape = strchr (dirname, '\\'); | |
888 | if (unescape) | |
889 | end_name = strchr (unescape, '\0'); | |
890 | } | |
891 | else | |
892 | unescape = memchr (dirname, '\\', end_name - dirname); | |
893 | } | |
894 | if (end_name == NULL) | |
895 | user_name = dirname + 1; | |
896 | else | |
897 | { | |
898 | char *newp; | |
899 | if (glob_use_alloca (alloca_used, end_name - dirname)) | |
900 | newp = alloca_account (end_name - dirname, alloca_used); | |
901 | else | |
902 | { | |
903 | newp = malloc (end_name - dirname); | |
904 | if (newp == NULL) | |
905 | { | |
906 | retval = GLOB_NOSPACE; | |
907 | goto out; | |
908 | } | |
909 | malloc_user_name = 1; | |
910 | } | |
911 | if (unescape != NULL) | |
912 | { | |
913 | char *p = mempcpy (newp, dirname + 1, | |
914 | unescape - dirname - 1); | |
915 | char *q = unescape; | |
5e8754f9 | 916 | while (*q != '\0') |
6ec2e0f5 SDJ |
917 | { |
918 | if (*q == '\\') | |
919 | { | |
5e8754f9 | 920 | if (q[1] == '\0') |
6ec2e0f5 SDJ |
921 | { |
922 | /* "~fo\\o\\" unescape to user_name "foo\\", | |
923 | but "~fo\\o\\/" unescape to user_name | |
924 | "foo". */ | |
925 | if (filename == NULL) | |
926 | *p++ = '\\'; | |
927 | break; | |
928 | } | |
929 | ++q; | |
930 | } | |
931 | *p++ = *q++; | |
932 | } | |
933 | *p = '\0'; | |
934 | } | |
935 | else | |
5e8754f9 | 936 | *((char *) mempcpy (newp, dirname + 1, end_name - dirname)) |
6ec2e0f5 SDJ |
937 | = '\0'; |
938 | user_name = newp; | |
939 | } | |
940 | ||
941 | /* Look up specific user's home directory. */ | |
942 | { | |
943 | struct passwd *p; | |
944 | # if defined HAVE_GETPWNAM_R || defined _LIBC | |
5e8754f9 SDJ |
945 | long int buflenmax = GETPW_R_SIZE_MAX (); |
946 | size_t buflen = buflenmax; | |
947 | char *pwtmpbuf; | |
948 | char *malloc_pwtmpbuf = NULL; | |
6ec2e0f5 | 949 | struct passwd pwbuf; |
5e8754f9 SDJ |
950 | int save = errno; |
951 | ||
952 | # ifndef _LIBC | |
953 | if (! (0 <= buflenmax && buflenmax <= SIZE_MAX)) | |
954 | /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a | |
955 | moderate value. */ | |
956 | buflen = 1024; | |
957 | # endif | |
958 | if (glob_use_alloca (alloca_used, buflen)) | |
959 | pwtmpbuf = alloca_account (buflen, alloca_used); | |
960 | else | |
6ec2e0f5 | 961 | { |
5e8754f9 SDJ |
962 | pwtmpbuf = malloc (buflen); |
963 | if (pwtmpbuf == NULL) | |
6ec2e0f5 | 964 | { |
5e8754f9 SDJ |
965 | nomem_getpw: |
966 | if (__glibc_unlikely (malloc_user_name)) | |
967 | free (user_name); | |
6ec2e0f5 SDJ |
968 | retval = GLOB_NOSPACE; |
969 | goto out; | |
970 | } | |
5e8754f9 SDJ |
971 | malloc_pwtmpbuf = pwtmpbuf; |
972 | } | |
973 | ||
974 | while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0) | |
975 | { | |
976 | size_t newlen; | |
977 | bool v; | |
978 | if (errno != ERANGE) | |
979 | { | |
980 | p = NULL; | |
981 | break; | |
982 | } | |
983 | v = size_add_wrapv (buflen, buflen, &newlen); | |
984 | if (!v && malloc_pwtmpbuf == NULL | |
985 | && glob_use_alloca (alloca_used, newlen)) | |
986 | pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen, | |
987 | newlen, alloca_used); | |
988 | else | |
989 | { | |
990 | char *newp = v ? NULL : realloc (malloc_pwtmpbuf, newlen); | |
991 | if (newp == NULL) | |
992 | { | |
993 | free (malloc_pwtmpbuf); | |
994 | goto nomem_getpw; | |
995 | } | |
996 | malloc_pwtmpbuf = pwtmpbuf = newp; | |
997 | } | |
998 | __set_errno (save); | |
6ec2e0f5 SDJ |
999 | } |
1000 | # else | |
1001 | p = getpwnam (user_name); | |
1002 | # endif | |
1003 | ||
1004 | if (__glibc_unlikely (malloc_user_name)) | |
1005 | free (user_name); | |
1006 | ||
1007 | /* If we found a home directory use this. */ | |
1008 | if (p != NULL) | |
1009 | { | |
1010 | size_t home_len = strlen (p->pw_dir); | |
1011 | size_t rest_len = end_name == NULL ? 0 : strlen (end_name); | |
1012 | ||
1013 | if (__glibc_unlikely (malloc_dirname)) | |
1014 | free (dirname); | |
1015 | malloc_dirname = 0; | |
1016 | ||
1017 | if (glob_use_alloca (alloca_used, home_len + rest_len + 1)) | |
1018 | dirname = alloca_account (home_len + rest_len + 1, | |
1019 | alloca_used); | |
1020 | else | |
1021 | { | |
1022 | dirname = malloc (home_len + rest_len + 1); | |
1023 | if (dirname == NULL) | |
1024 | { | |
5e8754f9 | 1025 | free (malloc_pwtmpbuf); |
6ec2e0f5 SDJ |
1026 | retval = GLOB_NOSPACE; |
1027 | goto out; | |
1028 | } | |
1029 | malloc_dirname = 1; | |
1030 | } | |
5e8754f9 SDJ |
1031 | *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len), |
1032 | end_name, rest_len)) = '\0'; | |
6ec2e0f5 SDJ |
1033 | |
1034 | dirlen = home_len + rest_len; | |
1035 | dirname_modified = 1; | |
5e8754f9 SDJ |
1036 | |
1037 | free (malloc_pwtmpbuf); | |
6ec2e0f5 SDJ |
1038 | } |
1039 | else | |
1040 | { | |
5e8754f9 SDJ |
1041 | free (malloc_pwtmpbuf); |
1042 | ||
6ec2e0f5 | 1043 | if (flags & GLOB_TILDE_CHECK) |
8ae0786c GB |
1044 | { |
1045 | /* We have to regard it as an error if we cannot find the | |
1046 | home directory. */ | |
1047 | retval = GLOB_NOMATCH; | |
1048 | goto out; | |
1049 | } | |
6ec2e0f5 SDJ |
1050 | } |
1051 | } | |
1052 | } | |
5e8754f9 | 1053 | # endif /* Not Amiga && not WINDOWS32. */ |
6ec2e0f5 SDJ |
1054 | } |
1055 | ||
1056 | /* Now test whether we looked for "~" or "~NAME". In this case we | |
1057 | can give the answer now. */ | |
1058 | if (filename == NULL) | |
1059 | { | |
5e8754f9 SDJ |
1060 | struct stat st; |
1061 | struct_stat64 st64; | |
1062 | ||
1063 | /* Return the directory if we don't check for error or if it exists. */ | |
1064 | if ((flags & GLOB_NOCHECK) | |
1065 | || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) | |
1066 | ? ((*pglob->gl_stat) (dirname, &st) == 0 | |
1067 | && S_ISDIR (st.st_mode)) | |
1068 | : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode))))) | |
6ec2e0f5 | 1069 | { |
5e8754f9 SDJ |
1070 | size_t newcount = pglob->gl_pathc + pglob->gl_offs; |
1071 | char **new_gl_pathv; | |
6ec2e0f5 | 1072 | |
5e8754f9 SDJ |
1073 | if (newcount > SIZE_MAX / sizeof (char *) - 2) |
1074 | { | |
1075 | nospace: | |
1076 | free (pglob->gl_pathv); | |
1077 | pglob->gl_pathv = NULL; | |
1078 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1079 | retval = GLOB_NOSPACE; |
1080 | goto out; | |
5e8754f9 | 1081 | } |
6ec2e0f5 | 1082 | |
5e8754f9 SDJ |
1083 | new_gl_pathv = realloc (pglob->gl_pathv, |
1084 | (newcount + 2) * sizeof (char *)); | |
1085 | if (new_gl_pathv == NULL) | |
6ec2e0f5 | 1086 | goto nospace; |
5e8754f9 SDJ |
1087 | pglob->gl_pathv = new_gl_pathv; |
1088 | ||
1089 | if (flags & GLOB_MARK) | |
1090 | { | |
1091 | char *p; | |
1092 | pglob->gl_pathv[newcount] = malloc (dirlen + 2); | |
1093 | if (pglob->gl_pathv[newcount] == NULL) | |
1094 | goto nospace; | |
1095 | p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen); | |
1096 | p[0] = '/'; | |
1097 | p[1] = '\0'; | |
8ae0786c GB |
1098 | if (__glibc_unlikely (malloc_dirname)) |
1099 | free (dirname); | |
5e8754f9 | 1100 | } |
6ec2e0f5 SDJ |
1101 | else |
1102 | { | |
8ae0786c GB |
1103 | if (__glibc_unlikely (malloc_dirname)) |
1104 | pglob->gl_pathv[newcount] = dirname; | |
1105 | else | |
1106 | { | |
1107 | pglob->gl_pathv[newcount] = strdup (dirname); | |
1108 | if (pglob->gl_pathv[newcount] == NULL) | |
1109 | goto nospace; | |
1110 | } | |
6ec2e0f5 | 1111 | } |
5e8754f9 SDJ |
1112 | pglob->gl_pathv[++newcount] = NULL; |
1113 | ++pglob->gl_pathc; | |
1114 | pglob->gl_flags = flags; | |
1115 | ||
1116 | return 0; | |
6ec2e0f5 SDJ |
1117 | } |
1118 | ||
5e8754f9 | 1119 | /* Not found. */ |
8ae0786c GB |
1120 | retval = GLOB_NOMATCH; |
1121 | goto out; | |
6ec2e0f5 SDJ |
1122 | } |
1123 | ||
1124 | meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE)); | |
1125 | /* meta is 1 if correct glob pattern containing metacharacters. | |
1126 | If meta has bit (1 << 2) set, it means there was an unterminated | |
1127 | [ which we handle the same, using fnmatch. Broken unterminated | |
1128 | pattern bracket expressions ought to be rare enough that it is | |
1129 | not worth special casing them, fnmatch will do the right thing. */ | |
5e8754f9 | 1130 | if (meta & 5) |
6ec2e0f5 SDJ |
1131 | { |
1132 | /* The directory name contains metacharacters, so we | |
1133 | have to glob for the directory, and then glob for | |
1134 | the pattern in each directory found. */ | |
1135 | size_t i; | |
1136 | ||
1137 | if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\') | |
1138 | { | |
1139 | /* "foo\\/bar". Remove the final backslash from dirname | |
1140 | if it has not been quoted. */ | |
1141 | char *p = (char *) &dirname[dirlen - 1]; | |
1142 | ||
1143 | while (p > dirname && p[-1] == '\\') --p; | |
1144 | if ((&dirname[dirlen] - p) & 1) | |
1145 | *(char *) &dirname[--dirlen] = '\0'; | |
1146 | } | |
1147 | ||
1148 | if (__glibc_unlikely ((flags & GLOB_ALTDIRFUNC) != 0)) | |
1149 | { | |
1150 | /* Use the alternative access functions also in the recursive | |
1151 | call. */ | |
1152 | dirs.gl_opendir = pglob->gl_opendir; | |
1153 | dirs.gl_readdir = pglob->gl_readdir; | |
1154 | dirs.gl_closedir = pglob->gl_closedir; | |
1155 | dirs.gl_stat = pglob->gl_stat; | |
1156 | dirs.gl_lstat = pglob->gl_lstat; | |
1157 | } | |
1158 | ||
1159 | status = glob (dirname, | |
1160 | ((flags & (GLOB_ERR | GLOB_NOESCAPE | |
1161 | | GLOB_ALTDIRFUNC)) | |
1162 | | GLOB_NOSORT | GLOB_ONLYDIR), | |
1163 | errfunc, &dirs); | |
1164 | if (status != 0) | |
1165 | { | |
1166 | if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH) | |
8ae0786c GB |
1167 | { |
1168 | retval = status; | |
1169 | goto out; | |
1170 | } | |
6ec2e0f5 SDJ |
1171 | goto no_matches; |
1172 | } | |
1173 | ||
1174 | /* We have successfully globbed the preceding directory name. | |
1175 | For each name we found, call glob_in_dir on it and FILENAME, | |
1176 | appending the results to PGLOB. */ | |
1177 | for (i = 0; i < dirs.gl_pathc; ++i) | |
1178 | { | |
1179 | size_t old_pathc; | |
1180 | ||
5e8754f9 SDJ |
1181 | #ifdef SHELL |
1182 | { | |
1183 | /* Make globbing interruptible in the bash shell. */ | |
1184 | extern int interrupt_state; | |
1185 | ||
1186 | if (interrupt_state) | |
1187 | { | |
1188 | globfree (&dirs); | |
8ae0786c GB |
1189 | retval = GLOB_ABORTED; |
1190 | goto out; | |
5e8754f9 SDJ |
1191 | } |
1192 | } | |
1193 | #endif /* SHELL. */ | |
1194 | ||
6ec2e0f5 SDJ |
1195 | old_pathc = pglob->gl_pathc; |
1196 | status = glob_in_dir (filename, dirs.gl_pathv[i], | |
1197 | ((flags | GLOB_APPEND) | |
1198 | & ~(GLOB_NOCHECK | GLOB_NOMAGIC)), | |
1199 | errfunc, pglob, alloca_used); | |
1200 | if (status == GLOB_NOMATCH) | |
1201 | /* No matches in this directory. Try the next. */ | |
1202 | continue; | |
1203 | ||
1204 | if (status != 0) | |
1205 | { | |
1206 | globfree (&dirs); | |
1207 | globfree (pglob); | |
1208 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1209 | retval = status; |
1210 | goto out; | |
6ec2e0f5 SDJ |
1211 | } |
1212 | ||
1213 | /* Stick the directory on the front of each name. */ | |
1214 | if (prefix_array (dirs.gl_pathv[i], | |
1215 | &pglob->gl_pathv[old_pathc + pglob->gl_offs], | |
1216 | pglob->gl_pathc - old_pathc)) | |
1217 | { | |
1218 | globfree (&dirs); | |
1219 | globfree (pglob); | |
1220 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1221 | retval = GLOB_NOSPACE; |
1222 | goto out; | |
6ec2e0f5 SDJ |
1223 | } |
1224 | } | |
1225 | ||
1226 | flags |= GLOB_MAGCHAR; | |
1227 | ||
1228 | /* We have ignored the GLOB_NOCHECK flag in the 'glob_in_dir' calls. | |
1229 | But if we have not found any matching entry and the GLOB_NOCHECK | |
1230 | flag was set we must return the input pattern itself. */ | |
1231 | if (pglob->gl_pathc + pglob->gl_offs == oldcount) | |
1232 | { | |
1233 | no_matches: | |
1234 | /* No matches. */ | |
1235 | if (flags & GLOB_NOCHECK) | |
1236 | { | |
1237 | size_t newcount = pglob->gl_pathc + pglob->gl_offs; | |
1238 | char **new_gl_pathv; | |
1239 | ||
1240 | if (newcount > SIZE_MAX / sizeof (char *) - 2) | |
1241 | { | |
1242 | nospace2: | |
1243 | globfree (&dirs); | |
8ae0786c GB |
1244 | retval = GLOB_NOSPACE; |
1245 | goto out; | |
6ec2e0f5 SDJ |
1246 | } |
1247 | ||
1248 | new_gl_pathv = realloc (pglob->gl_pathv, | |
1249 | (newcount + 2) * sizeof (char *)); | |
1250 | if (new_gl_pathv == NULL) | |
1251 | goto nospace2; | |
1252 | pglob->gl_pathv = new_gl_pathv; | |
1253 | ||
1254 | pglob->gl_pathv[newcount] = strdup (pattern); | |
1255 | if (pglob->gl_pathv[newcount] == NULL) | |
1256 | { | |
1257 | globfree (&dirs); | |
1258 | globfree (pglob); | |
1259 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1260 | retval = GLOB_NOSPACE; |
1261 | goto out; | |
6ec2e0f5 SDJ |
1262 | } |
1263 | ||
1264 | ++pglob->gl_pathc; | |
1265 | ++newcount; | |
1266 | ||
1267 | pglob->gl_pathv[newcount] = NULL; | |
1268 | pglob->gl_flags = flags; | |
1269 | } | |
1270 | else | |
1271 | { | |
1272 | globfree (&dirs); | |
8ae0786c GB |
1273 | retval = GLOB_NOMATCH; |
1274 | goto out; | |
6ec2e0f5 SDJ |
1275 | } |
1276 | } | |
1277 | ||
1278 | globfree (&dirs); | |
1279 | } | |
1280 | else | |
1281 | { | |
1282 | size_t old_pathc = pglob->gl_pathc; | |
1283 | int orig_flags = flags; | |
1284 | ||
5e8754f9 | 1285 | if (meta & 2) |
6ec2e0f5 SDJ |
1286 | { |
1287 | char *p = strchr (dirname, '\\'), *q; | |
1288 | /* We need to unescape the dirname string. It is certainly | |
1289 | allocated by alloca, as otherwise filename would be NULL | |
1290 | or dirname wouldn't contain backslashes. */ | |
1291 | q = p; | |
1292 | do | |
1293 | { | |
1294 | if (*p == '\\') | |
1295 | { | |
1296 | *q = *++p; | |
1297 | --dirlen; | |
1298 | } | |
1299 | else | |
1300 | *q = *p; | |
1301 | ++q; | |
1302 | } | |
1303 | while (*p++ != '\0'); | |
1304 | dirname_modified = 1; | |
1305 | } | |
1306 | if (dirname_modified) | |
1307 | flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); | |
1308 | status = glob_in_dir (filename, dirname, flags, errfunc, pglob, | |
1309 | alloca_used); | |
1310 | if (status != 0) | |
1311 | { | |
1312 | if (status == GLOB_NOMATCH && flags != orig_flags | |
1313 | && pglob->gl_pathc + pglob->gl_offs == oldcount) | |
1314 | { | |
1315 | /* Make sure globfree (&dirs); is a nop. */ | |
1316 | dirs.gl_pathv = NULL; | |
1317 | flags = orig_flags; | |
1318 | goto no_matches; | |
1319 | } | |
8ae0786c GB |
1320 | retval = status; |
1321 | goto out; | |
6ec2e0f5 SDJ |
1322 | } |
1323 | ||
1324 | if (dirlen > 0) | |
1325 | { | |
1326 | /* Stick the directory on the front of each name. */ | |
1327 | if (prefix_array (dirname, | |
1328 | &pglob->gl_pathv[old_pathc + pglob->gl_offs], | |
1329 | pglob->gl_pathc - old_pathc)) | |
1330 | { | |
1331 | globfree (pglob); | |
1332 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1333 | retval = GLOB_NOSPACE; |
1334 | goto out; | |
6ec2e0f5 SDJ |
1335 | } |
1336 | } | |
1337 | } | |
1338 | ||
1339 | if (flags & GLOB_MARK) | |
1340 | { | |
1341 | /* Append slashes to directory names. */ | |
1342 | size_t i; | |
5e8754f9 SDJ |
1343 | struct stat st; |
1344 | struct_stat64 st64; | |
6ec2e0f5 SDJ |
1345 | |
1346 | for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i) | |
5e8754f9 SDJ |
1347 | if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) |
1348 | ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0 | |
1349 | && S_ISDIR (st.st_mode)) | |
1350 | : (__stat64 (pglob->gl_pathv[i], &st64) == 0 | |
1351 | && S_ISDIR (st64.st_mode)))) | |
6ec2e0f5 SDJ |
1352 | { |
1353 | size_t len = strlen (pglob->gl_pathv[i]) + 2; | |
1354 | char *new = realloc (pglob->gl_pathv[i], len); | |
1355 | if (new == NULL) | |
1356 | { | |
1357 | globfree (pglob); | |
1358 | pglob->gl_pathc = 0; | |
8ae0786c GB |
1359 | retval = GLOB_NOSPACE; |
1360 | goto out; | |
6ec2e0f5 SDJ |
1361 | } |
1362 | strcpy (&new[len - 2], "/"); | |
1363 | pglob->gl_pathv[i] = new; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | if (!(flags & GLOB_NOSORT)) | |
1368 | { | |
1369 | /* Sort the vector. */ | |
1370 | qsort (&pglob->gl_pathv[oldcount], | |
1371 | pglob->gl_pathc + pglob->gl_offs - oldcount, | |
1372 | sizeof (char *), collated_compare); | |
1373 | } | |
1374 | ||
1375 | out: | |
1376 | if (__glibc_unlikely (malloc_dirname)) | |
1377 | free (dirname); | |
1378 | ||
1379 | return retval; | |
1380 | } | |
1381 | #if defined _LIBC && !defined glob | |
1382 | libc_hidden_def (glob) | |
1383 | #endif | |
1384 | ||
1385 | ||
5e8754f9 SDJ |
1386 | #ifndef GLOB_ONLY_P |
1387 | ||
1388 | /* Free storage allocated in PGLOB by a previous 'glob' call. */ | |
1389 | void | |
1390 | globfree (glob_t *pglob) | |
1391 | { | |
1392 | if (pglob->gl_pathv != NULL) | |
1393 | { | |
1394 | size_t i; | |
1395 | for (i = 0; i < pglob->gl_pathc; ++i) | |
1396 | free (pglob->gl_pathv[pglob->gl_offs + i]); | |
1397 | free (pglob->gl_pathv); | |
1398 | pglob->gl_pathv = NULL; | |
1399 | } | |
1400 | } | |
1401 | #if defined _LIBC && !defined globfree | |
1402 | libc_hidden_def (globfree) | |
1403 | #endif | |
1404 | ||
1405 | ||
6ec2e0f5 SDJ |
1406 | /* Do a collated comparison of A and B. */ |
1407 | static int | |
1408 | collated_compare (const void *a, const void *b) | |
1409 | { | |
1410 | char *const *ps1 = a; char *s1 = *ps1; | |
1411 | char *const *ps2 = b; char *s2 = *ps2; | |
1412 | ||
1413 | if (s1 == s2) | |
1414 | return 0; | |
1415 | if (s1 == NULL) | |
1416 | return 1; | |
1417 | if (s2 == NULL) | |
1418 | return -1; | |
1419 | return strcoll (s1, s2); | |
1420 | } | |
1421 | ||
1422 | ||
1423 | /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's | |
1424 | elements in place. Return nonzero if out of memory, zero if successful. | |
1425 | A slash is inserted between DIRNAME and each elt of ARRAY, | |
1426 | unless DIRNAME is just "/". Each old element of ARRAY is freed. */ | |
1427 | static int | |
1428 | prefix_array (const char *dirname, char **array, size_t n) | |
1429 | { | |
1430 | size_t i; | |
1431 | size_t dirlen = strlen (dirname); | |
5e8754f9 SDJ |
1432 | #if defined __MSDOS__ || defined WINDOWS32 |
1433 | int sep_char = '/'; | |
1434 | # define DIRSEP_CHAR sep_char | |
1435 | #else | |
1436 | # define DIRSEP_CHAR '/' | |
1437 | #endif | |
6ec2e0f5 SDJ |
1438 | |
1439 | if (dirlen == 1 && dirname[0] == '/') | |
1440 | /* DIRNAME is just "/", so normal prepending would get us "//foo". | |
1441 | We want "/foo" instead, so don't prepend any chars from DIRNAME. */ | |
1442 | dirlen = 0; | |
1443 | #if defined __MSDOS__ || defined WINDOWS32 | |
5e8754f9 | 1444 | else if (dirlen > 1) |
6ec2e0f5 SDJ |
1445 | { |
1446 | if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':') | |
1447 | /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */ | |
1448 | --dirlen; | |
1449 | else if (dirname[dirlen - 1] == ':') | |
1450 | { | |
1451 | /* DIRNAME is "d:". Use ':' instead of '/'. */ | |
1452 | --dirlen; | |
5e8754f9 | 1453 | sep_char = ':'; |
6ec2e0f5 SDJ |
1454 | } |
1455 | } | |
1456 | #endif | |
1457 | ||
1458 | for (i = 0; i < n; ++i) | |
1459 | { | |
1460 | size_t eltlen = strlen (array[i]) + 1; | |
1461 | char *new = malloc (dirlen + 1 + eltlen); | |
1462 | if (new == NULL) | |
1463 | { | |
1464 | while (i > 0) | |
1465 | free (array[--i]); | |
1466 | return 1; | |
1467 | } | |
1468 | ||
1469 | { | |
1470 | char *endp = mempcpy (new, dirname, dirlen); | |
5e8754f9 | 1471 | *endp++ = DIRSEP_CHAR; |
6ec2e0f5 SDJ |
1472 | mempcpy (endp, array[i], eltlen); |
1473 | } | |
1474 | free (array[i]); | |
1475 | array[i] = new; | |
1476 | } | |
1477 | ||
1478 | return 0; | |
1479 | } | |
1480 | ||
5e8754f9 SDJ |
1481 | |
1482 | /* We must not compile this function twice. */ | |
1483 | #ifndef NO_GLOB_PATTERN_P | |
1484 | int | |
1485 | __glob_pattern_type (const char *pattern, int quote) | |
1486 | { | |
1487 | const char *p; | |
1488 | int ret = 0; | |
1489 | ||
1490 | for (p = pattern; *p != '\0'; ++p) | |
1491 | switch (*p) | |
1492 | { | |
1493 | case '?': | |
1494 | case '*': | |
1495 | return 1; | |
1496 | ||
1497 | case '\\': | |
1498 | if (quote) | |
1499 | { | |
1500 | if (p[1] != '\0') | |
1501 | ++p; | |
1502 | ret |= 2; | |
1503 | } | |
1504 | break; | |
1505 | ||
1506 | case '[': | |
1507 | ret |= 4; | |
1508 | break; | |
1509 | ||
1510 | case ']': | |
1511 | if (ret & 4) | |
1512 | return 1; | |
1513 | break; | |
1514 | } | |
1515 | ||
1516 | return ret; | |
1517 | } | |
1518 | ||
1519 | /* Return nonzero if PATTERN contains any metacharacters. | |
1520 | Metacharacters can be quoted with backslashes if QUOTE is nonzero. */ | |
1521 | int | |
1522 | __glob_pattern_p (const char *pattern, int quote) | |
1523 | { | |
1524 | return __glob_pattern_type (pattern, quote) == 1; | |
1525 | } | |
1526 | # ifdef _LIBC | |
1527 | weak_alias (__glob_pattern_p, glob_pattern_p) | |
1528 | # endif | |
1529 | #endif | |
1530 | ||
1531 | ||
1532 | /* We put this in a separate function mainly to allow the memory | |
1533 | allocated with alloca to be recycled. */ | |
1534 | static int | |
1535 | __attribute_noinline__ | |
1536 | link_exists2_p (const char *dir, size_t dirlen, const char *fname, | |
1537 | glob_t *pglob | |
1538 | # if !defined _LIBC && !HAVE_FSTATAT | |
1539 | , int flags | |
1540 | # endif | |
1541 | ) | |
1542 | { | |
1543 | size_t fnamelen = strlen (fname); | |
1544 | char *fullname = __alloca (dirlen + 1 + fnamelen + 1); | |
1545 | struct stat st; | |
1546 | ||
1547 | mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1), | |
1548 | fname, fnamelen + 1); | |
1549 | ||
1550 | # if !defined _LIBC && !HAVE_FSTATAT | |
1551 | if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) == 0, 1)) | |
1552 | { | |
1553 | struct_stat64 st64; | |
1554 | return __stat64 (fullname, &st64) == 0; | |
1555 | } | |
1556 | # endif | |
1557 | return (*pglob->gl_stat) (fullname, &st) == 0; | |
1558 | } | |
1559 | ||
1560 | /* Return true if DIR/FNAME exists. */ | |
1561 | static int | |
1562 | link_exists_p (int dfd, const char *dir, size_t dirlen, const char *fname, | |
1563 | glob_t *pglob, int flags) | |
1564 | { | |
1565 | # if defined _LIBC || HAVE_FSTATAT | |
1566 | if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) | |
1567 | return link_exists2_p (dir, dirlen, fname, pglob); | |
1568 | else | |
1569 | { | |
1570 | /* dfd cannot be -1 here, because dirfd never returns -1 on | |
1571 | glibc, or on hosts that have fstatat. */ | |
1572 | struct_stat64 st64; | |
1573 | return __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; | |
1574 | } | |
1575 | # else | |
1576 | return link_exists2_p (dir, dirlen, fname, pglob, flags); | |
1577 | # endif | |
1578 | } | |
1579 | #endif /* !defined GLOB_ONLY_P */ | |
1580 | ||
1581 | ||
6ec2e0f5 SDJ |
1582 | /* Like 'glob', but PATTERN is a final pathname component, |
1583 | and matches are searched for in DIRECTORY. | |
1584 | The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. | |
1585 | The GLOB_APPEND flag is assumed to be set (always appends). */ | |
1586 | static int | |
1587 | glob_in_dir (const char *pattern, const char *directory, int flags, | |
1588 | int (*errfunc) (const char *, int), | |
1589 | glob_t *pglob, size_t alloca_used) | |
1590 | { | |
1591 | size_t dirlen = strlen (directory); | |
1592 | void *stream = NULL; | |
5e8754f9 SDJ |
1593 | struct globnames |
1594 | { | |
1595 | struct globnames *next; | |
1596 | size_t count; | |
1597 | char *name[64]; | |
1598 | }; | |
1599 | struct globnames init_names; | |
1600 | struct globnames *names = &init_names; | |
1601 | struct globnames *names_alloca = &init_names; | |
6ec2e0f5 SDJ |
1602 | size_t nfound = 0; |
1603 | size_t cur = 0; | |
1604 | int meta; | |
1605 | int save; | |
1606 | int result; | |
1607 | ||
5e8754f9 | 1608 | alloca_used += sizeof (init_names); |
6ec2e0f5 | 1609 | |
5e8754f9 SDJ |
1610 | init_names.next = NULL; |
1611 | init_names.count = sizeof init_names.name / sizeof init_names.name[0]; | |
6ec2e0f5 SDJ |
1612 | |
1613 | meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE)); | |
5e8754f9 | 1614 | if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) |
6ec2e0f5 SDJ |
1615 | { |
1616 | /* We need not do any tests. The PATTERN contains no meta | |
1617 | characters and we must not return an error therefore the | |
1618 | result will always contain exactly one name. */ | |
1619 | flags |= GLOB_NOCHECK; | |
1620 | } | |
5e8754f9 | 1621 | else if (meta == 0) |
6ec2e0f5 | 1622 | { |
5e8754f9 SDJ |
1623 | /* Since we use the normal file functions we can also use stat() |
1624 | to verify the file is there. */ | |
6ec2e0f5 SDJ |
1625 | union |
1626 | { | |
1627 | struct stat st; | |
1628 | struct_stat64 st64; | |
1629 | } ust; | |
1630 | size_t patlen = strlen (pattern); | |
1631 | size_t fullsize; | |
1632 | bool alloca_fullname | |
1633 | = (! size_add_wrapv (dirlen + 1, patlen + 1, &fullsize) | |
1634 | && glob_use_alloca (alloca_used, fullsize)); | |
1635 | char *fullname; | |
1636 | if (alloca_fullname) | |
1637 | fullname = alloca_account (fullsize, alloca_used); | |
1638 | else | |
1639 | { | |
1640 | fullname = malloc (fullsize); | |
1641 | if (fullname == NULL) | |
1642 | return GLOB_NOSPACE; | |
1643 | } | |
1644 | ||
1645 | mempcpy (mempcpy (mempcpy (fullname, directory, dirlen), | |
1646 | "/", 1), | |
1647 | pattern, patlen + 1); | |
5e8754f9 SDJ |
1648 | if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) |
1649 | ? (*pglob->gl_stat) (fullname, &ust.st) | |
1650 | : __stat64 (fullname, &ust.st64)) == 0) | |
6ec2e0f5 SDJ |
1651 | /* We found this file to be existing. Now tell the rest |
1652 | of the function to copy this name into the result. */ | |
1653 | flags |= GLOB_NOCHECK; | |
1654 | ||
1655 | if (__glibc_unlikely (!alloca_fullname)) | |
1656 | free (fullname); | |
1657 | } | |
1658 | else | |
1659 | { | |
1660 | stream = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) | |
1661 | ? (*pglob->gl_opendir) (directory) | |
1662 | : opendir (directory)); | |
1663 | if (stream == NULL) | |
1664 | { | |
1665 | if (errno != ENOTDIR | |
1666 | && ((errfunc != NULL && (*errfunc) (directory, errno)) | |
1667 | || (flags & GLOB_ERR))) | |
1668 | return GLOB_ABORTED; | |
1669 | } | |
1670 | else | |
1671 | { | |
5e8754f9 SDJ |
1672 | int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) |
1673 | ? -1 : dirfd ((DIR *) stream)); | |
6ec2e0f5 | 1674 | int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
5e8754f9 SDJ |
1675 | | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) |
1676 | #if defined _AMIGA || defined VMS | |
1677 | | FNM_CASEFOLD | |
1678 | #endif | |
1679 | ); | |
6ec2e0f5 SDJ |
1680 | flags |= GLOB_MAGCHAR; |
1681 | ||
1682 | while (1) | |
1683 | { | |
1684 | struct readdir_result d; | |
1685 | { | |
1686 | if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) | |
1687 | d = convert_dirent (GL_READDIR (pglob, stream)); | |
1688 | else | |
1689 | { | |
1690 | #ifdef COMPILE_GLOB64 | |
1691 | d = convert_dirent (__readdir (stream)); | |
1692 | #else | |
1693 | d = convert_dirent64 (__readdir64 (stream)); | |
1694 | #endif | |
1695 | } | |
1696 | } | |
1697 | if (d.name == NULL) | |
1698 | break; | |
5e8754f9 SDJ |
1699 | if (d.skip_entry) |
1700 | continue; | |
6ec2e0f5 SDJ |
1701 | |
1702 | /* If we shall match only directories use the information | |
1703 | provided by the dirent call if possible. */ | |
5e8754f9 SDJ |
1704 | if ((flags & GLOB_ONLYDIR) && !readdir_result_might_be_dir (d)) |
1705 | continue; | |
6ec2e0f5 SDJ |
1706 | |
1707 | if (fnmatch (pattern, d.name, fnm_flags) == 0) | |
1708 | { | |
5e8754f9 SDJ |
1709 | /* If the file we found is a symlink we have to |
1710 | make sure the target file exists. */ | |
1711 | if (!readdir_result_might_be_symlink (d) | |
1712 | || link_exists_p (dfd, directory, dirlen, d.name, | |
1713 | pglob, flags)) | |
6ec2e0f5 | 1714 | { |
5e8754f9 SDJ |
1715 | if (cur == names->count) |
1716 | { | |
1717 | struct globnames *newnames; | |
1718 | size_t count = names->count * 2; | |
1719 | size_t nameoff = offsetof (struct globnames, name); | |
1720 | size_t size = FLEXSIZEOF (struct globnames, name, | |
1721 | count * sizeof (char *)); | |
1722 | if ((SIZE_MAX - nameoff) / 2 / sizeof (char *) | |
1723 | < names->count) | |
1724 | goto memory_error; | |
1725 | if (glob_use_alloca (alloca_used, size)) | |
1726 | newnames = names_alloca | |
1727 | = alloca_account (size, alloca_used); | |
1728 | else if ((newnames = malloc (size)) | |
1729 | == NULL) | |
1730 | goto memory_error; | |
1731 | newnames->count = count; | |
1732 | newnames->next = names; | |
1733 | names = newnames; | |
1734 | cur = 0; | |
1735 | } | |
1736 | names->name[cur] = strdup (d.name); | |
1737 | if (names->name[cur] == NULL) | |
6ec2e0f5 | 1738 | goto memory_error; |
5e8754f9 SDJ |
1739 | ++cur; |
1740 | ++nfound; | |
1741 | if (SIZE_MAX - pglob->gl_offs <= nfound) | |
6ec2e0f5 SDJ |
1742 | goto memory_error; |
1743 | } | |
1744 | } | |
1745 | } | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | if (nfound == 0 && (flags & GLOB_NOCHECK)) | |
1750 | { | |
1751 | size_t len = strlen (pattern); | |
1752 | nfound = 1; | |
1753 | names->name[cur] = malloc (len + 1); | |
1754 | if (names->name[cur] == NULL) | |
1755 | goto memory_error; | |
1756 | *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0'; | |
1757 | } | |
1758 | ||
1759 | result = GLOB_NOMATCH; | |
1760 | if (nfound != 0) | |
1761 | { | |
1762 | char **new_gl_pathv; | |
1763 | result = 0; | |
1764 | ||
1765 | if (SIZE_MAX / sizeof (char *) - pglob->gl_pathc | |
1766 | < pglob->gl_offs + nfound + 1) | |
1767 | goto memory_error; | |
1768 | ||
1769 | new_gl_pathv | |
1770 | = realloc (pglob->gl_pathv, | |
1771 | (pglob->gl_pathc + pglob->gl_offs + nfound + 1) | |
5e8754f9 | 1772 | * sizeof (char *)); |
6ec2e0f5 SDJ |
1773 | |
1774 | if (new_gl_pathv == NULL) | |
1775 | { | |
1776 | memory_error: | |
1777 | while (1) | |
1778 | { | |
1779 | struct globnames *old = names; | |
5e8754f9 SDJ |
1780 | size_t i; |
1781 | for (i = 0; i < cur; ++i) | |
6ec2e0f5 SDJ |
1782 | free (names->name[i]); |
1783 | names = names->next; | |
1784 | /* NB: we will not leak memory here if we exit without | |
1785 | freeing the current block assigned to OLD. At least | |
1786 | the very first block is always allocated on the stack | |
1787 | and this is the block assigned to OLD here. */ | |
1788 | if (names == NULL) | |
1789 | { | |
5e8754f9 | 1790 | assert (old == &init_names); |
6ec2e0f5 SDJ |
1791 | break; |
1792 | } | |
1793 | cur = names->count; | |
1794 | if (old == names_alloca) | |
1795 | names_alloca = names; | |
1796 | else | |
1797 | free (old); | |
1798 | } | |
1799 | result = GLOB_NOSPACE; | |
1800 | } | |
1801 | else | |
1802 | { | |
1803 | while (1) | |
1804 | { | |
1805 | struct globnames *old = names; | |
5e8754f9 SDJ |
1806 | size_t i; |
1807 | for (i = 0; i < cur; ++i) | |
6ec2e0f5 SDJ |
1808 | new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++] |
1809 | = names->name[i]; | |
1810 | names = names->next; | |
1811 | /* NB: we will not leak memory here if we exit without | |
1812 | freeing the current block assigned to OLD. At least | |
1813 | the very first block is always allocated on the stack | |
1814 | and this is the block assigned to OLD here. */ | |
1815 | if (names == NULL) | |
1816 | { | |
5e8754f9 | 1817 | assert (old == &init_names); |
6ec2e0f5 SDJ |
1818 | break; |
1819 | } | |
1820 | cur = names->count; | |
1821 | if (old == names_alloca) | |
1822 | names_alloca = names; | |
1823 | else | |
1824 | free (old); | |
1825 | } | |
1826 | ||
1827 | pglob->gl_pathv = new_gl_pathv; | |
1828 | ||
1829 | pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
1830 | ||
1831 | pglob->gl_flags = flags; | |
1832 | } | |
1833 | } | |
1834 | ||
1835 | if (stream != NULL) | |
1836 | { | |
1837 | save = errno; | |
1838 | if (__glibc_unlikely (flags & GLOB_ALTDIRFUNC)) | |
1839 | (*pglob->gl_closedir) (stream); | |
1840 | else | |
1841 | closedir (stream); | |
1842 | __set_errno (save); | |
1843 | } | |
1844 | ||
1845 | return result; | |
1846 | } |