1 /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If not,
15 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
18 /* AIX requires this to be the first thing in the file. */
19 #if defined _AIX && !defined __GNUC__
27 /* Enable GNU extensions in glob.h. */
29 # define _GNU_SOURCE 1
33 #include <sys/types.h>
36 /* Outcomment the following line for production quality code. */
37 /* #define NDEBUG 1 */
40 #include <stdio.h> /* Needed on stupid SunOS for assert. */
43 /* Comment out all this code if we are using the GNU C Library, and are not
44 actually compiling the library itself. This code is part of the GNU C
45 Library, but also included in many other GNU distributions. Compiling
46 and linking in this code is a waste when using the GNU C library
47 (especially if it is a shared library). Rather than having every GNU
48 program understand `configure --with-gnu-libc' and omit the object files,
49 it is simpler to just do this in the source for each such file. */
51 #define GLOB_INTERFACE_VERSION 1
52 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
53 # include <gnu-versions.h>
54 # if _GNU_GLOB_INTERFACE_VERSION == GLOB_INTERFACE_VERSION
61 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
65 #if defined HAVE_UNISTD_H || defined _LIBC
68 # ifdef _POSIX_VERSION
74 #if !defined _AMIGA && !defined VMS && !defined WINDOWS32
78 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
82 # define __set_errno(val) errno = (val)
90 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
92 # define NAMLEN(dirent) strlen((dirent)->d_name)
94 # define dirent direct
95 # define NAMLEN(dirent) (dirent)->d_namlen
96 # ifdef HAVE_SYS_NDIR_H
97 # include <sys/ndir.h>
99 # ifdef HAVE_SYS_DIR_H
100 # include <sys/dir.h>
105 # ifdef HAVE_VMSDIR_H
107 # endif /* HAVE_VMSDIR_H */
111 /* In GNU systems, <dirent.h> defines this macro for us. */
114 # define NAMLEN(d) _D_NAMLEN(d)
117 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
118 if the `d_type' member for `struct dirent' is available. */
119 #ifdef _DIRENT_HAVE_D_TYPE
120 # define HAVE_D_TYPE 1
124 #if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
125 /* Posix does not require that the d_ino field be present, and some
126 systems do not provide it. */
127 # define REAL_DIR_ENTRY(dp) 1
129 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
132 #if defined STDC_HEADERS || defined __GNU_LIBRARY__
136 #else /* No standard headers. */
138 extern char *getenv ();
140 # ifdef HAVE_STRING_H
144 # include <strings.h>
146 # ifdef HAVE_MEMORY_H
150 extern char *malloc (), *realloc ();
153 extern void qsort ();
154 extern void abort (), exit ();
156 #endif /* Standard headers. */
158 #ifdef HAVE_GETLOGIN_R
159 extern int getlogin_r
__P ((char *, size_t));
161 extern char *getlogin
__P ((void));
167 extern void bzero ();
170 extern void bcopy ();
173 # define memcpy(d, s, n) bcopy ((s), (d), (n))
174 # define strrchr rindex
175 /* memset is only used for zero here, but let's be paranoid. */
176 # define memset(s, better_be_zero, n) \
177 ((void) ((better_be_zero) == 0 ? (bzero((s), (n)), 0) : (abort(), 0)))
178 #endif /* Not ANSI_STRING. */
180 #if !defined HAVE_STRCOLL && !defined _LIBC
181 # define strcoll strcmp
184 #if !defined HAVE_MEMPCPY && __GLIBC__ - 0 == 2 && __GLIBC_MINOR__ >= 1
185 # define HAVE_MEMPCPY 1
186 # define mempcpy(Dest, Src, Len) __mempcpy (Dest, Src, Len)
189 #ifndef __GNU_LIBRARY__
203 /* These casts are the for sake of the broken Ultrix compiler,
204 which warns of illegal pointer combinations otherwise. */
206 return (char *) malloc (n
);
207 return (char *) realloc (p
, n
);
209 # define realloc my_realloc
211 #endif /* __GNU_LIBRARY__ */
214 #if !defined __alloca && !defined __GNU_LIBRARY__
218 # define alloca(n) __builtin_alloca (n)
219 # else /* Not GCC. */
220 # ifdef HAVE_ALLOCA_H
222 # else /* Not HAVE_ALLOCA_H. */
227 extern char *alloca ();
228 # endif /* WINDOWS32 */
229 # endif /* Not _AIX. */
230 # endif /* sparc or HAVE_ALLOCA_H. */
233 # define __alloca alloca
237 #ifndef __GNU_LIBRARY__
239 # ifdef STAT_MACROS_BROKEN
243 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
249 # define strdup(str) __strdup (str)
250 # define sysconf(id) __sysconf (id)
251 # define closedir(dir) __closedir (dir)
252 # define opendir(name) __opendir (name)
253 # define readdir(str) __readdir (str)
254 # define getpwnam_r(name, bufp, buf, len, res) \
255 __getpwnam_r (name, bufp, buf, len, res)
258 #if !(defined STDC_HEADERS || defined __GNU_LIBRARY__)
260 # define size_t unsigned int
263 /* Some system header files erroneously define these.
264 We want our own definitions from <fnmatch.h> to take precedence. */
265 #ifndef __GNU_LIBRARY__
272 /* Some system header files erroneously define these.
273 We want our own definitions from <glob.h> to take precedence. */
274 #ifndef __GNU_LIBRARY__
281 # undef GLOB_NOESCAPE
287 #if __GNUC__ - 0 >= 2
290 const char *next_brace_sub
__P ((const char *begin
));
291 static int glob_in_dir
__P ((const char *pattern
, const char *directory
,
293 int (*errfunc
) __P ((const char *, int)),
295 static int prefix_array
__P ((const char *prefix
, char **array
, size_t n
));
296 static int collated_compare
__P ((const __ptr_t
, const __ptr_t
));
299 /* Find the end of the sub-pattern in a brace expression. We define
300 this as an inline function if the compiler permits. */
302 #if __GNUC__ - 0 >= 2
306 next_brace_sub (begin
)
309 unsigned int depth
= 0;
310 const char *cp
= begin
;
316 if (*cp
!= ',' && *cp
!= '}' && *cp
!= '\0')
326 while (*cp
!= '\0' && (*cp
!= '}' || depth
> 0))
333 /* An incorrectly terminated brace expression. */
344 /* Do glob searching for PATTERN, placing results in PGLOB.
345 The bits defined above may be set in FLAGS.
346 If a directory cannot be opened or read and ERRFUNC is not nil,
347 it is called with the pathname that caused the error, and the
348 `errno' value from the failing call; if it returns non-zero
349 `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored.
350 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
351 Otherwise, `glob' returns zero. */
353 glob (pattern
, flags
, errfunc
, pglob
)
356 int (*errfunc
) __P ((const char *, int));
359 const char *filename
;
365 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0)
367 __set_errno (EINVAL
);
371 if (flags
& GLOB_BRACE
)
373 const char *begin
= strchr (pattern
, '{');
376 /* Allocate working buffer large enough for our work. Note that
377 we have at least an opening and closing brace. */
385 char onealt
[strlen (pattern
) - 1];
387 char *onealt
= (char *) malloc (strlen (pattern
) - 1);
390 if (!(flags
& GLOB_APPEND
))
396 /* We know the prefix for all sub-patterns. */
398 alt_start
= mempcpy (onealt
, pattern
, begin
- pattern
);
400 memcpy (onealt
, pattern
, begin
- pattern
);
401 alt_start
= &onealt
[begin
- pattern
];
404 /* Find the first sub-pattern and at the same time find the
405 rest after the closing brace. */
406 next
= next_brace_sub (begin
+ 1);
409 /* It is an illegal expression. */
413 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
416 /* Now find the end of the whole brace expression. */
420 rest
= next_brace_sub (rest
+ 1);
423 /* It is an illegal expression. */
427 return glob (pattern
, flags
& ~GLOB_BRACE
, errfunc
, pglob
);
430 /* Please note that we now can be sure the brace expression
432 rest_len
= strlen (++rest
) + 1;
434 /* We have a brace expression. BEGIN points to the opening {,
435 NEXT points past the terminator of the first element, and END
436 points past the final }. We will accumulate result names from
437 recursive runs for each brace alternative in the buffer using
440 if (!(flags
& GLOB_APPEND
))
442 /* This call is to set a new vector, so clear out the
443 vector so we can append to it. */
445 pglob
->gl_pathv
= NULL
;
447 firstc
= pglob
->gl_pathc
;
454 /* Construct the new glob expression. */
456 mempcpy (mempcpy (alt_start
, p
, next
- p
), rest
, rest_len
);
458 memcpy (alt_start
, p
, next
- p
);
459 memcpy (&alt_start
[next
- p
], rest
, rest_len
);
462 result
= glob (onealt
,
463 ((flags
& ~(GLOB_NOCHECK
|GLOB_NOMAGIC
))
464 | GLOB_APPEND
), errfunc
, pglob
);
466 /* If we got an error, return it. */
467 if (result
&& result
!= GLOB_NOMATCH
)
472 if (!(flags
& GLOB_APPEND
))
478 /* We saw the last entry. */
482 next
= next_brace_sub (p
);
483 assert (next
!= NULL
);
490 if (pglob
->gl_pathc
!= firstc
)
491 /* We found some entries. */
493 else if (!(flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
)))
498 /* Find the filename. */
499 filename
= strrchr (pattern
, '/');
500 #if defined __MSDOS__ || defined WINDOWS32
501 /* The case of "d:pattern". Since `:' is not allowed in
502 file names, we can safely assume that wherever it
503 happens in pattern, it signals the filename part. This
504 is so we could some day support patterns like "[a-z]:foo". */
505 if (filename
== NULL
)
506 filename
= strchr (pattern
, ':');
507 #endif /* __MSDOS__ || WINDOWS32 */
508 if (filename
== NULL
)
510 /* This can mean two things: a simple name or "~name". The later
511 case is nothing but a notation for a directory. */
512 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && pattern
[0] == '~')
515 dirlen
= strlen (pattern
);
517 /* Set FILENAME to NULL as a special flag. This is ugly but
518 other solutions would require much more code. We test for
519 this special case below. */
533 else if (filename
== pattern
)
543 dirlen
= filename
- pattern
;
544 #if defined __MSDOS__ || defined WINDOWS32
546 || (filename
> pattern
+ 1 && filename
[-1] == ':'))
551 drive_spec
= (char *) __alloca (dirlen
+ 1);
553 *((char *) mempcpy (drive_spec
, pattern
, dirlen
)) = '\0';
555 memcpy (drive_spec
, pattern
, dirlen
);
556 drive_spec
[dirlen
] = '\0';
558 /* For now, disallow wildcards in the drive spec, to
559 prevent infinite recursion in glob. */
560 if (__glob_pattern_p (drive_spec
, !(flags
& GLOB_NOESCAPE
)))
562 /* If this is "d:pattern", we need to copy `:' to DIRNAME
563 as well. If it's "d:/pattern", don't remove the slash
564 from "d:/", since "d:" and "d:/" are not the same.*/
567 newp
= (char *) __alloca (dirlen
+ 1);
569 *((char *) mempcpy (newp
, pattern
, dirlen
)) = '\0';
571 memcpy (newp
, pattern
, dirlen
);
577 if (filename
[0] == '\0'
578 #if defined __MSDOS__ || defined WINDOWS32
579 && dirname
[dirlen
- 1] != ':'
580 && (dirlen
< 3 || dirname
[dirlen
- 2] != ':'
581 || dirname
[dirlen
- 1] != '/')
584 /* "pattern/". Expand "pattern", appending slashes. */
586 int val
= glob (dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
588 pglob
->gl_flags
= ((pglob
->gl_flags
& ~GLOB_MARK
)
589 | (flags
& GLOB_MARK
));
594 if (!(flags
& GLOB_APPEND
))
597 pglob
->gl_pathv
= NULL
;
600 oldcount
= pglob
->gl_pathc
;
603 if ((flags
& (GLOB_TILDE
|GLOB_TILDE_CHECK
)) && dirname
[0] == '~')
605 if (dirname
[1] == '\0' || dirname
[1] == '/')
607 /* Look up home directory. */
608 const char *home_dir
= getenv ("HOME");
610 if (home_dir
== NULL
|| home_dir
[0] == '\0')
614 if (home_dir
== NULL
|| home_dir
[0] == '\0')
615 home_dir
= "c:/users/default"; /* poor default */
617 if (home_dir
== NULL
|| home_dir
[0] == '\0')
621 # if defined HAVE_GETLOGIN_R || defined _LIBC
622 size_t buflen
= sysconf (_SC_LOGIN_NAME_MAX
) + 1;
625 /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
628 name
= (char *) __alloca (buflen
);
630 success
= getlogin_r (name
, buflen
) >= 0;
632 success
= (name
= getlogin ()) != NULL
;
637 # if defined HAVE_GETPWNAM_R || defined _LIBC
638 size_t pwbuflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
643 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX.
644 Try a moderate value. */
646 pwtmpbuf
= (char *) __alloca (pwbuflen
);
648 success
= (getpwnam_r (name
, &pwbuf
, pwtmpbuf
, pwbuflen
, &p
)
655 home_dir
= p
->pw_dir
;
658 if (home_dir
== NULL
|| home_dir
[0] == '\0')
659 if (flags
& GLOB_TILDE_CHECK
)
662 home_dir
= "~"; /* No luck. */
663 # endif /* WINDOWS32 */
665 /* Now construct the full directory. */
666 if (dirname
[1] == '\0')
671 size_t home_len
= strlen (home_dir
);
672 newp
= (char *) __alloca (home_len
+ dirlen
);
674 mempcpy (mempcpy (newp
, home_dir
, home_len
),
675 &dirname
[1], dirlen
);
677 memcpy (newp
, home_dir
, home_len
);
678 memcpy (&newp
[home_len
], &dirname
[1], dirlen
);
683 # if !defined _AMIGA && !defined WINDOWS32
686 char *end_name
= strchr (dirname
, '/');
687 const char *user_name
;
688 const char *home_dir
;
690 if (end_name
== NULL
)
691 user_name
= dirname
+ 1;
695 newp
= (char *) __alloca (end_name
- dirname
);
697 *((char *) mempcpy (newp
, dirname
+ 1, end_name
- dirname
))
700 memcpy (newp
, dirname
+ 1, end_name
- dirname
);
701 newp
[end_name
- dirname
- 1] = '\0';
706 /* Look up specific user's home directory. */
709 # if defined HAVE_GETPWNAM_R || defined _LIBC
710 size_t buflen
= sysconf (_SC_GETPW_R_SIZE_MAX
);
715 /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a
718 pwtmpbuf
= (char *) __alloca (buflen
);
720 if (getpwnam_r (user_name
, &pwbuf
, pwtmpbuf
, buflen
, &p
) >= 0)
721 home_dir
= p
->pw_dir
;
725 p
= getpwnam (user_name
);
727 home_dir
= p
->pw_dir
;
732 /* If we found a home directory use this. */
733 if (home_dir
!= NULL
)
736 size_t home_len
= strlen (home_dir
);
737 size_t rest_len
= end_name
== NULL
? 0 : strlen (end_name
);
738 newp
= (char *) __alloca (home_len
+ rest_len
+ 1);
740 *((char *) mempcpy (mempcpy (newp
, home_dir
, home_len
),
741 end_name
, rest_len
)) = '\0';
743 memcpy (newp
, home_dir
, home_len
);
744 memcpy (&newp
[home_len
], end_name
, rest_len
);
745 newp
[home_len
+ rest_len
] = '\0';
750 if (flags
& GLOB_TILDE_CHECK
)
751 /* We have to regard it as an error if we cannot find the
755 # endif /* Not Amiga && not WINDOWS32. */
757 #endif /* Not VMS. */
759 /* Now test whether we looked for "~" or "~NAME". In this case we
760 can give the answer now. */
761 if (filename
== NULL
)
765 /* Return the directory if we don't check for error or if it exists. */
766 if ((flags
& GLOB_NOCHECK
)
767 || (((flags
& GLOB_ALTDIRFUNC
)
768 ? (*pglob
->gl_stat
) (dirname
, &st
)
769 : __stat (dirname
, &st
)) == 0
770 && S_ISDIR (st
.st_mode
)))
773 = (char **) realloc (pglob
->gl_pathv
,
775 ((flags
& GLOB_DOOFFS
) ?
776 pglob
->gl_offs
: 0) +
779 if (pglob
->gl_pathv
== NULL
)
782 if (flags
& GLOB_DOOFFS
)
783 while (pglob
->gl_pathc
< pglob
->gl_offs
)
784 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
786 #if defined HAVE_STRDUP || defined _LIBC
787 pglob
->gl_pathv
[pglob
->gl_pathc
] = strdup (dirname
);
790 size_t len
= strlen (dirname
) + 1;
791 char *dircopy
= malloc (len
);
793 pglob
->gl_pathv
[pglob
->gl_pathc
] = memcpy (dircopy
, dirname
,
797 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
799 free (pglob
->gl_pathv
);
802 pglob
->gl_pathv
[++pglob
->gl_pathc
] = NULL
;
803 pglob
->gl_flags
= flags
;
812 if (__glob_pattern_p (dirname
, !(flags
& GLOB_NOESCAPE
)))
814 /* The directory name contains metacharacters, so we
815 have to glob for the directory, and then glob for
816 the pattern in each directory found. */
820 status
= glob (dirname
,
821 ((flags
& (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
))
822 | GLOB_NOSORT
| GLOB_ONLYDIR
),
827 /* We have successfully globbed the preceding directory name.
828 For each name we found, call glob_in_dir on it and FILENAME,
829 appending the results to PGLOB. */
830 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
836 /* Make globbing interruptible in the bash shell. */
837 extern int interrupt_state
;
848 old_pathc
= pglob
->gl_pathc
;
849 status
= glob_in_dir (filename
, dirs
.gl_pathv
[i
],
850 ((flags
| GLOB_APPEND
)
851 & ~(GLOB_NOCHECK
| GLOB_ERR
)),
853 if (status
== GLOB_NOMATCH
)
854 /* No matches in this directory. Try the next. */
864 /* Stick the directory on the front of each name. */
865 if (prefix_array (dirs
.gl_pathv
[i
],
866 &pglob
->gl_pathv
[old_pathc
],
867 pglob
->gl_pathc
- old_pathc
))
875 flags
|= GLOB_MAGCHAR
;
877 /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
878 But if we have not found any matching entry and thie GLOB_NOCHECK
879 flag was set we must return the list consisting of the disrectory
880 names followed by the filename. */
881 if (pglob
->gl_pathc
== oldcount
)
883 if (flags
& GLOB_NOCHECK
)
885 size_t filename_len
= strlen (filename
) + 1;
889 /* This is an pessimistic guess about the size. */
891 = (char **) realloc (pglob
->gl_pathv
,
893 ((flags
& GLOB_DOOFFS
) ?
894 pglob
->gl_offs
: 0) +
897 if (pglob
->gl_pathv
== NULL
)
903 if (flags
& GLOB_DOOFFS
)
904 while (pglob
->gl_pathc
< pglob
->gl_offs
)
905 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
907 for (i
= 0; i
< dirs
.gl_pathc
; ++i
)
909 const char *dir
= dirs
.gl_pathv
[i
];
910 size_t dir_len
= strlen (dir
);
912 /* First check whether this really is a directory. */
913 if (((flags
& GLOB_ALTDIRFUNC
)
914 ? (*pglob
->gl_stat
) (dir
, &st
) : __stat (dir
, &st
)) != 0
915 || !S_ISDIR (st
.st_mode
))
916 /* No directory, ignore this entry. */
919 pglob
->gl_pathv
[pglob
->gl_pathc
] = malloc (dir_len
+ 1
921 if (pglob
->gl_pathv
[pglob
->gl_pathc
] == NULL
)
929 mempcpy (mempcpy (mempcpy (pglob
->gl_pathv
[pglob
->gl_pathc
],
932 filename
, filename_len
);
934 memcpy (pglob
->gl_pathv
[pglob
->gl_pathc
], dir
, dir_len
);
935 pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
] = '/';
936 memcpy (&pglob
->gl_pathv
[pglob
->gl_pathc
][dir_len
+ 1],
937 filename
, filename_len
);
942 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
943 pglob
->gl_flags
= flags
;
945 /* Now we know how large the gl_pathv vector must be. */
946 new_pathv
= (char **) realloc (pglob
->gl_pathv
,
947 ((pglob
->gl_pathc
+ 1)
949 if (new_pathv
!= NULL
)
950 pglob
->gl_pathv
= new_pathv
;
959 status
= glob_in_dir (filename
, dirname
, flags
, errfunc
, pglob
);
965 /* Stick the directory on the front of each name. */
966 int ignore
= oldcount
;
968 if ((flags
& GLOB_DOOFFS
) && ignore
< pglob
->gl_offs
)
969 ignore
= pglob
->gl_offs
;
971 if (prefix_array (dirname
,
972 &pglob
->gl_pathv
[ignore
],
973 pglob
->gl_pathc
- ignore
))
981 if (flags
& GLOB_MARK
)
983 /* Append slashes to directory names. */
986 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
987 if (((flags
& GLOB_ALTDIRFUNC
)
988 ? (*pglob
->gl_stat
) (pglob
->gl_pathv
[i
], &st
)
989 : __stat (pglob
->gl_pathv
[i
], &st
)) == 0
990 && S_ISDIR (st
.st_mode
))
992 size_t len
= strlen (pglob
->gl_pathv
[i
]) + 2;
993 char *new = realloc (pglob
->gl_pathv
[i
], len
);
999 strcpy (&new[len
- 2], "/");
1000 pglob
->gl_pathv
[i
] = new;
1004 if (!(flags
& GLOB_NOSORT
))
1006 /* Sort the vector. */
1007 int non_sort
= oldcount
;
1009 if ((flags
& GLOB_DOOFFS
) && pglob
->gl_offs
> oldcount
)
1010 non_sort
= pglob
->gl_offs
;
1012 qsort ((__ptr_t
) &pglob
->gl_pathv
[non_sort
],
1013 pglob
->gl_pathc
- non_sort
,
1014 sizeof (char *), collated_compare
);
1021 /* Free storage allocated in PGLOB by a previous `glob' call. */
1024 register glob_t
*pglob
;
1026 if (pglob
->gl_pathv
!= NULL
)
1029 for (i
= 0; i
< pglob
->gl_pathc
; ++i
)
1030 if (pglob
->gl_pathv
[i
] != NULL
)
1031 free ((__ptr_t
) pglob
->gl_pathv
[i
]);
1032 free ((__ptr_t
) pglob
->gl_pathv
);
1037 /* Do a collated comparison of A and B. */
1039 collated_compare (a
, b
)
1043 const char *const s1
= *(const char *const * const) a
;
1044 const char *const s2
= *(const char *const * const) b
;
1052 return strcoll (s1
, s2
);
1056 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
1057 elements in place. Return nonzero if out of memory, zero if successful.
1058 A slash is inserted between DIRNAME and each elt of ARRAY,
1059 unless DIRNAME is just "/". Each old element of ARRAY is freed. */
1061 prefix_array (dirname
, array
, n
)
1062 const char *dirname
;
1067 size_t dirlen
= strlen (dirname
);
1068 #if defined __MSDOS__ || defined WINDOWS32
1070 # define DIRSEP_CHAR sep_char
1072 # define DIRSEP_CHAR '/'
1075 if (dirlen
== 1 && dirname
[0] == '/')
1076 /* DIRNAME is just "/", so normal prepending would get us "//foo".
1077 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
1079 #if defined __MSDOS__ || defined WINDOWS32
1080 else if (dirlen
> 1)
1082 if (dirname
[dirlen
- 1] == '/')
1083 /* DIRNAME is "d:/". Don't prepend the slash from DIRNAME. */
1085 else if (dirname
[dirlen
- 1] == ':')
1087 /* DIRNAME is "d:". Use `:' instead of `/'. */
1094 for (i
= 0; i
< n
; ++i
)
1096 size_t eltlen
= strlen (array
[i
]) + 1;
1097 char *new = (char *) malloc (dirlen
+ 1 + eltlen
);
1101 free ((__ptr_t
) array
[--i
]);
1107 char *endp
= (char *) mempcpy (new, dirname
, dirlen
);
1108 *endp
++ = DIRSEP_CHAR
;
1109 mempcpy (endp
, array
[i
], eltlen
);
1112 memcpy (new, dirname
, dirlen
);
1113 new[dirlen
] = DIRSEP_CHAR
;
1114 memcpy (&new[dirlen
+ 1], array
[i
], eltlen
);
1116 free ((__ptr_t
) array
[i
]);
1124 /* Return nonzero if PATTERN contains any metacharacters.
1125 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
1127 __glob_pattern_p (pattern
, quote
)
1128 const char *pattern
;
1131 register const char *p
;
1134 for (p
= pattern
; *p
!= '\0'; ++p
)
1142 if (quote
&& p
[1] != '\0')
1159 weak_alias (__glob_pattern_p
, glob_pattern_p
)
1163 /* Like `glob', but PATTERN is a final pathname component,
1164 and matches are searched for in DIRECTORY.
1165 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
1166 The GLOB_APPEND flag is assumed to be set (always appends). */
1168 glob_in_dir (pattern
, directory
, flags
, errfunc
, pglob
)
1169 const char *pattern
;
1170 const char *directory
;
1172 int (*errfunc
) __P ((const char *, int));
1175 __ptr_t stream
= NULL
;
1179 struct globlink
*next
;
1182 struct globlink
*names
= NULL
;
1187 meta
= __glob_pattern_p (pattern
, !(flags
& GLOB_NOESCAPE
));
1190 if (flags
& (GLOB_NOCHECK
|GLOB_NOMAGIC
))
1191 /* We need not do any tests. The PATTERN contains no meta
1192 characters and we must not return an error therefore the
1193 result will always contain exactly one name. */
1194 flags
|= GLOB_NOCHECK
;
1197 /* Since we use the normal file functions we can also use stat()
1198 to verify the file is there. */
1200 size_t patlen
= strlen (pattern
);
1201 size_t dirlen
= strlen (directory
);
1202 char *fullname
= (char *) __alloca (dirlen
+ 1 + patlen
+ 1);
1204 # ifdef HAVE_MEMPCPY
1205 mempcpy (mempcpy (mempcpy (fullname
, directory
, dirlen
),
1207 pattern
, patlen
+ 1);
1209 memcpy (fullname
, directory
, dirlen
);
1210 fullname
[dirlen
] = '/';
1211 memcpy (&fullname
[dirlen
+ 1], pattern
, patlen
+ 1);
1213 if (((flags
& GLOB_ALTDIRFUNC
)
1214 ? (*pglob
->gl_stat
) (fullname
, &st
)
1215 : __stat (fullname
, &st
)) == 0)
1216 /* We found this file to be existing. Now tell the rest
1217 of the function to copy this name into the result. */
1218 flags
|= GLOB_NOCHECK
;
1225 if (pattern
[0] == '\0')
1227 /* This is a special case for matching directories like in
1229 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1230 names
->name
= (char *) malloc (1);
1231 if (names
->name
== NULL
)
1233 names
->name
[0] = '\0';
1240 stream
= ((flags
& GLOB_ALTDIRFUNC
)
1241 ? (*pglob
->gl_opendir
) (directory
)
1242 : (__ptr_t
) opendir (directory
));
1245 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
))
1246 || (flags
& GLOB_ERR
))
1247 return GLOB_ABORTED
;
1253 int fnm_flags
= ((!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0)
1254 | ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)
1255 #if defined _AMIGA || defined VMS
1260 flags
|= GLOB_MAGCHAR
;
1266 struct dirent
*d
= ((flags
& GLOB_ALTDIRFUNC
)
1267 ? (*pglob
->gl_readdir
) (stream
)
1268 : readdir ((DIR *) stream
));
1271 if (! REAL_DIR_ENTRY (d
))
1275 /* If we shall match only directories use the information
1276 provided by the dirent call if possible. */
1277 if ((flags
& GLOB_ONLYDIR
)
1278 && d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
1284 if (fnmatch (pattern
, name
, fnm_flags
) == 0)
1286 struct globlink
*new = (struct globlink
*)
1287 __alloca (sizeof (struct globlink
));
1289 new->name
= (char *) malloc (len
+ 1);
1290 if (new->name
== NULL
)
1293 *((char *) mempcpy ((__ptr_t
) new->name
, name
, len
))
1296 memcpy ((__ptr_t
) new->name
, name
, len
);
1297 new->name
[len
] = '\0';
1308 if (nfound
== 0 && (flags
& GLOB_NOCHECK
))
1310 size_t len
= strlen (pattern
);
1312 names
= (struct globlink
*) __alloca (sizeof (struct globlink
));
1314 names
->name
= (char *) malloc (len
+ 1);
1315 if (names
->name
== NULL
)
1318 *((char *) mempcpy (names
->name
, pattern
, len
)) = '\0';
1320 memcpy (names
->name
, pattern
, len
);
1321 names
->name
[len
] = '\0';
1328 = (char **) realloc (pglob
->gl_pathv
,
1330 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
1333 if (pglob
->gl_pathv
== NULL
)
1336 if (flags
& GLOB_DOOFFS
)
1337 while (pglob
->gl_pathc
< pglob
->gl_offs
)
1338 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
1340 for (; names
!= NULL
; names
= names
->next
)
1341 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
1342 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
1344 pglob
->gl_flags
= flags
;
1349 if (flags
& GLOB_ALTDIRFUNC
)
1350 (*pglob
->gl_closedir
) (stream
);
1352 closedir ((DIR *) stream
);
1355 return nfound
== 0 ? GLOB_NOMATCH
: 0;
1360 if (flags
& GLOB_ALTDIRFUNC
)
1361 (*pglob
->gl_closedir
) (stream
);
1363 closedir ((DIR *) stream
);
1366 while (names
!= NULL
)
1368 if (names
->name
!= NULL
)
1369 free ((__ptr_t
) names
->name
);
1370 names
= names
->next
;
1372 return GLOB_NOSPACE
;
1375 #endif /* Not ELIDE_CODE. */