grrr.
[p5sagit/p5-mst-13.2.git] / ext / File / Glob / Glob.pm
CommitLineData
72b16652 1package File::Glob;
2
3use strict;
17f410f9 4our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS,
5 $AUTOLOAD, $DEFAULT_FLAGS);
72b16652 6
9426adcd 7use XSLoader ();
72b16652 8
72f7b9a1 9@ISA = qw(Exporter);
72b16652 10
00c80938 11# NOTE: The glob() export is only here for compatibility with 5.6.0.
12# csh_glob() should not be used directly, unless you know what you're doing.
13
72b16652 14@EXPORT_OK = qw(
72b16652 15 csh_glob
00c80938 16 bsd_glob
72b16652 17 glob
18 GLOB_ABEND
2d5e9e5d 19 GLOB_ALPHASORT
72b16652 20 GLOB_ALTDIRFUNC
21 GLOB_BRACE
220398a0 22 GLOB_CSH
72b16652 23 GLOB_ERR
24 GLOB_ERROR
b8ef571c 25 GLOB_LIMIT
72b16652 26 GLOB_MARK
220398a0 27 GLOB_NOCASE
72b16652 28 GLOB_NOCHECK
29 GLOB_NOMAGIC
30 GLOB_NOSORT
31 GLOB_NOSPACE
32 GLOB_QUOTE
33 GLOB_TILDE
34);
35
72b16652 36%EXPORT_TAGS = (
37 'glob' => [ qw(
38 GLOB_ABEND
2d5e9e5d 39 GLOB_ALPHASORT
72b16652 40 GLOB_ALTDIRFUNC
41 GLOB_BRACE
220398a0 42 GLOB_CSH
72b16652 43 GLOB_ERR
44 GLOB_ERROR
b8ef571c 45 GLOB_LIMIT
72b16652 46 GLOB_MARK
220398a0 47 GLOB_NOCASE
72b16652 48 GLOB_NOCHECK
49 GLOB_NOMAGIC
50 GLOB_NOSORT
51 GLOB_NOSPACE
52 GLOB_QUOTE
53 GLOB_TILDE
54 glob
00c80938 55 bsd_glob
72b16652 56 ) ],
57);
58
72f7b9a1 59$VERSION = '1.01';
220398a0 60
61sub import {
7d3fb230 62 require Exporter;
220398a0 63 my $i = 1;
64 while ($i < @_) {
65 if ($_[$i] =~ /^:(case|nocase|globally)$/) {
66 splice(@_, $i, 1);
67 $DEFAULT_FLAGS &= ~GLOB_NOCASE() if $1 eq 'case';
68 $DEFAULT_FLAGS |= GLOB_NOCASE() if $1 eq 'nocase';
69 if ($1 eq 'globally') {
7d3fb230 70 local $^W;
220398a0 71 *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
72 }
73 next;
74 }
75 ++$i;
72b16652 76 }
220398a0 77 goto &Exporter::import;
72b16652 78}
79
80sub AUTOLOAD {
81 # This AUTOLOAD is used to 'autoload' constants from the constant()
82 # XS function. If a constant is not found then control is passed
83 # to the AUTOLOAD in AutoLoader.
84
85 my $constname;
86 ($constname = $AUTOLOAD) =~ s/.*:://;
72f7b9a1 87 my ($error, $val) = constant($constname);
88 if ($error) {
89 require Carp;
90 Carp::croak($error);
72b16652 91 }
92 eval "sub $AUTOLOAD { $val }";
93 goto &$AUTOLOAD;
94}
95
9426adcd 96XSLoader::load 'File::Glob', $VERSION;
72b16652 97
98# Preloaded methods go here.
99
100sub GLOB_ERROR {
72f7b9a1 101 return (constant('GLOB_ERROR'))[1];
72b16652 102}
103
2d5e9e5d 104sub GLOB_CSH () {
105 GLOB_BRACE()
106 | GLOB_NOMAGIC()
107 | GLOB_QUOTE()
108 | GLOB_TILDE()
109 | GLOB_ALPHASORT()
110}
72b16652 111
220398a0 112$DEFAULT_FLAGS = GLOB_CSH();
113if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos|MacOS)$/) {
114 $DEFAULT_FLAGS |= GLOB_NOCASE();
115}
116
72b16652 117# Autoload methods go after =cut, and are processed by the autosplit program.
118
00c80938 119sub bsd_glob {
f0963acb 120 my ($pat,$flags) = @_;
121 $flags = $DEFAULT_FLAGS if @_ < 2;
122 return doglob($pat,$flags);
72b16652 123}
124
00c80938 125# File::Glob::glob() is deprecated because its prototype is different from
126# CORE::glob() (use bsd_glob() instead)
127sub glob {
128 goto &bsd_glob;
129}
130
72b16652 131## borrowed heavily from gsar's File::DosGlob
132my %iter;
133my %entries;
134
135sub csh_glob {
136 my $pat = shift;
137 my $cxix = shift;
138 my @pat;
139
140 # glob without args defaults to $_
141 $pat = $_ unless defined $pat;
142
143 # extract patterns
be3174d2 144 $pat =~ s/^\s+//; # Protect against empty elements in
145 $pat =~ s/\s+$//; # things like < *.c> and <*.c >.
146 # These alone shouldn't trigger ParseWords.
72b16652 147 if ($pat =~ /\s/) {
148 # XXX this is needed for compatibility with the csh
149 # implementation in Perl. Need to support a flag
150 # to disable this behavior.
151 require Text::ParseWords;
152 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
153 }
154
155 # assume global context if not provided one
156 $cxix = '_G_' unless defined $cxix;
157 $iter{$cxix} = 0 unless exists $iter{$cxix};
158
159 # if we're just beginning, do it all first
160 if ($iter{$cxix} == 0) {
161 if (@pat) {
220398a0 162 $entries{$cxix} = [ map { doglob($_, $DEFAULT_FLAGS) } @pat ];
72b16652 163 }
164 else {
220398a0 165 $entries{$cxix} = [ doglob($pat, $DEFAULT_FLAGS) ];
72b16652 166 }
167 }
168
169 # chuck it all out, quick or slow
170 if (wantarray) {
171 delete $iter{$cxix};
172 return @{delete $entries{$cxix}};
173 }
174 else {
175 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
176 return shift @{$entries{$cxix}};
177 }
178 else {
179 # return undef for EOL
180 delete $iter{$cxix};
181 delete $entries{$cxix};
182 return undef;
183 }
184 }
185}
186
1871;
188__END__
189
190=head1 NAME
191
192File::Glob - Perl extension for BSD glob routine
193
194=head1 SYNOPSIS
195
196 use File::Glob ':glob';
00c80938 197 @list = bsd_glob('*.[ch]');
198 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
72b16652 199 if (GLOB_ERROR) {
200 # an error occurred reading $homedir
201 }
202
00c80938 203 ## override the core glob (CORE::glob() does this automatically
11fe14b1 204 ## by default anyway, since v5.6.0)
220398a0 205 use File::Glob ':globally';
206 my @sources = <*.{c,h,y}>
207
208 ## override the core glob, forcing case sensitivity
209 use File::Glob qw(:globally :case);
210 my @sources = <*.{c,h,y}>
211
212 ## override the core glob forcing case insensitivity
213 use File::Glob qw(:globally :nocase);
72b16652 214 my @sources = <*.{c,h,y}>
215
216=head1 DESCRIPTION
217
00c80938 218File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
219a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
220bsd_glob() takes a mandatory C<pattern> argument, and an optional
72b16652 221C<flags> argument, and returns a list of filenames matching the
222pattern, with interpretation of the pattern modified by the C<flags>
00c80938 223variable.
224
225Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
226Note that they don't share the same prototype--CORE::glob() only accepts
227a single argument. Due to historical reasons, CORE::glob() will also
228split its argument on whitespace, treating it as multiple patterns,
229whereas bsd_glob() considers them as one pattern.
230
231The POSIX defined flags for bsd_glob() are:
72b16652 232
233=over 4
234
235=item C<GLOB_ERR>
236
00c80938 237Force bsd_glob() to return an error when it encounters a directory it
238cannot open or read. Ordinarily bsd_glob() continues to find matches.
72b16652 239
b8ef571c 240=item C<GLOB_LIMIT>
241
242Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern expands
243to a size bigger than the system constant C<ARG_MAX> (usually found in
244limits.h). If your system does not define this constant, bsd_glob() uses
245C<sysconf(_SC_ARG_MAX)> or C<_POSIX_ARG_MAX> where available (in that
246order). You can inspect these values using the standard C<POSIX>
247extension.
248
72b16652 249=item C<GLOB_MARK>
250
251Each pathname that is a directory that matches the pattern has a slash
252appended.
253
220398a0 254=item C<GLOB_NOCASE>
255
256By default, file names are assumed to be case sensitive; this flag
00c80938 257makes bsd_glob() treat case differences as not significant.
220398a0 258
72b16652 259=item C<GLOB_NOCHECK>
260
00c80938 261If the pattern does not match any pathname, then bsd_glob() returns a list
72b16652 262consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect
263is present in the pattern returned.
264
265=item C<GLOB_NOSORT>
266
267By default, the pathnames are sorted in ascending ASCII order; this
00c80938 268flag prevents that sorting (speeding up bsd_glob()).
72b16652 269
270=back
271
272The FreeBSD extensions to the POSIX standard are the following flags:
273
274=over 4
275
276=item C<GLOB_BRACE>
277
a45bd81d 278Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
72b16652 279The pattern '{}' is left unexpanded for historical reasons (and csh(1)
280does the same thing to ease typing of find(1) patterns).
281
282=item C<GLOB_NOMAGIC>
283
284Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not
285contain any of the special characters "*", "?" or "[". C<NOMAGIC> is
286provided to simplify implementing the historic csh(1) globbing
287behaviour and should probably not be used anywhere else.
288
289=item C<GLOB_QUOTE>
290
291Use the backslash ('\') character for quoting: every occurrence of a
292backslash followed by a character in the pattern is replaced by that
293character, avoiding any special interpretation of the character.
220398a0 294(But see below for exceptions on DOSISH systems).
72b16652 295
296=item C<GLOB_TILDE>
297
298Expand patterns that start with '~' to user name home directories.
299
300=item C<GLOB_CSH>
301
302For convenience, C<GLOB_CSH> is a synonym for
2d5e9e5d 303C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>.
72b16652 304
305=back
306
307The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
308extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
309implemented in the Perl version because they involve more complex
310interaction with the underlying C structures.
311
2d5e9e5d 312The following flag has been added in the Perl implementation for
313csh compatibility:
314
315=over 4
316
317=item C<GLOB_ALPHASORT>
318
319If C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical
320order (case does not matter) rather than in ASCII order.
321
322=back
323
72b16652 324=head1 DIAGNOSTICS
325
00c80938 326bsd_glob() returns a list of matching paths, possibly zero length. If an
72b16652 327error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
328set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
329or one of the following values otherwise:
330
331=over 4
332
333=item C<GLOB_NOSPACE>
334
335An attempt to allocate memory failed.
336
337=item C<GLOB_ABEND>
338
339The glob was stopped because an error was encountered.
340
341=back
342
00c80938 343In the case where bsd_glob() has found some matching paths, but is
344interrupted by an error, it will return a list of filenames B<and>
72b16652 345set &File::Glob::ERROR.
346
00c80938 347Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
348by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
72b16652 349continue processing despite those errors, unless the C<GLOB_ERR> flag is
350set.
351
352Be aware that all filenames returned from File::Glob are tainted.
353
354=head1 NOTES
355
356=over 4
357
358=item *
359
00c80938 360If you want to use multiple patterns, e.g. C<bsd_glob "a* b*">, you should
150b260b 361probably throw them in a set as in C<bsd_glob "{a*,b*}">. This is because
362the argument to bsd_glob() isn't subjected to parsing by the C shell.
363Remember that you can use a backslash to escape things.
72b16652 364
365=item *
366
220398a0 367On DOSISH systems, backslash is a valid directory separator character.
368In this case, use of backslash as a quoting character (via GLOB_QUOTE)
369interferes with the use of backslash as a directory separator. The
370best (simplest, most portable) solution is to use forward slashes for
371directory separators, and backslashes for quoting. However, this does
372not match "normal practice" on these systems. As a concession to user
373expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
374glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
375All other backslashes are passed through unchanged.
376
377=item *
378
72b16652 379Win32 users should use the real slash. If you really want to use
380backslashes, consider using Sarathy's File::DosGlob, which comes with
381the standard Perl distribution.
382
7369a524 383=item *
384
385Mac OS (Classic) users should note a few differences. Since
386Mac OS is not Unix, when the glob code encounters a tilde glob (e.g.
387~user/foo) and the C<GLOB_TILDE> flag is used, it simply returns that
388pattern without doing any expansion.
389
390Glob on Mac OS is case-insensitive by default (if you don't use any
391flags). If you specify any flags at all and still want glob
392to be case-insensitive, you must include C<GLOB_NOCASE> in the flags.
393
394The path separator is ':' (aka colon), not '/' (aka slash). Mac OS users
395should be careful about specifying relative pathnames. While a full path
396always begins with a volume name, a relative pathname should always
397begin with a ':'. If specifying a volume name only, a trailing ':' is
398required.
399
a45bd81d 400=back
401
72b16652 402=head1 AUTHOR
403
0e950d83 404The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
72b16652 405and is released under the artistic license. Further modifications were
7369a524 406made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy
407E<lt>gsar@activestate.comE<gt>, and Thomas Wegner
408E<lt>wegner_thomas@yahoo.comE<gt>. The C glob code has the
72b16652 409following copyright:
410
0e950d83 411 Copyright (c) 1989, 1993 The Regents of the University of California.
412 All rights reserved.
3cb6de81 413
0e950d83 414 This code is derived from software contributed to Berkeley by
415 Guido van Rossum.
416
417 Redistribution and use in source and binary forms, with or without
418 modification, are permitted provided that the following conditions
419 are met:
420
421 1. Redistributions of source code must retain the above copyright
422 notice, this list of conditions and the following disclaimer.
423 2. Redistributions in binary form must reproduce the above copyright
424 notice, this list of conditions and the following disclaimer in the
425 documentation and/or other materials provided with the distribution.
426 3. Neither the name of the University nor the names of its contributors
427 may be used to endorse or promote products derived from this software
428 without specific prior written permission.
429
430 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
431 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
432 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
433 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
434 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
435 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
436 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
437 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
438 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
439 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
440 SUCH DAMAGE.
72b16652 441
442=cut