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