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.

283 lines
9.2 KiB

  1. package File::Basename;
  2. =head1 NAME
  3. fileparse - split a pathname into pieces
  4. basename - extract just the filename from a path
  5. dirname - extract just the directory from a path
  6. =head1 SYNOPSIS
  7. use File::Basename;
  8. ($name,$path,$suffix) = fileparse($fullname,@suffixlist)
  9. fileparse_set_fstype($os_string);
  10. $basename = basename($fullname,@suffixlist);
  11. $dirname = dirname($fullname);
  12. ($name,$path,$suffix) = fileparse("lib/File/Basename.pm","\.pm");
  13. fileparse_set_fstype("VMS");
  14. $basename = basename("lib/File/Basename.pm",".pm");
  15. $dirname = dirname("lib/File/Basename.pm");
  16. =head1 DESCRIPTION
  17. These routines allow you to parse file specifications into useful
  18. pieces using the syntax of different operating systems.
  19. =over 4
  20. =item fileparse_set_fstype
  21. You select the syntax via the routine fileparse_set_fstype().
  22. If the argument passed to it contains one of the substrings
  23. "VMS", "MSDOS", "MacOS", "AmigaOS" or "MSWin32", the file specification
  24. syntax of that operating system is used in future calls to
  25. fileparse(), basename(), and dirname(). If it contains none of
  26. these substrings, Unix syntax is used. This pattern matching is
  27. case-insensitive. If you've selected VMS syntax, and the file
  28. specification you pass to one of these routines contains a "/",
  29. they assume you are using Unix emulation and apply the Unix syntax
  30. rules instead, for that function call only.
  31. If the argument passed to it contains one of the substrings "VMS",
  32. "MSDOS", "MacOS", "AmigaOS", "os2", "MSWin32" or "RISCOS", then the pattern
  33. matching for suffix removal is performed without regard for case,
  34. since those systems are not case-sensitive when opening existing files
  35. (though some of them preserve case on file creation).
  36. If you haven't called fileparse_set_fstype(), the syntax is chosen
  37. by examining the builtin variable C<$^O> according to these rules.
  38. =item fileparse
  39. The fileparse() routine divides a file specification into three
  40. parts: a leading B<path>, a file B<name>, and a B<suffix>. The
  41. B<path> contains everything up to and including the last directory
  42. separator in the input file specification. The remainder of the input
  43. file specification is then divided into B<name> and B<suffix> based on
  44. the optional patterns you specify in C<@suffixlist>. Each element of
  45. this list is interpreted as a regular expression, and is matched
  46. against the end of B<name>. If this succeeds, the matching portion of
  47. B<name> is removed and prepended to B<suffix>. By proper use of
  48. C<@suffixlist>, you can remove file types or versions for examination.
  49. You are guaranteed that if you concatenate B<path>, B<name>, and
  50. B<suffix> together in that order, the result will denote the same
  51. file as the input file specification.
  52. =back
  53. =head1 EXAMPLES
  54. Using Unix file syntax:
  55. ($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
  56. '\.book\d+');
  57. would yield
  58. $base eq 'draft'
  59. $path eq '/virgil/aeneid/',
  60. $type eq '.book7'
  61. Similarly, using VMS syntax:
  62. ($name,$dir,$type) = fileparse('Doc_Root:[Help]Rhetoric.Rnh',
  63. '\..*');
  64. would yield
  65. $name eq 'Rhetoric'
  66. $dir eq 'Doc_Root:[Help]'
  67. $type eq '.Rnh'
  68. =over
  69. =item C<basename>
  70. The basename() routine returns the first element of the list produced
  71. by calling fileparse() with the same arguments, except that it always
  72. quotes metacharacters in the given suffixes. It is provided for
  73. programmer compatibility with the Unix shell command basename(1).
  74. =item C<dirname>
  75. The dirname() routine returns the directory portion of the input file
  76. specification. When using VMS or MacOS syntax, this is identical to the
  77. second element of the list produced by calling fileparse() with the same
  78. input file specification. (Under VMS, if there is no directory information
  79. in the input file specification, then the current default device and
  80. directory are returned.) When using Unix or MSDOS syntax, the return
  81. value conforms to the behavior of the Unix shell command dirname(1). This
  82. is usually the same as the behavior of fileparse(), but differs in some
  83. cases. For example, for the input file specification F<lib/>, fileparse()
  84. considers the directory name to be F<lib/>, while dirname() considers the
  85. directory name to be F<.>).
  86. =back
  87. =cut
  88. ## use strict;
  89. # A bit of juggling to insure that C<use re 'taint';> always works, since
  90. # File::Basename is used during the Perl build, when the re extension may
  91. # not be available.
  92. BEGIN {
  93. unless (eval { require re; })
  94. { eval ' sub re::import { $^H |= 0x00100000; } ' }
  95. import re 'taint';
  96. }
  97. use 5.005_64;
  98. our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase);
  99. require Exporter;
  100. @ISA = qw(Exporter);
  101. @EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
  102. $VERSION = "2.6";
  103. # fileparse_set_fstype() - specify OS-based rules used in future
  104. # calls to routines in this package
  105. #
  106. # Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
  107. # Any other name uses Unix-style rules and is case-sensitive
  108. sub fileparse_set_fstype {
  109. my @old = ($Fileparse_fstype, $Fileparse_igncase);
  110. if (@_) {
  111. $Fileparse_fstype = $_[0];
  112. $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i);
  113. }
  114. wantarray ? @old : $old[0];
  115. }
  116. # fileparse() - parse file specification
  117. #
  118. # Version 2.4 27-Sep-1996 Charles Bailey [email protected]
  119. sub fileparse {
  120. my($fullname,@suffices) = @_;
  121. my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
  122. my($dirpath,$tail,$suffix,$basename);
  123. my($taint) = substr($fullname,0,0); # Is $fullname tainted?
  124. if ($fstype =~ /^VMS/i) {
  125. if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation
  126. else {
  127. ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s);
  128. $dirpath ||= ''; # should always be defined
  129. }
  130. }
  131. if ($fstype =~ /^MS(DOS|Win32)|epoc/i) {
  132. ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s);
  133. $dirpath .= '.\\' unless $dirpath =~ /[\\\/]\z/;
  134. }
  135. elsif ($fstype =~ /^MacOS/si) {
  136. ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/s);
  137. }
  138. elsif ($fstype =~ /^AmigaOS/i) {
  139. ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s);
  140. $dirpath = './' unless $dirpath;
  141. }
  142. elsif ($fstype !~ /^VMS/i) { # default to Unix
  143. ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
  144. if ($^O eq 'VMS' and $fullname =~ m:^(/[^/]+/000000(/|$))(.*):) {
  145. # dev:[000000] is top of VMS tree, similar to Unix '/'
  146. # so strip it off and treat the rest as "normal"
  147. my $devspec = $1;
  148. my $remainder = $3;
  149. ($dirpath,$basename) = ($remainder =~ m#^(.*/)?(.*)#s);
  150. $dirpath = $devspec.$dirpath;
  151. }
  152. $dirpath = './' unless $dirpath;
  153. }
  154. if (@suffices) {
  155. $tail = '';
  156. foreach $suffix (@suffices) {
  157. my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
  158. if ($basename =~ s/$pat//s) {
  159. $taint .= substr($suffix,0,0);
  160. $tail = $1 . $tail;
  161. }
  162. }
  163. }
  164. $tail .= $taint if defined $tail; # avoid warning if $tail == undef
  165. wantarray ? ($basename . $taint, $dirpath . $taint, $tail)
  166. : $basename . $taint;
  167. }
  168. # basename() - returns first element of list returned by fileparse()
  169. sub basename {
  170. my($name) = shift;
  171. (fileparse($name, map("\Q$_\E",@_)))[0];
  172. }
  173. # dirname() - returns device and directory portion of file specification
  174. # Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
  175. # filespecs except for names ending with a separator, e.g., "/xx/yy/".
  176. # This differs from the second element of the list returned
  177. # by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
  178. # the last directory name if the filespec ends in a '/' or '\'), is lost.
  179. sub dirname {
  180. my($basename,$dirname) = fileparse($_[0]);
  181. my($fstype) = $Fileparse_fstype;
  182. if ($fstype =~ /VMS/i) {
  183. if ($_[0] =~ m#/#) { $fstype = '' }
  184. else { return $dirname || $ENV{DEFAULT} }
  185. }
  186. if ($fstype =~ /MacOS/i) {
  187. if( !length($basename) && $dirname !~ /^[^:]+:\z/) {
  188. $dirname =~ s/([^:]):\z/$1/s;
  189. ($basename,$dirname) = fileparse $dirname;
  190. }
  191. $dirname .= ":" unless $dirname =~ /:\z/;
  192. }
  193. elsif ($fstype =~ /MSDOS/i) {
  194. $dirname =~ s/([^:])[\\\/]*\z/$1/;
  195. unless( length($basename) ) {
  196. ($basename,$dirname) = fileparse $dirname;
  197. $dirname =~ s/([^:])[\\\/]*\z/$1/;
  198. }
  199. }
  200. elsif ($fstype =~ /MSWin32/i) {
  201. $dirname =~ s/([^:])[\\\/]*\z/$1/;
  202. unless( length($basename) ) {
  203. ($basename,$dirname) = fileparse $dirname;
  204. $dirname =~ s/([^:])[\\\/]*\z/$1/;
  205. }
  206. }
  207. elsif ($fstype =~ /AmigaOS/i) {
  208. if ( $dirname =~ /:\z/) { return $dirname }
  209. chop $dirname;
  210. $dirname =~ s#[^:/]+\z## unless length($basename);
  211. }
  212. else {
  213. $dirname =~ s:(.)/*\z:$1:s;
  214. unless( length($basename) ) {
  215. local($File::Basename::Fileparse_fstype) = $fstype;
  216. ($basename,$dirname) = fileparse $dirname;
  217. $dirname =~ s:(.)/*\z:$1:s;
  218. }
  219. }
  220. $dirname;
  221. }
  222. fileparse_set_fstype $^O;
  223. 1;