glob.c revision 1.1.1.1.26.1 1 /* Copyright (C) 1991-2002,2003,2004,2005 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: glob.c,v 1.1.1.1.26.1 2017/05/13 06:23:23 snj Exp $");
20
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <glob.h>
27
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stddef.h>
32
33 /* Outcomment the following line for production quality code. */
34 /* #define NDEBUG 1 */
35 #include <assert.h>
36
37 #include <stdio.h> /* Needed on stupid SunOS for assert. */
38
39 #if !defined _LIBC || !defined GLOB_ONLY_P
40 #if defined HAVE_UNISTD_H || defined _LIBC
41 # include <unistd.h>
42 # ifndef POSIX
43 # ifdef _POSIX_VERSION
44 # define POSIX
45 # endif
46 # endif
47 #endif
48
49 #include <pwd.h>
50
51 #include <errno.h>
52 #ifndef __set_errno
53 # define __set_errno(val) errno = (val)
54 #endif
55
56 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
57 # include <dirent.h>
58 # define NAMLEN(dirent) strlen((dirent)->d_name)
59 #else
60 # define dirent direct
61 # define NAMLEN(dirent) (dirent)->d_namlen
62 # ifdef HAVE_SYS_NDIR_H
63 # include <sys/ndir.h>
64 # endif
65 # ifdef HAVE_SYS_DIR_H
66 # include <sys/dir.h>
67 # endif
68 # ifdef HAVE_NDIR_H
69 # include <ndir.h>
70 # endif
71 # ifdef HAVE_VMSDIR_H
72 # include "vmsdir.h"
73 # endif /* HAVE_VMSDIR_H */
74 #endif
75
76
77 /* In GNU systems, <dirent.h> defines this macro for us. */
78 #ifdef _D_NAMLEN
79 # undef NAMLEN
80 # define NAMLEN(d) _D_NAMLEN(d)
81 #endif
82
83 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
84 if the `d_type' member for `struct dirent' is available.
85 HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
86 #if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
87 /* True if the directory entry D must be of type T. */
88 # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
89
90 /* True if the directory entry D might be a symbolic link. */
91 # define DIRENT_MIGHT_BE_SYMLINK(d) \
92 ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
93
94 /* True if the directory entry D might be a directory. */
95 # define DIRENT_MIGHT_BE_DIR(d) \
96 ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
97
98 #else /* !HAVE_D_TYPE */
99 # define DIRENT_MUST_BE(d, t) false
100 # define DIRENT_MIGHT_BE_SYMLINK(d) true
101 # define DIRENT_MIGHT_BE_DIR(d) true
102 #endif /* HAVE_D_TYPE */
103
104 /* If the system has the `struct dirent64' type we use it internally. */
105 #if defined _LIBC && !defined COMPILE_GLOB64
106 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
107 # define CONVERT_D_NAMLEN(d64, d32)
108 # else
109 # define CONVERT_D_NAMLEN(d64, d32) \
110 (d64)->d_namlen = (d32)->d_namlen;
111 # endif
112
113 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
114 # define CONVERT_D_INO(d64, d32)
115 # else
116 # define CONVERT_D_INO(d64, d32) \
117 (d64)->d_ino = (d32)->d_ino;
118 # endif
119
120 # ifdef _DIRENT_HAVE_D_TYPE
121 # define CONVERT_D_TYPE(d64, d32) \
122 (d64)->d_type = (d32)->d_type;
123 # else
124 # define CONVERT_D_TYPE(d64, d32)
125 # endif
126
127 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
128 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
129 CONVERT_D_NAMLEN (d64, d32) \
130 CONVERT_D_INO (d64, d32) \
131 CONVERT_D_TYPE (d64, d32)
132 #endif
133
134
135 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
136 /* Posix does not require that the d_ino field be present, and some
137 systems do not provide it. */
138 # define REAL_DIR_ENTRY(dp) 1
139 #else
140 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
141 #endif /* POSIX */
142
143 #include <stdlib.h>
144 #include <string.h>
145
146 /* NAME_MAX is usually defined in <dirent.h> or <limits.h>. */
147 #include <limits.h>
148 #ifndef NAME_MAX
149 # define NAME_MAX (sizeof (((struct dirent *) 0)->d_name))
150 #endif
151
152 #include <alloca.h>
153
154 #ifdef _LIBC
155 # undef strdup
156 # define strdup(str) __strdup (str)
157 # define sysconf(id) __sysconf (id)
158 # define closedir(dir) __closedir (dir)
159 # define opendir(name) __opendir (name)
160 # define readdir(str) __readdir64 (str)
161 # define getpwnam_r(name, bufp, buf, len, res) \
162 __getpwnam_r (name, bufp, buf, len, res)
163 # ifndef __stat64
164 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
165 # endif
166 # define struct_stat64 struct stat64
167 #else /* !_LIBC */
168 # include "getlogin_r.h"
169 # include "mempcpy.h"
170 # include "stat-macros.h"
171 # include "strdup.h"
172 # define __stat64(fname, buf) stat (fname, buf)
173 # define struct_stat64 struct stat
174 # define __stat(fname, buf) stat (fname, buf)
175 # define __alloca alloca
176 # define __readdir readdir
177 # define __readdir64 readdir64
178 # define __glob_pattern_p glob_pattern_p
179 #endif /* _LIBC */
180
181 #include <stdbool.h>
182 #include <fnmatch.h>
183
184 #ifdef _SC_GETPW_R_SIZE_MAX
185 # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
186 #else
187 # define GETPW_R_SIZE_MAX() (-1)
188 #endif
189 #ifdef _SC_LOGIN_NAME_MAX
190 # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
191 #else
192 # define GET_LOGIN_NAME_MAX() (-1)
193 #endif
194
195 static const char *next_brace_sub (const char *begin, int flags) __THROW;
197
198 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
199
200 static int glob_in_dir (const char *pattern, const char *directory,
201 int flags, int (*errfunc) (const char *, int),
202 glob_t *pglob);
203
204 #if !defined _LIBC || !defined GLOB_ONLY_P
205 static int prefix_array (const char *prefix, char **array, size_t n) __THROW;
206 static int collated_compare (const void *, const void *) __THROW;
207
208
209 /* Find the end of the sub-pattern in a brace expression. */
210 static const char *
211 next_brace_sub (const char *cp, int flags)
212 {
213 unsigned int depth = 0;
214 while (*cp != '\0')
215 if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
216 {
217 if (*++cp == '\0')
218 break;
219 ++cp;
220 }
221 else
222 {
223 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
224 break;
225
226 if (*cp++ == '{')
227 depth++;
228 }
229
230 return *cp != '\0' ? cp : NULL;
231 }
232
233 #endif /* !defined _LIBC || !defined GLOB_ONLY_P */
234
235 /* Do glob searching for PATTERN, placing results in PGLOB.
236 The bits defined above may be set in FLAGS.
237 If a directory cannot be opened or read and ERRFUNC is not nil,
238 it is called with the pathname that caused the error, and the
239 `errno' value from the failing call; if it returns non-zero
240 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
241 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
242 Otherwise, `glob' returns zero. */
243 int
244 #ifdef GLOB_ATTRIBUTE
245 GLOB_ATTRIBUTE
246 #endif
247 glob (pattern, flags, errfunc, pglob)
248 const char *pattern;
249 int flags;
250 int (*errfunc) (const char *, int);
251 glob_t *pglob;
252 {
253 const char *filename;
254 const char *dirname;
255 size_t dirlen;
256 int status;
257 size_t oldcount;
258
259 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
260 {
261 __set_errno (EINVAL);
262 return -1;
263 }
264
265 if (!(flags & GLOB_DOOFFS))
266 /* Have to do this so `globfree' knows where to start freeing. It
267 also makes all the code that uses gl_offs simpler. */
268 pglob->gl_offs = 0;
269
270 if (flags & GLOB_BRACE)
271 {
272 const char *begin;
273
274 if (flags & GLOB_NOESCAPE)
275 begin = strchr (pattern, '{');
276 else
277 {
278 begin = pattern;
279 while (1)
280 {
281 if (*begin == '\0')
282 {
283 begin = NULL;
284 break;
285 }
286
287 if (*begin == '\\' && begin[1] != '\0')
288 ++begin;
289 else if (*begin == '{')
290 break;
291
292 ++begin;
293 }
294 }
295
296 if (begin != NULL)
297 {
298 /* Allocate working buffer large enough for our work. Note that
299 we have at least an opening and closing brace. */
300 size_t firstc;
301 char *alt_start;
302 const char *p;
303 const char *next;
304 const char *rest;
305 size_t rest_len;
306 #ifdef __GNUC__
307 char onealt[strlen (pattern) - 1];
308 #else
309 char *onealt = malloc (strlen (pattern) - 1);
310 if (onealt == NULL)
311 {
312 if (!(flags & GLOB_APPEND))
313 {
314 pglob->gl_pathc = 0;
315 pglob->gl_pathv = NULL;
316 }
317 return GLOB_NOSPACE;
318 }
319 #endif
320
321 /* We know the prefix for all sub-patterns. */
322 alt_start = mempcpy (onealt, pattern, begin - pattern);
323
324 /* Find the first sub-pattern and at the same time find the
325 rest after the closing brace. */
326 next = next_brace_sub (begin + 1, flags);
327 if (next == NULL)
328 {
329 /* It is an illegal expression. */
330 #ifndef __GNUC__
331 free (onealt);
332 #endif
333 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
334 }
335
336 /* Now find the end of the whole brace expression. */
337 rest = next;
338 while (*rest != '}')
339 {
340 rest = next_brace_sub (rest + 1, flags);
341 if (rest == NULL)
342 {
343 /* It is an illegal expression. */
344 #ifndef __GNUC__
345 free (onealt);
346 #endif
347 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
348 }
349 }
350 /* Please note that we now can be sure the brace expression
351 is well-formed. */
352 rest_len = strlen (++rest) + 1;
353
354 /* We have a brace expression. BEGIN points to the opening {,
355 NEXT points past the terminator of the first element, and END
356 points past the final }. We will accumulate result names from
357 recursive runs for each brace alternative in the buffer using
358 GLOB_APPEND. */
359
360 if (!(flags & GLOB_APPEND))
361 {
362 /* This call is to set a new vector, so clear out the
363 vector so we can append to it. */
364 pglob->gl_pathc = 0;
365 pglob->gl_pathv = NULL;
366 }
367 firstc = pglob->gl_pathc;
368
369 p = begin + 1;
370 while (1)
371 {
372 int result;
373
374 /* Construct the new glob expression. */
375 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
376
377 result = glob (onealt,
378 ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
379 | GLOB_APPEND), errfunc, pglob);
380
381 /* If we got an error, return it. */
382 if (result && result != GLOB_NOMATCH)
383 {
384 #ifndef __GNUC__
385 free (onealt);
386 #endif
387 if (!(flags & GLOB_APPEND))
388 {
389 globfree (pglob);
390 pglob->gl_pathc = 0;
391 }
392 return result;
393 }
394
395 if (*next == '}')
396 /* We saw the last entry. */
397 break;
398
399 p = next + 1;
400 next = next_brace_sub (p, flags);
401 assert (next != NULL);
402 }
403
404 #ifndef __GNUC__
405 free (onealt);
406 #endif
407
408 if (pglob->gl_pathc != firstc)
409 /* We found some entries. */
410 return 0;
411 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
412 return GLOB_NOMATCH;
413 }
414 }
415
416 /* Find the filename. */
417 filename = strrchr (pattern, '/');
418 #if defined __MSDOS__ || defined WINDOWS32
419 /* The case of "d:pattern". Since `:' is not allowed in
420 file names, we can safely assume that wherever it
421 happens in pattern, it signals the filename part. This
422 is so we could some day support patterns like "[a-z]:foo". */
423 if (filename == NULL)
424 filename = strchr (pattern, ':');
425 #endif /* __MSDOS__ || WINDOWS32 */
426 if (filename == NULL)
427 {
428 /* This can mean two things: a simple name or "~name". The latter
429 case is nothing but a notation for a directory. */
430 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
431 {
432 dirname = pattern;
433 dirlen = strlen (pattern);
434
435 /* Set FILENAME to NULL as a special flag. This is ugly but
436 other solutions would require much more code. We test for
437 this special case below. */
438 filename = NULL;
439 }
440 else
441 {
442 filename = pattern;
443 #ifdef _AMIGA
444 dirname = "";
445 #else
446 dirname = ".";
447 #endif
448 dirlen = 0;
449 }
450 }
451 else if (filename == pattern)
452 {
453 /* "/pattern". */
454 dirname = "/";
455 dirlen = 1;
456 ++filename;
457 }
458 else
459 {
460 char *newp;
461 dirlen = filename - pattern;
462 #if defined __MSDOS__ || defined WINDOWS32
463 if (*filename == ':'
464 || (filename > pattern + 1 && filename[-1] == ':'))
465 {
466 char *drive_spec;
467
468 ++dirlen;
469 drive_spec = __alloca (dirlen + 1);
470 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
471 /* For now, disallow wildcards in the drive spec, to
472 prevent infinite recursion in glob. */
473 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
474 return GLOB_NOMATCH;
475 /* If this is "d:pattern", we need to copy `:' to DIRNAME
476 as well. If it's "d:/pattern", don't remove the slash
477 from "d:/", since "d:" and "d:/" are not the same.*/
478 }
479 #endif
480 newp = __alloca (dirlen + 1);
481 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
482 dirname = newp;
483 ++filename;
484
485 if (filename[0] == '\0'
486 #if defined __MSDOS__ || defined WINDOWS32
487 && dirname[dirlen - 1] != ':'
488 && (dirlen < 3 || dirname[dirlen - 2] != ':'
489 || dirname[dirlen - 1] != '/')
490 #endif
491 && dirlen > 1)
492 /* "pattern/". Expand "pattern", appending slashes. */
493 {
494 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
495 if (val == 0)
496 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
497 | (flags & GLOB_MARK));
498 return val;
499 }
500 }
501
502 if (!(flags & GLOB_APPEND))
503 {
504 pglob->gl_pathc = 0;
505 if (!(flags & GLOB_DOOFFS))
506 pglob->gl_pathv = NULL;
507 else
508 {
509 size_t i;
510 pglob->gl_pathv = malloc ((pglob->gl_offs + 1) * sizeof (char *));
511 if (pglob->gl_pathv == NULL)
512 return GLOB_NOSPACE;
513
514 for (i = 0; i <= pglob->gl_offs; ++i)
515 pglob->gl_pathv[i] = NULL;
516 }
517 }
518
519 oldcount = pglob->gl_pathc + pglob->gl_offs;
520
521 #ifndef VMS
522 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
523 {
524 if (dirname[1] == '\0' || dirname[1] == '/')
525 {
526 /* Look up home directory. */
527 const char *home_dir = getenv ("HOME");
528 # ifdef _AMIGA
529 if (home_dir == NULL || home_dir[0] == '\0')
530 home_dir = "SYS:";
531 # else
532 # ifdef WINDOWS32
533 if (home_dir == NULL || home_dir[0] == '\0')
534 home_dir = "c:/users/default"; /* poor default */
535 # else
536 if (home_dir == NULL || home_dir[0] == '\0')
537 {
538 int success;
539 char *name;
540 size_t buflen = GET_LOGIN_NAME_MAX () + 1;
541
542 if (buflen == 0)
543 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
544 a moderate value. */
545 buflen = 20;
546 name = __alloca (buflen);
547
548 success = getlogin_r (name, buflen) == 0;
549 if (success)
550 {
551 struct passwd *p;
552 # if defined HAVE_GETPWNAM_R || defined _LIBC
553 long int pwbuflen = GETPW_R_SIZE_MAX ();
554 char *pwtmpbuf;
555 struct passwd pwbuf;
556 int save = errno;
557
558 # ifndef _LIBC
559 if (pwbuflen == -1)
560 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
561 Try a moderate value. */
562 pwbuflen = 1024;
563 # endif
564 pwtmpbuf = __alloca (pwbuflen);
565
566 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
567 != 0)
568 {
569 if (errno != ERANGE)
570 {
571 p = NULL;
572 break;
573 }
574 # ifdef _LIBC
575 pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
576 2 * pwbuflen);
577 # else
578 pwbuflen *= 2;
579 pwtmpbuf = __alloca (pwbuflen);
580 # endif
581 __set_errno (save);
582 }
583 # else
584 p = getpwnam (name);
585 # endif
586 if (p != NULL)
587 home_dir = p->pw_dir;
588 }
589 }
590 if (home_dir == NULL || home_dir[0] == '\0')
591 {
592 if (flags & GLOB_TILDE_CHECK)
593 return GLOB_NOMATCH;
594 else
595 home_dir = "~"; /* No luck. */
596 }
597 # endif /* WINDOWS32 */
598 # endif
599 /* Now construct the full directory. */
600 if (dirname[1] == '\0')
601 dirname = home_dir;
602 else
603 {
604 char *newp;
605 size_t home_len = strlen (home_dir);
606 newp = __alloca (home_len + dirlen);
607 mempcpy (mempcpy (newp, home_dir, home_len),
608 &dirname[1], dirlen);
609 dirname = newp;
610 }
611 }
612 # if !defined _AMIGA && !defined WINDOWS32
613 else
614 {
615 char *end_name = strchr (dirname, '/');
616 const char *user_name;
617 const char *home_dir;
618
619 if (end_name == NULL)
620 user_name = dirname + 1;
621 else
622 {
623 char *newp;
624 newp = __alloca (end_name - dirname);
625 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
626 = '\0';
627 user_name = newp;
628 }
629
630 /* Look up specific user's home directory. */
631 {
632 struct passwd *p;
633 # if defined HAVE_GETPWNAM_R || defined _LIBC
634 long int buflen = GETPW_R_SIZE_MAX ();
635 char *pwtmpbuf;
636 struct passwd pwbuf;
637 int save = errno;
638
639 # ifndef _LIBC
640 if (buflen == -1)
641 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
642 moderate value. */
643 buflen = 1024;
644 # endif
645 pwtmpbuf = __alloca (buflen);
646
647 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
648 {
649 if (errno != ERANGE)
650 {
651 p = NULL;
652 break;
653 }
654 # ifdef _LIBC
655 pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
656 # else
657 buflen *= 2;
658 pwtmpbuf = __alloca (buflen);
659 # endif
660 __set_errno (save);
661 }
662 # else
663 p = getpwnam (user_name);
664 # endif
665 if (p != NULL)
666 home_dir = p->pw_dir;
667 else
668 home_dir = NULL;
669 }
670 /* If we found a home directory use this. */
671 if (home_dir != NULL)
672 {
673 char *newp;
674 size_t home_len = strlen (home_dir);
675 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
676 newp = __alloca (home_len + rest_len + 1);
677 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
678 end_name, rest_len)) = '\0';
679 dirname = newp;
680 }
681 else
682 if (flags & GLOB_TILDE_CHECK)
683 /* We have to regard it as an error if we cannot find the
684 home directory. */
685 return GLOB_NOMATCH;
686 }
687 # endif /* Not Amiga && not WINDOWS32. */
688 }
689 #endif /* Not VMS. */
690
691 /* Now test whether we looked for "~" or "~NAME". In this case we
692 can give the answer now. */
693 if (filename == NULL)
694 {
695 struct stat st;
696 struct_stat64 st64;
697
698 /* Return the directory if we don't check for error or if it exists. */
699 if ((flags & GLOB_NOCHECK)
700 || (((flags & GLOB_ALTDIRFUNC)
701 ? ((*pglob->gl_stat) (dirname, &st) == 0
702 && S_ISDIR (st.st_mode))
703 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
704 {
705 int newcount = pglob->gl_pathc + pglob->gl_offs;
706 char **new_gl_pathv;
707
708 new_gl_pathv
709 = realloc (pglob->gl_pathv, (newcount + 1 + 1) * sizeof (char *));
710 if (new_gl_pathv == NULL)
711 {
712 nospace:
713 free (pglob->gl_pathv);
714 pglob->gl_pathv = NULL;
715 pglob->gl_pathc = 0;
716 return GLOB_NOSPACE;
717 }
718 pglob->gl_pathv = new_gl_pathv;
719
720 pglob->gl_pathv[newcount] = strdup (dirname);
721 if (pglob->gl_pathv[newcount] == NULL)
722 goto nospace;
723 pglob->gl_pathv[++newcount] = NULL;
724 ++pglob->gl_pathc;
725 pglob->gl_flags = flags;
726
727 return 0;
728 }
729
730 /* Not found. */
731 return GLOB_NOMATCH;
732 }
733
734 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
735 {
736 /* The directory name contains metacharacters, so we
737 have to glob for the directory, and then glob for
738 the pattern in each directory found. */
739 glob_t dirs;
740 size_t i;
741
742 if ((flags & GLOB_ALTDIRFUNC) != 0)
743 {
744 /* Use the alternative access functions also in the recursive
745 call. */
746 dirs.gl_opendir = pglob->gl_opendir;
747 dirs.gl_readdir = pglob->gl_readdir;
748 dirs.gl_closedir = pglob->gl_closedir;
749 dirs.gl_stat = pglob->gl_stat;
750 dirs.gl_lstat = pglob->gl_lstat;
751 }
752
753 status = glob (dirname,
754 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
755 | GLOB_ALTDIRFUNC))
756 | GLOB_NOSORT | GLOB_ONLYDIR),
757 errfunc, &dirs);
758 if (status != 0)
759 return status;
760
761 /* We have successfully globbed the preceding directory name.
762 For each name we found, call glob_in_dir on it and FILENAME,
763 appending the results to PGLOB. */
764 for (i = 0; i < dirs.gl_pathc; ++i)
765 {
766 int old_pathc;
767
768 #ifdef SHELL
769 {
770 /* Make globbing interruptible in the bash shell. */
771 extern int interrupt_state;
772
773 if (interrupt_state)
774 {
775 globfree (&dirs);
776 return GLOB_ABORTED;
777 }
778 }
779 #endif /* SHELL. */
780
781 old_pathc = pglob->gl_pathc;
782 status = glob_in_dir (filename, dirs.gl_pathv[i],
783 ((flags | GLOB_APPEND)
784 & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
785 errfunc, pglob);
786 if (status == GLOB_NOMATCH)
787 /* No matches in this directory. Try the next. */
788 continue;
789
790 if (status != 0)
791 {
792 globfree (&dirs);
793 globfree (pglob);
794 pglob->gl_pathc = 0;
795 return status;
796 }
797
798 /* Stick the directory on the front of each name. */
799 if (prefix_array (dirs.gl_pathv[i],
800 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
801 pglob->gl_pathc - old_pathc))
802 {
803 globfree (&dirs);
804 globfree (pglob);
805 pglob->gl_pathc = 0;
806 return GLOB_NOSPACE;
807 }
808 }
809
810 flags |= GLOB_MAGCHAR;
811
812 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
813 But if we have not found any matching entry and the GLOB_NOCHECK
814 flag was set we must return the input pattern itself. */
815 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
816 {
817 /* No matches. */
818 if (flags & GLOB_NOCHECK)
819 {
820 int newcount = pglob->gl_pathc + pglob->gl_offs;
821 char **new_gl_pathv;
822
823 new_gl_pathv = realloc (pglob->gl_pathv,
824 (newcount + 2) * sizeof (char *));
825 if (new_gl_pathv == NULL)
826 {
827 globfree (&dirs);
828 return GLOB_NOSPACE;
829 }
830 pglob->gl_pathv = new_gl_pathv;
831
832 pglob->gl_pathv[newcount] = strdup (pattern);
833 if (pglob->gl_pathv[newcount] == NULL)
834 {
835 globfree (&dirs);
836 globfree (pglob);
837 pglob->gl_pathc = 0;
838 return GLOB_NOSPACE;
839 }
840
841 ++pglob->gl_pathc;
842 ++newcount;
843
844 pglob->gl_pathv[newcount] = NULL;
845 pglob->gl_flags = flags;
846 }
847 else
848 {
849 globfree (&dirs);
850 return GLOB_NOMATCH;
851 }
852 }
853
854 globfree (&dirs);
855 }
856 else
857 {
858 int old_pathc = pglob->gl_pathc;
859
860 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
861 if (status != 0)
862 return status;
863
864 if (dirlen > 0)
865 {
866 /* Stick the directory on the front of each name. */
867 if (prefix_array (dirname,
868 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
869 pglob->gl_pathc - old_pathc))
870 {
871 globfree (pglob);
872 pglob->gl_pathc = 0;
873 return GLOB_NOSPACE;
874 }
875 }
876 }
877
878 if (!(flags & GLOB_NOSORT))
879 {
880 /* Sort the vector. */
881 qsort (&pglob->gl_pathv[oldcount],
882 pglob->gl_pathc + pglob->gl_offs - oldcount,
883 sizeof (char *), collated_compare);
884 }
885
886 return 0;
887 }
888 #if defined _LIBC && !defined glob
889 libc_hidden_def (glob)
890 #endif
891
892
893 #if !defined _LIBC || !defined GLOB_ONLY_P
894
895 /* Free storage allocated in PGLOB by a previous `glob' call. */
896 void
897 globfree (pglob)
898 register glob_t *pglob;
899 {
900 if (pglob->gl_pathv != NULL)
901 {
902 size_t i;
903 for (i = 0; i < pglob->gl_pathc; ++i)
904 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
905 free (pglob->gl_pathv[pglob->gl_offs + i]);
906 free (pglob->gl_pathv);
907 pglob->gl_pathv = NULL;
908 }
909 }
910 #if defined _LIBC && !defined globfree
911 libc_hidden_def (globfree)
912 #endif
913
914
915 /* Do a collated comparison of A and B. */
916 static int
917 collated_compare (const void *a, const void *b)
918 {
919 const char *const s1 = *(const char *const * const) a;
920 const char *const s2 = *(const char *const * const) b;
921
922 if (s1 == s2)
923 return 0;
924 if (s1 == NULL)
925 return 1;
926 if (s2 == NULL)
927 return -1;
928 return strcoll (s1, s2);
929 }
930
931
932 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
933 elements in place. Return nonzero if out of memory, zero if successful.
934 A slash is inserted between DIRNAME and each elt of ARRAY,
935 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
936 static int
937 prefix_array (const char *dirname, char **array, size_t n)
938 {
939 register size_t i;
940 size_t dirlen = strlen (dirname);
941 #if defined __MSDOS__ || defined WINDOWS32
942 int sep_char = '/';
943 # define DIRSEP_CHAR sep_char
944 #else
945 # define DIRSEP_CHAR '/'
946 #endif
947
948 if (dirlen == 1 && dirname[0] == '/')
949 /* DIRNAME is just "/", so normal prepending would get us "//foo".
950 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
951 dirlen = 0;
952 #if defined __MSDOS__ || defined WINDOWS32
953 else if (dirlen > 1)
954 {
955 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
956 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
957 --dirlen;
958 else if (dirname[dirlen - 1] == ':')
959 {
960 /* DIRNAME is "d:". Use `:' instead of `/'. */
961 --dirlen;
962 sep_char = ':';
963 }
964 }
965 #endif
966
967 for (i = 0; i < n; ++i)
968 {
969 size_t eltlen = strlen (array[i]) + 1;
970 char *new = malloc (dirlen + 1 + eltlen);
971 if (new == NULL)
972 {
973 while (i > 0)
974 free (array[--i]);
975 return 1;
976 }
977
978 {
979 char *endp = mempcpy (new, dirname, dirlen);
980 *endp++ = DIRSEP_CHAR;
981 mempcpy (endp, array[i], eltlen);
982 }
983 free (array[i]);
984 array[i] = new;
985 }
986
987 return 0;
988 }
989
990
991 /* We must not compile this function twice. */
992 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
993 /* Return nonzero if PATTERN contains any metacharacters.
994 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
995 int
996 __glob_pattern_p (pattern, quote)
997 const char *pattern;
998 int quote;
999 {
1000 register const char *p;
1001 int open = 0;
1002
1003 for (p = pattern; *p != '\0'; ++p)
1004 switch (*p)
1005 {
1006 case '?':
1007 case '*':
1008 return 1;
1009
1010 case '\\':
1011 if (quote && p[1] != '\0')
1012 ++p;
1013 break;
1014
1015 case '[':
1016 open = 1;
1017 break;
1018
1019 case ']':
1020 if (open)
1021 return 1;
1022 break;
1023 }
1024
1025 return 0;
1026 }
1027 # ifdef _LIBC
1028 weak_alias (__glob_pattern_p, glob_pattern_p)
1029 # endif
1030 #endif
1031
1032 #endif /* !GLOB_ONLY_P */
1033
1034
1035 /* We put this in a separate function mainly to allow the memory
1036 allocated with alloca to be recycled. */
1037 #if !defined _LIBC || !defined GLOB_ONLY_P
1038 static bool
1039 is_dir_p (const char *dir, size_t dirlen, const char *fname,
1040 glob_t *pglob, int flags)
1041 {
1042 size_t fnamelen = strlen (fname);
1043 char *fullname = __alloca (dirlen + 1 + fnamelen + 1);
1044 struct stat st;
1045 struct_stat64 st64;
1046
1047 mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
1048 fname, fnamelen + 1);
1049
1050 return ((flags & GLOB_ALTDIRFUNC)
1051 ? (*pglob->gl_stat) (fullname, &st) == 0 && S_ISDIR (st.st_mode)
1052 : __stat64 (fullname, &st64) == 0 && S_ISDIR (st64.st_mode));
1053 }
1054 #endif
1055
1056
1057 /* Like `glob', but PATTERN is a final pathname component,
1058 and matches are searched for in DIRECTORY.
1059 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1060 The GLOB_APPEND flag is assumed to be set (always appends). */
1061 static int
1062 glob_in_dir (const char *pattern, const char *directory, int flags,
1063 int (*errfunc) (const char *, int),
1064 glob_t *pglob)
1065 {
1066 size_t dirlen = strlen (directory);
1067 void *stream = NULL;
1068 struct globlink
1069 {
1070 struct globlink *next;
1071 char *name;
1072 };
1073 struct globlink *names = NULL;
1074 size_t nfound;
1075 int meta;
1076 int save;
1077
1078 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1079 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1080 {
1081 /* We need not do any tests. The PATTERN contains no meta
1082 characters and we must not return an error therefore the
1083 result will always contain exactly one name. */
1084 flags |= GLOB_NOCHECK;
1085 nfound = 0;
1086 }
1087 else if (meta == 0 &&
1088 ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
1089 {
1090 /* Since we use the normal file functions we can also use stat()
1091 to verify the file is there. */
1092 struct stat st;
1093 struct_stat64 st64;
1094 size_t patlen = strlen (pattern);
1095 char *fullname = __alloca (dirlen + 1 + patlen + 1);
1096
1097 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1098 "/", 1),
1099 pattern, patlen + 1);
1100 if (((flags & GLOB_ALTDIRFUNC)
1101 ? (*pglob->gl_stat) (fullname, &st)
1102 : __stat64 (fullname, &st64)) == 0)
1103 /* We found this file to be existing. Now tell the rest
1104 of the function to copy this name into the result. */
1105 flags |= GLOB_NOCHECK;
1106
1107 nfound = 0;
1108 }
1109 else
1110 {
1111 if (pattern[0] == '\0')
1112 {
1113 /* This is a special case for matching directories like in
1114 "*a/". */
1115 names = __alloca (sizeof (struct globlink));
1116 names->name = malloc (1);
1117 if (names->name == NULL)
1118 goto memory_error;
1119 names->name[0] = '\0';
1120 names->next = NULL;
1121 nfound = 1;
1122 meta = 0;
1123 }
1124 else
1125 {
1126 stream = ((flags & GLOB_ALTDIRFUNC)
1127 ? (*pglob->gl_opendir) (directory)
1128 : opendir (directory));
1129 if (stream == NULL)
1130 {
1131 if (errno != ENOTDIR
1132 && ((errfunc != NULL && (*errfunc) (directory, errno))
1133 || (flags & GLOB_ERR)))
1134 return GLOB_ABORTED;
1135 nfound = 0;
1136 meta = 0;
1137 }
1138 else
1139 {
1140 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1141 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1142 #if defined _AMIGA || defined VMS
1143 | FNM_CASEFOLD
1144 #endif
1145 );
1146 nfound = 0;
1147 flags |= GLOB_MAGCHAR;
1148
1149 while (1)
1150 {
1151 const char *name;
1152 size_t len;
1153 #if defined _LIBC && !defined COMPILE_GLOB64
1154 struct dirent64 *d;
1155 union
1156 {
1157 struct dirent64 d64;
1158 char room [offsetof (struct dirent64, d_name[0])
1159 + NAME_MAX + 1];
1160 }
1161 d64buf;
1162
1163 if (flags & GLOB_ALTDIRFUNC)
1164 {
1165 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1166 if (d32 != NULL)
1167 {
1168 CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
1169 d = &d64buf.d64;
1170 }
1171 else
1172 d = NULL;
1173 }
1174 else
1175 d = __readdir64 (stream);
1176 #else
1177 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1178 ? ((*pglob->gl_readdir) (stream))
1179 : __readdir (stream));
1180 #endif
1181 if (d == NULL)
1182 break;
1183 if (! REAL_DIR_ENTRY (d))
1184 continue;
1185
1186 /* If we shall match only directories use the information
1187 provided by the dirent call if possible. */
1188 if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
1189 continue;
1190
1191 name = d->d_name;
1192
1193 if (fnmatch (pattern, name, fnm_flags) == 0)
1194 {
1195 /* ISDIR will often be incorrectly set to false
1196 when not in GLOB_ONLYDIR || GLOB_MARK mode, but we
1197 don't care. It won't be used and we save the
1198 expensive call to stat. */
1199 int need_dir_test =
1200 (GLOB_MARK | (DIRENT_MIGHT_BE_SYMLINK (d)
1201 ? GLOB_ONLYDIR : 0));
1202 bool isdir = (DIRENT_MUST_BE (d, DT_DIR)
1203 || ((flags & need_dir_test)
1204 && is_dir_p (directory, dirlen, name,
1205 pglob, flags)));
1206
1207 /* In GLOB_ONLYDIR mode, skip non-dirs. */
1208 if ((flags & GLOB_ONLYDIR) && !isdir)
1209 continue;
1210
1211 {
1212 struct globlink *new =
1213 __alloca (sizeof (struct globlink));
1214 char *p;
1215 len = NAMLEN (d);
1216 new->name =
1217 malloc (len + 1 + ((flags & GLOB_MARK) && isdir));
1218 if (new->name == NULL)
1219 goto memory_error;
1220 p = mempcpy (new->name, name, len);
1221 if ((flags & GLOB_MARK) && isdir)
1222 *p++ = '/';
1223 *p = '\0';
1224 new->next = names;
1225 names = new;
1226 ++nfound;
1227 }
1228 }
1229 }
1230 }
1231 }
1232 }
1233
1234 if (nfound == 0 && (flags & GLOB_NOCHECK))
1235 {
1236 size_t len = strlen (pattern);
1237 nfound = 1;
1238 names = __alloca (sizeof (struct globlink));
1239 names->next = NULL;
1240 names->name = malloc (len + 1);
1241 if (names->name == NULL)
1242 goto memory_error;
1243 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1244 }
1245
1246 if (nfound != 0)
1247 {
1248 char **new_gl_pathv;
1249
1250 new_gl_pathv
1251 = realloc (pglob->gl_pathv,
1252 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1253 * sizeof (char *));
1254 if (new_gl_pathv == NULL)
1255 goto memory_error;
1256 pglob->gl_pathv = new_gl_pathv;
1257
1258 for (; names != NULL; names = names->next)
1259 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1260 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1261
1262 pglob->gl_flags = flags;
1263 }
1264
1265 save = errno;
1266 if (stream != NULL)
1267 {
1268 if (flags & GLOB_ALTDIRFUNC)
1269 (*pglob->gl_closedir) (stream);
1270 else
1271 closedir (stream);
1272 }
1273 __set_errno (save);
1274
1275 return nfound == 0 ? GLOB_NOMATCH : 0;
1276
1277 memory_error:
1278 {
1279 int save = errno;
1280 if (flags & GLOB_ALTDIRFUNC)
1281 (*pglob->gl_closedir) (stream);
1282 else
1283 closedir (stream);
1284 __set_errno (save);
1285 }
1286 while (names != NULL)
1287 {
1288 if (names->name != NULL)
1289 free (names->name);
1290 names = names->next;
1291 }
1292 return GLOB_NOSPACE;
1293 }
1294