Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
13 KiB

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