1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
21 #include <linux/stat.h>
30 extern __ptr_t(*__glob_opendir_hook
) __P((const char *directory
));
31 extern void (*__glob_closedir_hook
) __P((__ptr_t stream
));
32 extern const char *(*__glob_readdir_hook
) __P((__ptr_t stream
));
34 static int glob_in_dir
__P((const char *pattern
, const char *directory
,
36 int (*errfunc
) __P((const char *, int)),
38 static int prefix_array
__P((const char *prefix
, char **array
, size_t n
,
42 extern int glob_pattern_p(const char *pattern
, int quote
);
44 /* Return nonzero if PATTERN contains any metacharacters.
45 Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
46 int glob_pattern_p(const char *pattern
, int quote
)
51 for (p
= pattern
; *p
!= '\0'; ++p
)
58 if (quote
&& p
[1] != '\0')
76 #ifdef CONFIG_GLOB_SORT
77 /* Do a collated comparison of A and B. */
78 static int collated_compare(a
, b
)
82 const char *const s1
= *(const char *const *)a
;
83 const char *const s2
= *(const char *const *)b
;
91 return strcmp(s1
, s2
);
95 /* Do glob searching for PATTERN, placing results in PGLOB.
96 The bits defined above may be set in FLAGS.
97 If a directory cannot be opened or read and ERRFUNC is not nil,
98 it is called with the pathname that caused the error, and the
99 `errno' value from the failing call; if it returns non-zero
100 `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
101 If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
102 Otherwise, `glob' returns zero. */
103 int glob(pattern
, flags
, errfunc
, pglob
)
106 int (*errfunc
) __P((const char *, int));
109 const char *filename
;
110 char *dirname
= NULL
;
115 if (pattern
== NULL
|| pglob
== NULL
|| (flags
& ~__GLOB_FLAGS
) != 0) {
120 /* Find the filename. */
121 filename
= strrchr(pattern
, '/');
122 if (filename
== NULL
) {
124 dirname
= strdup(".");
126 } else if (filename
== pattern
) {
128 dirname
= strdup("/");
132 dirlen
= filename
- pattern
;
133 dirname
= (char *)xmalloc(dirlen
+ 1);
134 memcpy(dirname
, pattern
, dirlen
);
135 dirname
[dirlen
] = '\0';
139 if (filename
[0] == '\0' && dirlen
> 1) {
140 /* "pattern/". Expand "pattern", appending slashes. */
141 int val
= glob(dirname
, flags
| GLOB_MARK
, errfunc
, pglob
);
145 gl_flags
& ~GLOB_MARK
) | (flags
& GLOB_MARK
);
150 if (!(flags
& GLOB_APPEND
)) {
152 pglob
->gl_pathv
= NULL
;
155 oldcount
= pglob
->gl_pathc
;
157 if (glob_pattern_p(dirname
, !(flags
& GLOB_NOESCAPE
))) {
158 /* The directory name contains metacharacters, so we
159 have to glob for the directory, and then glob for
160 the pattern in each directory found. */
163 status
= glob(dirname
,
165 (GLOB_ERR
| GLOB_NOCHECK
| GLOB_NOESCAPE
)) |
166 GLOB_NOSORT
), errfunc
, &dirs
);
170 /* We have successfully globbed the preceding directory name.
171 For each name we found, call glob_in_dir on it and FILENAME,
172 appending the results to PGLOB. */
173 for (i
= 0; i
< dirs
.gl_pathc
; ++i
) {
178 /* Make globbing interruptible in the bash shell. */
179 extern int interrupt_state
;
181 if (interrupt_state
) {
184 status
= GLOB_ABEND
goto out
;
189 oldcount
= pglob
->gl_pathc
;
190 status
= glob_in_dir(filename
, dirs
.gl_pathv
[i
],
191 (flags
| GLOB_APPEND
) &
192 ~GLOB_NOCHECK
, errfunc
, pglob
);
193 if (status
== GLOB_NOMATCH
) {
194 /* No matches in this directory. Try the next. */
203 /* Stick the directory on the front of each name. */
204 prefix_array(dirs
.gl_pathv
[i
],
205 &pglob
->gl_pathv
[oldcount
],
206 pglob
->gl_pathc
- oldcount
,
211 flags
|= GLOB_MAGCHAR
;
213 if (pglob
->gl_pathc
== oldcount
) {
215 #ifdef CONFIG_GLOB_NOCHECK
216 if (flags
& GLOB_NOCHECK
) {
217 size_t len
= strlen(pattern
) + 1;
218 char *patcopy
= (char *)xmalloc(len
);
219 memcpy(patcopy
, pattern
, len
);
222 = (char **)xrealloc(pglob
->gl_pathv
,
224 ((flags
& GLOB_DOOFFS
) ?
225 pglob
->gl_offs
: 0) +
229 if (flags
& GLOB_DOOFFS
)
230 while (pglob
->gl_pathc
< pglob
->gl_offs
)
231 pglob
->gl_pathv
[pglob
->
235 pglob
->gl_pathv
[pglob
->gl_pathc
++] = patcopy
;
236 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
237 pglob
->gl_flags
= flags
;
241 status
= GLOB_NOMATCH
;
246 status
= glob_in_dir(filename
, dirname
, flags
, errfunc
, pglob
);
251 /* Stick the directory on the front of each name. */
252 prefix_array(dirname
,
253 &pglob
->gl_pathv
[oldcount
],
254 pglob
->gl_pathc
- oldcount
,
259 if (flags
& GLOB_MARK
) {
260 /* Append slashes to directory names. glob_in_dir has already
261 allocated the extra character for us. */
264 for (i
= oldcount
; i
< pglob
->gl_pathc
; ++i
)
265 if (stat(pglob
->gl_pathv
[i
], &st
) == 0 &&
267 strcat(pglob
->gl_pathv
[i
], "/");
269 #ifdef CONFIG_GLOB_SORT
270 if (!(flags
& GLOB_NOSORT
))
271 /* Sort the vector. */
272 qsort((__ptr_t
) & pglob
->gl_pathv
[oldcount
],
273 pglob
->gl_pathc
- oldcount
,
274 sizeof(char *), (__compar_fn_t
) collated_compare
);
283 /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
284 elements in place. Return nonzero if out of memory, zero if successful.
285 A slash is inserted between DIRNAME and each elt of ARRAY,
286 unless DIRNAME is just "/". Each old element of ARRAY is freed.
287 If ADD_SLASH is non-zero, allocate one character more than
288 necessary, so that a slash can be appended later. */
289 static int prefix_array(dirname
, array
, n
, add_slash
)
296 size_t dirlen
= strlen(dirname
);
298 if (dirlen
== 1 && dirname
[0] == '/')
299 /* DIRNAME is just "/", so normal prepending would get us "//foo".
300 We want "/foo" instead, so don't prepend any chars from DIRNAME. */
303 for (i
= 0; i
< n
; ++i
) {
304 size_t eltlen
= strlen(array
[i
]) + 1;
306 (char *)xmalloc(dirlen
+ 1 + eltlen
+ (add_slash
? 1 : 0));
308 memcpy(new, dirname
, dirlen
);
310 memcpy(&new[dirlen
+ 1], array
[i
], eltlen
);
311 free((__ptr_t
) array
[i
]);
318 /* Like `glob', but PATTERN is a final pathname component,
319 and matches are searched for in DIRECTORY.
320 The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
321 The GLOB_APPEND flag is assumed to be set (always appends). */
322 static int glob_in_dir(pattern
, directory
, flags
, errfunc
, pglob
)
324 const char *directory
;
326 int (*errfunc
) __P((const char *, int));
332 struct globlink
*next
;
335 struct globlink
*names
= NULL
;
339 stream
= opendir(directory
);
341 if (stream
== NULL
) {
342 if ((errfunc
!= NULL
&& (*errfunc
) (directory
, errno
)) ||
347 meta
= glob_pattern_p(pattern
, !(flags
& GLOB_NOESCAPE
));
350 flags
|= GLOB_MAGCHAR
;
356 struct dirent
*d
= readdir((DIR *) stream
);
359 // if (! (d->d_ino != 0))
363 if ((!meta
&& strcmp(pattern
, name
) == 0)
364 || fnmatch(pattern
, name
,
365 (!(flags
& GLOB_PERIOD
) ? FNM_PERIOD
: 0) |
366 ((flags
& GLOB_NOESCAPE
) ? FNM_NOESCAPE
: 0)) == 0) {
367 struct globlink
*new =
368 (struct globlink
*)xmalloc(sizeof(struct globlink
));
370 new->name
= xmalloc(len
+ ((flags
& GLOB_MARK
) ? 1 : 0) + 1);
371 memcpy((__ptr_t
) new->name
, name
, len
);
372 new->name
[len
] = '\0';
380 #ifdef CONFIG_GLOB_NOCHECK
381 if (nfound
== 0 && (flags
& GLOB_NOCHECK
)) {
382 size_t len
= strlen(pattern
);
384 names
= (struct globlink
*)xmalloc(sizeof(struct globlink
));
387 (char *)xmalloc(len
+ (flags
& GLOB_MARK
? 1 : 0) + 1);
388 memcpy(names
->name
, pattern
, len
);
389 names
->name
[len
] = '\0';
393 = (char **)xrealloc(pglob
->gl_pathv
,
395 ((flags
& GLOB_DOOFFS
) ? pglob
->gl_offs
: 0) +
396 nfound
+ 1) * sizeof(char *));
398 if (flags
& GLOB_DOOFFS
)
399 while (pglob
->gl_pathc
< pglob
->gl_offs
)
400 pglob
->gl_pathv
[pglob
->gl_pathc
++] = NULL
;
403 struct globlink
*tmp
;
405 pglob
->gl_pathv
[pglob
->gl_pathc
++] = names
->name
;
410 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
412 pglob
->gl_flags
= flags
;
416 (void)closedir((DIR *) stream
);
419 return nfound
== 0 ? GLOB_NOMATCH
: 0;
421 #endif /* CONFIG_GLOB */
423 #ifdef CONFIG_FAKE_GLOB
424 /* Fake version of glob. We simply put the input string into
425 * the gl_pathv array. Currently we don't need it as hush.c won't
426 * call us if no glob support is available.
428 int glob(pattern
, flags
, errfunc
, pglob
)
431 int (*errfunc
) __P((const char *, int));
436 if (!(flags
& GLOB_APPEND
)) {
438 pglob
->gl_pathv
= NULL
;
441 elems
= pglob
->gl_pathc
+ 2;
442 if (flags
& GLOB_DOOFFS
)
443 elems
+= pglob
->gl_offs
;
445 pglob
->gl_pathv
= xrealloc(pglob
->gl_pathv
, elems
* sizeof(char *));
447 if (flags
& GLOB_DOOFFS
)
448 for (i
= 0; i
< pglob
->gl_offs
; i
++)
449 pglob
->gl_pathv
[i
] = NULL
;
451 pglob
->gl_pathv
[pglob
->gl_pathc
] = strdup(pattern
);
453 pglob
->gl_pathv
[pglob
->gl_pathc
] = NULL
;
457 #endif /* CONFIG_FAKE_GLOB */
459 /* Free storage allocated in PGLOB by a previous `glob' call. */
461 register glob_t
*pglob
;
463 if (pglob
->gl_pathv
!= NULL
) {
465 pglob
->gl_flags
& GLOB_DOOFFS
? pglob
->gl_offs
: 0;
466 for (; i
< pglob
->gl_pathc
; ++i
)
467 if (pglob
->gl_pathv
[i
] != NULL
)
468 free((__ptr_t
) pglob
->gl_pathv
[i
]);
469 free((__ptr_t
) pglob
->gl_pathv
);