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