* sysdeps/unix/sysv/linux/m68k/sysdep.h (INLINE_SYSCALL): Don't
[glibc.git] / sysdeps / generic / glob.c
blob2fedf4a06882502cc233e2481501762958f11535
1 /* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 /* AIX requires this to be the first thing in the file. */
20 #if defined _AIX && !defined __GNUC__
21 #pragma alloca
22 #endif
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 /* Enable GNU extensions in glob.h. */
29 #ifndef _GNU_SOURCE
30 # define _GNU_SOURCE 1
31 #endif
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 /* Outcomment the following line for production quality code. */
38 /* #define NDEBUG 1 */
39 #include <assert.h>
41 #include <stdio.h> /* Needed on stupid SunOS for assert. */
44 /* Comment out all this code if we are using the GNU C Library, and are not
45 actually compiling the library itself. This code is part of the GNU C
46 Library, but also included in many other GNU distributions. Compiling
47 and linking in this code is a waste when using the GNU C library
48 (especially if it is a shared library). Rather than having every GNU
49 program understand `configure --with-gnu-libc' and omit the object files,
50 it is simpler to just do this in the source for each such file. */
52 #define GLOB_INTERFACE_VERSION 1
53 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
54 # include <gnu-versions.h>
55 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
56 # define ELIDE_CODE
57 # endif
58 #endif
60 #ifndef ELIDE_CODE
61 #if !defined _LIBC || !defined GLOB_ONLY_P
63 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
64 # include <stddef.h>
65 #endif
67 #if defined HAVE_UNISTD_H || defined _LIBC
68 # include <unistd.h>
69 # ifndef POSIX
70 # ifdef _POSIX_VERSION
71 # define POSIX
72 # endif
73 # endif
74 #endif
76 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
77 # include <pwd.h>
78 #endif
80 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
81 extern int errno;
82 #endif
83 #ifndef __set_errno
84 # define __set_errno(val) errno = (val)
85 #endif
87 #ifndef NULL
88 # define NULL 0
89 #endif
92 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
93 # include <dirent.h>
94 # define NAMLEN(dirent) strlen((dirent)->d_name)
95 #else
96 # define dirent direct
97 # define NAMLEN(dirent) (dirent)->d_namlen
98 # ifdef HAVE_SYS_NDIR_H
99 # include <sys/ndir.h>
100 # endif
101 # ifdef HAVE_SYS_DIR_H
102 # include <sys/dir.h>
103 # endif
104 # ifdef HAVE_NDIR_H
105 # include <ndir.h>
106 # endif
107 # ifdef HAVE_VMSDIR_H
108 # include "vmsdir.h"
109 # endif /* HAVE_VMSDIR_H */
110 #endif
113 /* In GNU systems, <dirent.h> defines this macro for us. */
114 #ifdef _D_NAMLEN
115 # undef NAMLEN
116 # define NAMLEN(d) _D_NAMLEN(d)
117 #endif
119 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
120 if the `d_type' member for `struct dirent' is available. */
121 #ifdef _DIRENT_HAVE_D_TYPE
122 # define HAVE_D_TYPE 1
123 #endif
125 #if _LIBC
126 # define HAVE_DIRENT64 1
127 #endif
129 /* If the system has the `struct dirent64' type we use it internally. */
130 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
131 # if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
132 # define CONVERT_D_NAMLEN(d64, d32)
133 # else
134 # define CONVERT_D_NAMLEN(d64, d32) \
135 (d64)->d_namlen = (d32)->d_namlen;
136 # endif
138 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
139 # define CONVERT_D_INO(d64, d32)
140 # else
141 # define CONVERT_D_INO(d64, d32) \
142 (d64)->d_ino = (d32)->d_ino;
143 # endif
145 # ifdef HAVE_D_TYPE
146 # define CONVERT_D_TYPE(d64, d32) \
147 (d64)->d_type = (d32)->d_type;
148 # else
149 # define CONVERT_D_TYPE(d64, d32)
150 # endif
152 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
153 memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
154 CONVERT_D_NAMLEN (d64, d32) \
155 CONVERT_D_INO (d64, d32) \
156 CONVERT_D_TYPE (d64, d32)
157 #endif
160 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
161 /* Posix does not require that the d_ino field be present, and some
162 systems do not provide it. */
163 # define REAL_DIR_ENTRY(dp) 1
164 #else
165 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
166 #endif /* POSIX */
168 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
169 # include <stdlib.h>
170 # include <string.h>
171 # define ANSI_STRING
172 #else /* No standard headers. */
174 extern char *getenv ();
176 # ifdef HAVE_STRING_H
177 # include <string.h>
178 # define ANSI_STRING
179 # else
180 # include <strings.h>
181 # endif
182 # ifdef HAVE_MEMORY_H
183 # include <memory.h>
184 # endif
186 extern char *malloc (), *realloc ();
187 extern void free ();
189 extern void qsort ();
190 extern void abort (), exit ();
192 #endif /* Standard headers. */
194 #ifndef ANSI_STRING
196 # ifndef bzero
197 extern void bzero ();
198 # endif
199 # ifndef bcopy
200 extern void bcopy ();
201 # endif
203 # define memcpy(d, s, n) bcopy ((s), (d), (n))
204 # define strrchr rindex
205 /* memset is only used for zero here, but let's be paranoid. */
206 # define memset(s, better_be_zero, n) \
207 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
208 #endif /* Not ANSI_STRING. */
210 #if !defined HAVE_STRCOLL && !defined _LIBC
211 # define strcoll strcmp
212 #endif
214 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
215 # define HAVE_MEMPCPY 1
216 # undef mempcpy
217 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
218 #endif
220 #ifndef __GNU_LIBRARY__
221 # ifdef __GNUC__
222 __inline
223 # endif
224 # ifndef __SASC
225 # ifdef WINDOWS32
226 static void *
227 # else
228 static char *
229 # endif
230 my_realloc (p, n)
231 char *p;
232 unsigned int n;
234 /* These casts are the for sake of the broken Ultrix compiler,
235 which warns of illegal pointer combinations otherwise. */
236 if (p == NULL)
237 return (char *) malloc (n);
238 return (char *) realloc (p, n);
240 # define realloc my_realloc
241 # endif /* __SASC */
242 #endif /* __GNU_LIBRARY__ */
245 #if !defined __alloca && !defined __GNU_LIBRARY__
247 # ifdef __GNUC__
248 # undef alloca
249 # define alloca(n) __builtin_alloca (n)
250 # else /* Not GCC. */
251 # ifdef HAVE_ALLOCA_H
252 # include <alloca.h>
253 # else /* Not HAVE_ALLOCA_H. */
254 # ifndef _AIX
255 # ifdef WINDOWS32
256 # include <malloc.h>
257 # else
258 extern char *alloca ();
259 # endif /* WINDOWS32 */
260 # endif /* Not _AIX. */
261 # endif /* sparc or HAVE_ALLOCA_H. */
262 # endif /* GCC. */
264 # define __alloca alloca
266 #endif
268 #ifndef __GNU_LIBRARY__
269 # define __stat stat
270 # ifdef STAT_MACROS_BROKEN
271 # undef S_ISDIR
272 # endif
273 # ifndef S_ISDIR
274 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
275 # endif
276 #endif
278 #ifdef _LIBC
279 # undef strdup
280 # define strdup(str) __strdup (str)
281 # define sysconf(id) __sysconf (id)
282 # define closedir(dir) __closedir (dir)
283 # define opendir(name) __opendir (name)
284 # define readdir(str) __readdir64 (str)
285 # define getpwnam_r(name, bufp, buf, len, res) \
286 __getpwnam_r (name, bufp, buf, len, res)
287 # ifndef __stat64
288 # define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf)
289 # endif
290 # define HAVE_STAT64 1
291 #endif
293 #ifndef HAVE_STAT64
294 # define __stat64(fname, buf) __stat (fname, buf)
295 /* This is the variable name we are using. */
296 # define st64 st
297 #endif
299 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
300 # undef size_t
301 # define size_t unsigned int
302 #endif
304 /* Some system header files erroneously define these.
305 We want our own definitions from <fnmatch.h> to take precedence. */
306 #ifndef __GNU_LIBRARY__
307 # undef FNM_PATHNAME
308 # undef FNM_NOESCAPE
309 # undef FNM_PERIOD
310 #endif
311 #include <fnmatch.h>
313 /* Some system header files erroneously define these.
314 We want our own definitions from <glob.h> to take precedence. */
315 #ifndef __GNU_LIBRARY__
316 # undef GLOB_ERR
317 # undef GLOB_MARK
318 # undef GLOB_NOSORT
319 # undef GLOB_DOOFFS
320 # undef GLOB_NOCHECK
321 # undef GLOB_APPEND
322 # undef GLOB_NOESCAPE
323 # undef GLOB_PERIOD
324 #endif
325 #include <glob.h>
327 #ifdef HAVE_GETLOGIN_R
328 extern int getlogin_r __P ((char *, size_t));
329 #else
330 extern char *getlogin __P ((void));
331 #endif
333 static
334 #if __GNUC__ - 0 >= 2
335 inline
336 #endif
337 const char *next_brace_sub __P ((const char *begin));
339 #endif /* GLOB_ONLY_P */
341 static int glob_in_dir __P ((const char *pattern, const char *directory,
342 int flags,
343 int (*errfunc) (const char *, int),
344 glob_t *pglob));
346 #if !defined _LIBC || !defined GLOB_ONLY_P
347 static int prefix_array __P ((const char *prefix, char **array, size_t n));
348 static int collated_compare __P ((const __ptr_t, const __ptr_t));
351 /* Find the end of the sub-pattern in a brace expression. We define
352 this as an inline function if the compiler permits. */
353 static
354 #if __GNUC__ - 0 >= 2
355 inline
356 #endif
357 const char *
358 next_brace_sub (begin)
359 const char *begin;
361 unsigned int depth = 0;
362 const char *cp = begin;
364 while (1)
366 if (depth == 0)
368 if (*cp != ',' && *cp != '}' && *cp != '\0')
370 if (*cp == '{')
371 ++depth;
372 ++cp;
373 continue;
376 else
378 while (*cp != '\0' && (*cp != '}' || depth > 0))
380 if (*cp == '}')
381 --depth;
382 ++cp;
384 if (*cp == '\0')
385 /* An incorrectly terminated brace expression. */
386 return NULL;
388 continue;
390 break;
393 return cp;
396 #endif /* !GLOB_ONLY_P */
398 /* Do glob searching for PATTERN, placing results in PGLOB.
399 The bits defined above may be set in FLAGS.
400 If a directory cannot be opened or read and ERRFUNC is not nil,
401 it is called with the pathname that caused the error, and the
402 `errno' value from the failing call; if it returns non-zero
403 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
404 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
405 Otherwise, `glob' returns zero. */
407 glob (pattern, flags, errfunc, pglob)
408 const char *pattern;
409 int flags;
410 int (*errfunc) __P ((const char *, int));
411 glob_t *pglob;
413 const char *filename;
414 const char *dirname;
415 size_t dirlen;
416 int status;
417 size_t oldcount;
419 if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
421 __set_errno (EINVAL);
422 return -1;
425 if (!(flags & GLOB_DOOFFS))
426 /* Have to do this so `globfree' knows where to start freeing. It
427 also makes all the code that uses gl_offs simpler. */
428 pglob->gl_offs = 0;
430 if (flags & GLOB_BRACE)
432 const char *begin = strchr (pattern, '{');
433 if (begin != NULL)
435 /* Allocate working buffer large enough for our work. Note that
436 we have at least an opening and closing brace. */
437 size_t firstc;
438 char *alt_start;
439 const char *p;
440 const char *next;
441 const char *rest;
442 size_t rest_len;
443 #ifdef __GNUC__
444 char onealt[strlen (pattern) - 1];
445 #else
446 char *onealt = (char *) malloc (strlen (pattern) - 1);
447 if (onealt == NULL)
449 if (!(flags & GLOB_APPEND))
450 globfree (pglob);
451 return GLOB_NOSPACE;
453 #endif
455 /* We know the prefix for all sub-patterns. */
456 #ifdef HAVE_MEMPCPY
457 alt_start = mempcpy (onealt, pattern, begin - pattern);
458 #else
459 memcpy (onealt, pattern, begin - pattern);
460 alt_start = &onealt[begin - pattern];
461 #endif
463 /* Find the first sub-pattern and at the same time find the
464 rest after the closing brace. */
465 next = next_brace_sub (begin + 1);
466 if (next == NULL)
468 /* It is an illegal expression. */
469 #ifndef __GNUC__
470 free (onealt);
471 #endif
472 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
475 /* Now find the end of the whole brace expression. */
476 rest = next;
477 while (*rest != '}')
479 rest = next_brace_sub (rest + 1);
480 if (rest == NULL)
482 /* It is an illegal expression. */
483 #ifndef __GNUC__
484 free (onealt);
485 #endif
486 return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
489 /* Please note that we now can be sure the brace expression
490 is well-formed. */
491 rest_len = strlen (++rest) + 1;
493 /* We have a brace expression. BEGIN points to the opening {,
494 NEXT points past the terminator of the first element, and END
495 points past the final }. We will accumulate result names from
496 recursive runs for each brace alternative in the buffer using
497 GLOB_APPEND. */
499 if (!(flags & GLOB_APPEND))
501 /* This call is to set a new vector, so clear out the
502 vector so we can append to it. */
503 pglob->gl_pathc = 0;
504 pglob->gl_pathv = NULL;
506 firstc = pglob->gl_pathc;
508 p = begin + 1;
509 while (1)
511 int result;
513 /* Construct the new glob expression. */
514 #ifdef HAVE_MEMPCPY
515 mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
516 #else
517 memcpy (alt_start, p, next - p);
518 memcpy (&alt_start[next - p], rest, rest_len);
519 #endif
521 result = glob (onealt,
522 ((flags & ~(GLOB_NOCHECK|GLOB_NOMAGIC))
523 | GLOB_APPEND), errfunc, pglob);
525 /* If we got an error, return it. */
526 if (result && result != GLOB_NOMATCH)
528 #ifndef __GNUC__
529 free (onealt);
530 #endif
531 if (!(flags & GLOB_APPEND))
532 globfree (pglob);
533 return result;
536 if (*next == '}')
537 /* We saw the last entry. */
538 break;
540 p = next + 1;
541 next = next_brace_sub (p);
542 assert (next != NULL);
545 #ifndef __GNUC__
546 free (onealt);
547 #endif
549 if (pglob->gl_pathc != firstc)
550 /* We found some entries. */
551 return 0;
552 else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
553 return GLOB_NOMATCH;
557 /* Find the filename. */
558 filename = strrchr (pattern, '/');
559 #if defined __MSDOS__ || defined WINDOWS32
560 /* The case of "d:pattern". Since `:' is not allowed in
561 file names, we can safely assume that wherever it
562 happens in pattern, it signals the filename part. This
563 is so we could some day support patterns like "[a-z]:foo". */
564 if (filename == NULL)
565 filename = strchr (pattern, ':');
566 #endif /* __MSDOS__ || WINDOWS32 */
567 if (filename == NULL)
569 /* This can mean two things: a simple name or "~name". The latter
570 case is nothing but a notation for a directory. */
571 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
573 dirname = pattern;
574 dirlen = strlen (pattern);
576 /* Set FILENAME to NULL as a special flag. This is ugly but
577 other solutions would require much more code. We test for
578 this special case below. */
579 filename = NULL;
581 else
583 filename = pattern;
584 #ifdef _AMIGA
585 dirname = "";
586 #else
587 dirname = ".";
588 #endif
589 dirlen = 0;
592 else if (filename == pattern)
594 /* "/pattern". */
595 dirname = "/";
596 dirlen = 1;
597 ++filename;
599 else
601 char *newp;
602 dirlen = filename - pattern;
603 #if defined __MSDOS__ || defined WINDOWS32
604 if (*filename == ':'
605 || (filename > pattern + 1 && filename[-1] == ':'))
607 char *drive_spec;
609 ++dirlen;
610 drive_spec = (char *) __alloca (dirlen + 1);
611 #ifdef HAVE_MEMPCPY
612 *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0';
613 #else
614 memcpy (drive_spec, pattern, dirlen);
615 drive_spec[dirlen] = '\0';
616 #endif
617 /* For now, disallow wildcards in the drive spec, to
618 prevent infinite recursion in glob. */
619 if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE)))
620 return GLOB_NOMATCH;
621 /* If this is "d:pattern", we need to copy `:' to DIRNAME
622 as well. If it's "d:/pattern", don't remove the slash
623 from "d:/", since "d:" and "d:/" are not the same.*/
625 #endif
626 newp = (char *) __alloca (dirlen + 1);
627 #ifdef HAVE_MEMPCPY
628 *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
629 #else
630 memcpy (newp, pattern, dirlen);
631 newp[dirlen] = '\0';
632 #endif
633 dirname = newp;
634 ++filename;
636 if (filename[0] == '\0'
637 #if defined __MSDOS__ || defined WINDOWS32
638 && dirname[dirlen - 1] != ':'
639 && (dirlen < 3 || dirname[dirlen - 2] != ':'
640 || dirname[dirlen - 1] != '/')
641 #endif
642 && dirlen > 1)
643 /* "pattern/". Expand "pattern", appending slashes. */
645 int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
646 if (val == 0)
647 pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
648 | (flags & GLOB_MARK));
649 return val;
653 if (!(flags & GLOB_APPEND))
655 pglob->gl_pathc = 0;
656 if (!(flags & GLOB_DOOFFS))
657 pglob->gl_pathv = NULL;
658 else
660 size_t i;
661 pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
662 * sizeof (char *));
663 if (pglob->gl_pathv == NULL)
664 return GLOB_NOSPACE;
666 for (i = 0; i <= pglob->gl_offs; ++i)
667 pglob->gl_pathv[i] = NULL;
671 oldcount = pglob->gl_pathc + pglob->gl_offs;
673 #ifndef VMS
674 if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
676 if (dirname[1] == '\0' || dirname[1] == '/')
678 /* Look up home directory. */
679 const char *home_dir = getenv ("HOME");
680 # ifdef _AMIGA
681 if (home_dir == NULL || home_dir[0] == '\0')
682 home_dir = "SYS:";
683 # else
684 # ifdef WINDOWS32
685 if (home_dir == NULL || home_dir[0] == '\0')
686 home_dir = "c:/users/default"; /* poor default */
687 # else
688 if (home_dir == NULL || home_dir[0] == '\0')
690 int success;
691 char *name;
692 # if defined HAVE_GETLOGIN_R || defined _LIBC
693 size_t buflen = sysconf (_SC_LOGIN_NAME_MAX) + 1;
695 if (buflen == 0)
696 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
697 a moderate value. */
698 buflen = 20;
699 name = (char *) __alloca (buflen);
701 success = getlogin_r (name, buflen) >= 0;
702 # else
703 success = (name = getlogin ()) != NULL;
704 # endif
705 if (success)
707 struct passwd *p;
708 # if defined HAVE_GETPWNAM_R || defined _LIBC
709 long int pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
710 char *pwtmpbuf;
711 struct passwd pwbuf;
712 int save = errno;
714 if (pwbuflen == -1)
715 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
716 Try a moderate value. */
717 pwbuflen = 1024;
718 pwtmpbuf = (char *) __alloca (pwbuflen);
720 while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
721 != 0)
723 if (errno != ERANGE)
725 p = NULL;
726 break;
728 pwbuflen *= 2;
729 pwtmpbuf = (char *) __alloca (pwbuflen);
730 __set_errno (save);
732 # else
733 p = getpwnam (name);
734 # endif
735 if (p != NULL)
736 home_dir = p->pw_dir;
739 if (home_dir == NULL || home_dir[0] == '\0')
741 if (flags & GLOB_TILDE_CHECK)
742 return GLOB_NOMATCH;
743 else
744 home_dir = "~"; /* No luck. */
746 # endif /* WINDOWS32 */
747 # endif
748 /* Now construct the full directory. */
749 if (dirname[1] == '\0')
750 dirname = home_dir;
751 else
753 char *newp;
754 size_t home_len = strlen (home_dir);
755 newp = (char *) __alloca (home_len + dirlen);
756 # ifdef HAVE_MEMPCPY
757 mempcpy (mempcpy (newp, home_dir, home_len),
758 &dirname[1], dirlen);
759 # else
760 memcpy (newp, home_dir, home_len);
761 memcpy (&newp[home_len], &dirname[1], dirlen);
762 # endif
763 dirname = newp;
766 # if !defined _AMIGA && !defined WINDOWS32
767 else
769 char *end_name = strchr (dirname, '/');
770 const char *user_name;
771 const char *home_dir;
773 if (end_name == NULL)
774 user_name = dirname + 1;
775 else
777 char *newp;
778 newp = (char *) __alloca (end_name - dirname);
779 # ifdef HAVE_MEMPCPY
780 *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
781 = '\0';
782 # else
783 memcpy (newp, dirname + 1, end_name - dirname);
784 newp[end_name - dirname - 1] = '\0';
785 # endif
786 user_name = newp;
789 /* Look up specific user's home directory. */
791 struct passwd *p;
792 # if defined HAVE_GETPWNAM_R || defined _LIBC
793 long int buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
794 char *pwtmpbuf;
795 struct passwd pwbuf;
796 int save = errno;
798 if (buflen == -1)
799 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
800 moderate value. */
801 buflen = 1024;
802 pwtmpbuf = (char *) __alloca (buflen);
804 while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
806 if (errno != ERANGE)
808 p = NULL;
809 break;
811 buflen *= 2;
812 pwtmpbuf = __alloca (buflen);
813 __set_errno (save);
815 # else
816 p = getpwnam (user_name);
817 # endif
818 if (p != NULL)
819 home_dir = p->pw_dir;
820 else
821 home_dir = NULL;
823 /* If we found a home directory use this. */
824 if (home_dir != NULL)
826 char *newp;
827 size_t home_len = strlen (home_dir);
828 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
829 newp = (char *) __alloca (home_len + rest_len + 1);
830 # ifdef HAVE_MEMPCPY
831 *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
832 end_name, rest_len)) = '\0';
833 # else
834 memcpy (newp, home_dir, home_len);
835 memcpy (&newp[home_len], end_name, rest_len);
836 newp[home_len + rest_len] = '\0';
837 # endif
838 dirname = newp;
840 else
841 if (flags & GLOB_TILDE_CHECK)
842 /* We have to regard it as an error if we cannot find the
843 home directory. */
844 return GLOB_NOMATCH;
846 # endif /* Not Amiga && not WINDOWS32. */
848 #endif /* Not VMS. */
850 /* Now test whether we looked for "~" or "~NAME". In this case we
851 can give the answer now. */
852 if (filename == NULL)
854 struct stat st;
855 #ifdef HAVE_STAT64
856 struct stat64 st64;
857 #endif
859 /* Return the directory if we don't check for error or if it exists. */
860 if ((flags & GLOB_NOCHECK)
861 || (((flags & GLOB_ALTDIRFUNC)
862 ? ((*pglob->gl_stat) (dirname, &st) == 0
863 && S_ISDIR (st.st_mode))
864 : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
866 int newcount = pglob->gl_pathc + pglob->gl_offs;
868 pglob->gl_pathv
869 = (char **) realloc (pglob->gl_pathv,
870 (newcount + 1 + 1) * sizeof (char *));
871 if (pglob->gl_pathv == NULL)
872 return GLOB_NOSPACE;
874 #if defined HAVE_STRDUP || defined _LIBC
875 pglob->gl_pathv[newcount] = strdup (dirname);
876 #else
878 size_t len = strlen (dirname) + 1;
879 char *dircopy = malloc (len);
880 if (dircopy != NULL)
881 pglob->gl_pathv[newcount] = memcpy (dircopy, dirname, len);
883 #endif
884 if (pglob->gl_pathv[newcount] == NULL)
886 free (pglob->gl_pathv);
887 return GLOB_NOSPACE;
889 pglob->gl_pathv[++newcount] = NULL;
890 ++pglob->gl_pathc;
891 pglob->gl_flags = flags;
893 return 0;
896 /* Not found. */
897 return GLOB_NOMATCH;
900 if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
902 /* The directory name contains metacharacters, so we
903 have to glob for the directory, and then glob for
904 the pattern in each directory found. */
905 glob_t dirs;
906 size_t i;
908 if ((flags & GLOB_ALTDIRFUNC) != 0)
910 /* Use the alternative access functions also in the recursive
911 call. */
912 dirs.gl_opendir = pglob->gl_opendir;
913 dirs.gl_readdir = pglob->gl_readdir;
914 dirs.gl_closedir = pglob->gl_closedir;
915 dirs.gl_stat = pglob->gl_stat;
916 dirs.gl_lstat = pglob->gl_lstat;
919 status = glob (dirname,
920 ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
921 | GLOB_ALTDIRFUNC))
922 | GLOB_NOSORT | GLOB_ONLYDIR),
923 errfunc, &dirs);
924 if (status != 0)
925 return status;
927 /* We have successfully globbed the preceding directory name.
928 For each name we found, call glob_in_dir on it and FILENAME,
929 appending the results to PGLOB. */
930 for (i = 0; i < dirs.gl_pathc; ++i)
932 int old_pathc;
934 #ifdef SHELL
936 /* Make globbing interruptible in the bash shell. */
937 extern int interrupt_state;
939 if (interrupt_state)
941 globfree (&dirs);
942 return GLOB_ABORTED;
945 #endif /* SHELL. */
947 old_pathc = pglob->gl_pathc;
948 status = glob_in_dir (filename, dirs.gl_pathv[i],
949 ((flags | GLOB_APPEND) & ~GLOB_NOCHECK),
950 errfunc, pglob);
951 if (status == GLOB_NOMATCH)
952 /* No matches in this directory. Try the next. */
953 continue;
955 if (status != 0)
957 globfree (&dirs);
958 globfree (pglob);
959 return status;
962 /* Stick the directory on the front of each name. */
963 if (prefix_array (dirs.gl_pathv[i],
964 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
965 pglob->gl_pathc - old_pathc))
967 globfree (&dirs);
968 globfree (pglob);
969 return GLOB_NOSPACE;
973 flags |= GLOB_MAGCHAR;
975 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
976 But if we have not found any matching entry and the GLOB_NOCHECK
977 flag was set we must return the list consisting of the disrectory
978 names followed by the filename. */
979 if (pglob->gl_pathc + pglob->gl_offs == oldcount)
981 /* No matches. */
982 if (flags & GLOB_NOCHECK)
984 size_t filename_len = strlen (filename) + 1;
985 char **new_pathv;
986 int newcount = pglob->gl_pathc + pglob->gl_offs;
987 struct stat st;
988 #ifdef HAVE_STAT64
989 struct stat64 st64;
990 #endif
992 /* This is an pessimistic guess about the size. */
993 pglob->gl_pathv
994 = (char **) realloc (pglob->gl_pathv,
995 (newcount + dirs.gl_pathc + 1)
996 * sizeof (char *));
997 if (pglob->gl_pathv == NULL)
999 globfree (&dirs);
1000 return GLOB_NOSPACE;
1003 for (i = 0; i < dirs.gl_pathc; ++i)
1005 const char *dir = dirs.gl_pathv[i];
1006 size_t dir_len = strlen (dir);
1008 /* First check whether this really is a directory. */
1009 if (((flags & GLOB_ALTDIRFUNC)
1010 ? ((*pglob->gl_stat) (dir, &st) != 0
1011 || !S_ISDIR (st.st_mode))
1012 : (__stat64 (dir, &st64) != 0
1013 || !S_ISDIR (st64.st_mode))))
1014 /* No directory, ignore this entry. */
1015 continue;
1017 pglob->gl_pathv[newcount] = malloc (dir_len + 1
1018 + filename_len);
1019 if (pglob->gl_pathv[newcount] == NULL)
1021 globfree (&dirs);
1022 globfree (pglob);
1023 return GLOB_NOSPACE;
1026 #ifdef HAVE_MEMPCPY
1027 mempcpy (mempcpy (mempcpy (pglob->gl_pathv[newcount],
1028 dir, dir_len),
1029 "/", 1),
1030 filename, filename_len);
1031 #else
1032 memcpy (pglob->gl_pathv[newcount], dir, dir_len);
1033 pglob->gl_pathv[newcount][dir_len] = '/';
1034 memcpy (&pglob->gl_pathv[newcount][dir_len + 1],
1035 filename, filename_len);
1036 #endif
1037 ++pglob->gl_pathc;
1038 ++newcount;
1041 pglob->gl_pathv[newcount] = NULL;
1042 pglob->gl_flags = flags;
1044 /* Now we know how large the gl_pathv vector must be. */
1045 new_pathv = (char **) realloc (pglob->gl_pathv,
1046 ((newcount + 1)
1047 * sizeof (char *)));
1048 if (new_pathv != NULL)
1049 pglob->gl_pathv = new_pathv;
1051 else
1053 globfree (&dirs);
1054 return GLOB_NOMATCH;
1058 globfree (&dirs);
1060 else
1062 int old_pathc = pglob->gl_pathc;
1064 status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
1065 if (status != 0)
1066 return status;
1068 if (dirlen > 0)
1070 /* Stick the directory on the front of each name. */
1071 if (prefix_array (dirname,
1072 &pglob->gl_pathv[old_pathc + pglob->gl_offs],
1073 pglob->gl_pathc - old_pathc))
1075 globfree (pglob);
1076 return GLOB_NOSPACE;
1081 if (flags & GLOB_MARK)
1083 /* Append slashes to directory names. */
1084 size_t i;
1085 struct stat st;
1086 #ifdef HAVE_STAT64
1087 struct stat64 st64;
1088 #endif
1090 for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
1091 if (((flags & GLOB_ALTDIRFUNC)
1092 ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
1093 && S_ISDIR (st.st_mode))
1094 : (__stat64 (pglob->gl_pathv[i], &st64) == 0
1095 && S_ISDIR (st64.st_mode))))
1097 size_t len = strlen (pglob->gl_pathv[i]) + 2;
1098 char *new = realloc (pglob->gl_pathv[i], len);
1099 if (new == NULL)
1101 globfree (pglob);
1102 return GLOB_NOSPACE;
1104 strcpy (&new[len - 2], "/");
1105 pglob->gl_pathv[i] = new;
1109 if (!(flags & GLOB_NOSORT))
1111 /* Sort the vector. */
1112 qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
1113 pglob->gl_pathc + pglob->gl_offs - oldcount,
1114 sizeof (char *), collated_compare);
1117 return 0;
1121 #if !defined _LIBC || !defined GLOB_ONLY_P
1123 /* Free storage allocated in PGLOB by a previous `glob' call. */
1124 void
1125 globfree (pglob)
1126 register glob_t *pglob;
1128 if (pglob->gl_pathv != NULL)
1130 size_t i;
1131 for (i = 0; i < pglob->gl_pathc; ++i)
1132 if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
1133 free ((__ptr_t) pglob->gl_pathv[pglob->gl_offs + i]);
1134 free ((__ptr_t) pglob->gl_pathv);
1139 /* Do a collated comparison of A and B. */
1140 static int
1141 collated_compare (a, b)
1142 const __ptr_t a;
1143 const __ptr_t b;
1145 const char *const s1 = *(const char *const * const) a;
1146 const char *const s2 = *(const char *const * const) b;
1148 if (s1 == s2)
1149 return 0;
1150 if (s1 == NULL)
1151 return 1;
1152 if (s2 == NULL)
1153 return -1;
1154 return strcoll (s1, s2);
1158 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1159 elements in place. Return nonzero if out of memory, zero if successful.
1160 A slash is inserted between DIRNAME and each elt of ARRAY,
1161 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1162 static int
1163 prefix_array (dirname, array, n)
1164 const char *dirname;
1165 char **array;
1166 size_t n;
1168 register size_t i;
1169 size_t dirlen = strlen (dirname);
1170 #if defined __MSDOS__ || defined WINDOWS32
1171 int sep_char = '/';
1172 # define DIRSEP_CHAR sep_char
1173 #else
1174 # define DIRSEP_CHAR '/'
1175 #endif
1177 if (dirlen == 1 && dirname[0] == '/')
1178 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1179 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1180 dirlen = 0;
1181 #if defined __MSDOS__ || defined WINDOWS32
1182 else if (dirlen > 1)
1184 if (dirname[dirlen - 1] == '/' && dirname[dirlen - 2] == ':')
1185 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1186 --dirlen;
1187 else if (dirname[dirlen - 1] == ':')
1189 /* DIRNAME is "d:". Use `:' instead of `/'. */
1190 --dirlen;
1191 sep_char = ':';
1194 #endif
1196 for (i = 0; i < n; ++i)
1198 size_t eltlen = strlen (array[i]) + 1;
1199 char *new = (char *) malloc (dirlen + 1 + eltlen);
1200 if (new == NULL)
1202 while (i > 0)
1203 free ((__ptr_t) array[--i]);
1204 return 1;
1207 #ifdef HAVE_MEMPCPY
1209 char *endp = (char *) mempcpy (new, dirname, dirlen);
1210 *endp++ = DIRSEP_CHAR;
1211 mempcpy (endp, array[i], eltlen);
1213 #else
1214 memcpy (new, dirname, dirlen);
1215 new[dirlen] = DIRSEP_CHAR;
1216 memcpy (&new[dirlen + 1], array[i], eltlen);
1217 #endif
1218 free ((__ptr_t) array[i]);
1219 array[i] = new;
1222 return 0;
1226 /* We must not compile this function twice. */
1227 #if !defined _LIBC || !defined NO_GLOB_PATTERN_P
1228 /* Return nonzero if PATTERN contains any metacharacters.
1229 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1231 __glob_pattern_p (pattern, quote)
1232 const char *pattern;
1233 int quote;
1235 register const char *p;
1236 int open = 0;
1238 for (p = pattern; *p != '\0'; ++p)
1239 switch (*p)
1241 case '?':
1242 case '*':
1243 return 1;
1245 case '\\':
1246 if (quote && p[1] != '\0')
1247 ++p;
1248 break;
1250 case '[':
1251 open = 1;
1252 break;
1254 case ']':
1255 if (open)
1256 return 1;
1257 break;
1260 return 0;
1262 # ifdef _LIBC
1263 weak_alias (__glob_pattern_p, glob_pattern_p)
1264 # endif
1265 #endif
1267 #endif /* !GLOB_ONLY_P */
1270 /* Like `glob', but PATTERN is a final pathname component,
1271 and matches are searched for in DIRECTORY.
1272 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1273 The GLOB_APPEND flag is assumed to be set (always appends). */
1274 static int
1275 glob_in_dir (pattern, directory, flags, errfunc, pglob)
1276 const char *pattern;
1277 const char *directory;
1278 int flags;
1279 int (*errfunc) __P ((const char *, int));
1280 glob_t *pglob;
1282 __ptr_t stream = NULL;
1283 struct globlink
1285 struct globlink *next;
1286 char *name;
1288 struct globlink *names = NULL;
1289 size_t nfound;
1290 int meta;
1291 int save;
1293 meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
1294 if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
1296 /* We need not do any tests. The PATTERN contains no meta
1297 characters and we must not return an error therefore the
1298 result will always contain exactly one name. */
1299 flags |= GLOB_NOCHECK;
1300 nfound = 0;
1302 else if (meta == 0 &&
1303 ((flags & GLOB_NOESCAPE) || strchr(pattern, '\\') == NULL))
1305 /* Since we use the normal file functions we can also use stat()
1306 to verify the file is there. */
1307 struct stat st;
1308 # ifdef HAVE_STAT64
1309 struct stat64 st64;
1310 # endif
1311 size_t patlen = strlen (pattern);
1312 size_t dirlen = strlen (directory);
1313 char *fullname = (char *) __alloca (dirlen + 1 + patlen + 1);
1315 # ifdef HAVE_MEMPCPY
1316 mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
1317 "/", 1),
1318 pattern, patlen + 1);
1319 # else
1320 memcpy (fullname, directory, dirlen);
1321 fullname[dirlen] = '/';
1322 memcpy (&fullname[dirlen + 1], pattern, patlen + 1);
1323 # endif
1324 if (((flags & GLOB_ALTDIRFUNC)
1325 ? (*pglob->gl_stat) (fullname, &st)
1326 : __stat64 (fullname, &st64)) == 0)
1327 /* We found this file to be existing. Now tell the rest
1328 of the function to copy this name into the result. */
1329 flags |= GLOB_NOCHECK;
1331 nfound = 0;
1333 else
1335 if (pattern[0] == '\0')
1337 /* This is a special case for matching directories like in
1338 "*a/". */
1339 names = (struct globlink *) __alloca (sizeof (struct globlink));
1340 names->name = (char *) malloc (1);
1341 if (names->name == NULL)
1342 goto memory_error;
1343 names->name[0] = '\0';
1344 names->next = NULL;
1345 nfound = 1;
1346 meta = 0;
1348 else
1350 stream = ((flags & GLOB_ALTDIRFUNC)
1351 ? (*pglob->gl_opendir) (directory)
1352 : (__ptr_t) opendir (directory));
1353 if (stream == NULL)
1355 if (errno != ENOTDIR
1356 && ((errfunc != NULL && (*errfunc) (directory, errno))
1357 || (flags & GLOB_ERR)))
1358 return GLOB_ABORTED;
1359 nfound = 0;
1360 meta = 0;
1362 else
1364 int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
1365 | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
1366 #if defined _AMIGA || defined VMS
1367 | FNM_CASEFOLD
1368 #endif
1370 nfound = 0;
1371 flags |= GLOB_MAGCHAR;
1373 while (1)
1375 const char *name;
1376 size_t len;
1377 #if defined HAVE_DIRENT64 && !defined COMPILE_GLOB64
1378 struct dirent64 *d;
1379 struct dirent64 d64;
1381 if (flags & GLOB_ALTDIRFUNC)
1383 struct dirent *d32 = (*pglob->gl_readdir) (stream);
1384 if (d32 != NULL)
1386 CONVERT_DIRENT_DIRENT64 (&d64, d32);
1387 d = &d64;
1389 else
1390 d = NULL;
1392 else
1393 d = __readdir64 ((DIR *) stream);
1394 #else
1395 struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
1396 ? ((struct dirent *)
1397 (*pglob->gl_readdir) (stream))
1398 : __readdir ((DIR *) stream));
1399 #endif
1400 if (d == NULL)
1401 break;
1402 if (! REAL_DIR_ENTRY (d))
1403 continue;
1405 #ifdef HAVE_D_TYPE
1406 /* If we shall match only directories use the information
1407 provided by the dirent call if possible. */
1408 if ((flags & GLOB_ONLYDIR)
1409 && d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
1410 continue;
1411 #endif
1413 name = d->d_name;
1415 if (fnmatch (pattern, name, fnm_flags) == 0)
1417 struct globlink *new = (struct globlink *)
1418 __alloca (sizeof (struct globlink));
1419 len = NAMLEN (d);
1420 new->name = (char *) malloc (len + 1);
1421 if (new->name == NULL)
1422 goto memory_error;
1423 #ifdef HAVE_MEMPCPY
1424 *((char *) mempcpy ((__ptr_t) new->name, name, len))
1425 = '\0';
1426 #else
1427 memcpy ((__ptr_t) new->name, name, len);
1428 new->name[len] = '\0';
1429 #endif
1430 new->next = names;
1431 names = new;
1432 ++nfound;
1439 if (nfound == 0 && (flags & GLOB_NOCHECK))
1441 size_t len = strlen (pattern);
1442 nfound = 1;
1443 names = (struct globlink *) __alloca (sizeof (struct globlink));
1444 names->next = NULL;
1445 names->name = (char *) malloc (len + 1);
1446 if (names->name == NULL)
1447 goto memory_error;
1448 #ifdef HAVE_MEMPCPY
1449 *((char *) mempcpy (names->name, pattern, len)) = '\0';
1450 #else
1451 memcpy (names->name, pattern, len);
1452 names->name[len] = '\0';
1453 #endif
1456 if (nfound != 0)
1458 pglob->gl_pathv
1459 = (char **) realloc (pglob->gl_pathv,
1460 (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
1461 * sizeof (char *));
1462 if (pglob->gl_pathv == NULL)
1463 goto memory_error;
1465 for (; names != NULL; names = names->next)
1466 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
1467 pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
1469 pglob->gl_flags = flags;
1472 save = errno;
1473 if (stream != NULL)
1475 if (flags & GLOB_ALTDIRFUNC)
1476 (*pglob->gl_closedir) (stream);
1477 else
1478 closedir ((DIR *) stream);
1480 __set_errno (save);
1482 return nfound == 0 ? GLOB_NOMATCH : 0;
1484 memory_error:
1486 int save = errno;
1487 if (flags & GLOB_ALTDIRFUNC)
1488 (*pglob->gl_closedir) (stream);
1489 else
1490 closedir ((DIR *) stream);
1491 __set_errno (save);
1493 while (names != NULL)
1495 if (names->name != NULL)
1496 free ((__ptr_t) names->name);
1497 names = names->next;
1499 return GLOB_NOSPACE;
1502 #endif /* Not ELIDE_CODE. */