Debian Codegraph @

lib/glob.c

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