Source code of Windows XP (NT5)
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.

263 lines
8.3 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. use re 'taint';
  90. require Exporter;
  91. @ISA = qw(Exporter);
  92. @EXPORT = qw(fileparse fileparse_set_fstype basename dirname);
  93. use vars qw($VERSION $Fileparse_fstype $Fileparse_igncase);
  94. $VERSION = "2.6";
  95. # fileparse_set_fstype() - specify OS-based rules used in future
  96. # calls to routines in this package
  97. #
  98. # Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS
  99. # Any other name uses Unix-style rules and is case-sensitive
  100. sub fileparse_set_fstype {
  101. my @old = ($Fileparse_fstype, $Fileparse_igncase);
  102. if (@_) {
  103. $Fileparse_fstype = $_[0];
  104. $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i);
  105. }
  106. wantarray ? @old : $old[0];
  107. }
  108. # fileparse() - parse file specification
  109. #
  110. # Version 2.4 27-Sep-1996 Charles Bailey [email protected]
  111. sub fileparse {
  112. my($fullname,@suffices) = @_;
  113. my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase);
  114. my($dirpath,$tail,$suffix,$basename);
  115. my($taint) = substr($fullname,0,0); # Is $fullname tainted?
  116. if ($fstype =~ /^VMS/i) {
  117. if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation
  118. else {
  119. ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/);
  120. $dirpath ||= ''; # should always be defined
  121. }
  122. }
  123. if ($fstype =~ /^MS(DOS|Win32)/i) {
  124. ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/);
  125. $dirpath .= '.\\' unless $dirpath =~ /[\\\/]$/;
  126. }
  127. elsif ($fstype =~ /^MacOS/i) {
  128. ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/);
  129. }
  130. elsif ($fstype =~ /^AmigaOS/i) {
  131. ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/);
  132. $dirpath = './' unless $dirpath;
  133. }
  134. elsif ($fstype !~ /^VMS/i) { # default to Unix
  135. ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#);
  136. if ($^O eq 'VMS' and $fullname =~ m:/[^/]+/000000/?:) {
  137. # dev:[000000] is top of VMS tree, similar to Unix '/'
  138. ($basename,$dirpath) = ('',$fullname);
  139. }
  140. $dirpath = './' unless $dirpath;
  141. }
  142. if (@suffices) {
  143. $tail = '';
  144. foreach $suffix (@suffices) {
  145. my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
  146. if ($basename =~ s/$pat//) {
  147. $taint .= substr($suffix,0,0);
  148. $tail = $1 . $tail;
  149. }
  150. }
  151. }
  152. $tail .= $taint if defined $tail; # avoid warning if $tail == undef
  153. wantarray ? ($basename . $taint, $dirpath . $taint, $tail)
  154. : $basename . $taint;
  155. }
  156. # basename() - returns first element of list returned by fileparse()
  157. sub basename {
  158. my($name) = shift;
  159. (fileparse($name, map("\Q$_\E",@_)))[0];
  160. }
  161. # dirname() - returns device and directory portion of file specification
  162. # Behavior matches that of Unix dirname(1) exactly for Unix and MSDOS
  163. # filespecs except for names ending with a separator, e.g., "/xx/yy/".
  164. # This differs from the second element of the list returned
  165. # by fileparse() in that the trailing '/' (Unix) or '\' (MSDOS) (and
  166. # the last directory name if the filespec ends in a '/' or '\'), is lost.
  167. sub dirname {
  168. my($basename,$dirname) = fileparse($_[0]);
  169. my($fstype) = $Fileparse_fstype;
  170. if ($fstype =~ /VMS/i) {
  171. if ($_[0] =~ m#/#) { $fstype = '' }
  172. else { return $dirname || $ENV{DEFAULT} }
  173. }
  174. if ($fstype =~ /MacOS/i) { return $dirname }
  175. elsif ($fstype =~ /MSDOS/i) {
  176. $dirname =~ s/([^:])[\\\/]*$/$1/;
  177. unless( length($basename) ) {
  178. ($basename,$dirname) = fileparse $dirname;
  179. $dirname =~ s/([^:])[\\\/]*$/$1/;
  180. }
  181. }
  182. elsif ($fstype =~ /MSWin32/i) {
  183. $dirname =~ s/([^:])[\\\/]*$/$1/;
  184. unless( length($basename) ) {
  185. ($basename,$dirname) = fileparse $dirname;
  186. $dirname =~ s/([^:])[\\\/]*$/$1/;
  187. }
  188. }
  189. elsif ($fstype =~ /AmigaOS/i) {
  190. if ( $dirname =~ /:$/) { return $dirname }
  191. chop $dirname;
  192. $dirname =~ s#[^:/]+$## unless length($basename);
  193. }
  194. else {
  195. $dirname =~ s:(.)/*$:$1:;
  196. unless( length($basename) ) {
  197. local($File::Basename::Fileparse_fstype) = $fstype;
  198. ($basename,$dirname) = fileparse $dirname;
  199. $dirname =~ s:(.)/*$:$1:;
  200. }
  201. }
  202. $dirname;
  203. }
  204. fileparse_set_fstype $^O;
  205. 1;