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