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