Commit | Line | Data |
f06db76b |
1 | =head1 NAME |
2 | |
767010ca |
3 | File::Basename - Parse file paths into directory, filename and suffix. |
f06db76b |
4 | |
5 | =head1 SYNOPSIS |
6 | |
7 | use File::Basename; |
8 | |
1c33a35c |
9 | ($name,$path,$suffix) = fileparse($fullname,@suffixlist); |
10 | $name = fileparse($fullname,@suffixlist); |
767010ca |
11 | |
f06db76b |
12 | $basename = basename($fullname,@suffixlist); |
767010ca |
13 | $dirname = dirname($fullname); |
f06db76b |
14 | |
f06db76b |
15 | |
16 | =head1 DESCRIPTION |
17 | |
767010ca |
18 | These routines allow you to parse file paths into their directory, filename |
19 | and suffix. |
f06db76b |
20 | |
6eae9758 |
21 | B<NOTE>: C<dirname()> and C<basename()> emulate the behaviours, and |
22 | quirks, of the shell and C functions of the same name. See each |
3c4b39be |
23 | function's documentation for details. If your concern is just parsing |
6eae9758 |
24 | paths it is safer to use L<File::Spec>'s C<splitpath()> and |
25 | C<splitdir()> methods. |
2ae324a7 |
26 | |
e586b3eb |
27 | It is guaranteed that |
28 | |
29 | # Where $path_separator is / for Unix, \ for Windows, etc... |
30 | dirname($path) . $path_separator . basename($path); |
31 | |
32 | is equivalent to the original path for all systems but VMS. |
33 | |
6eae9758 |
34 | |
f06db76b |
35 | =cut |
36 | |
b3eb6a9b |
37 | |
767010ca |
38 | package File::Basename; |
39 | |
1f47e8e2 |
40 | # A bit of juggling to insure that C<use re 'taint';> always works, since |
918c0b2d |
41 | # File::Basename is used during the Perl build, when the re extension may |
42 | # not be available. |
43 | BEGIN { |
44 | unless (eval { require re; }) |
9cfe5470 |
45 | { eval ' sub re::import { $^H |= 0x00100000; } ' } # HINT_RE_TAINT |
918c0b2d |
46 | import re 'taint'; |
47 | } |
48 | |
49 | |
767010ca |
50 | use strict; |
3b825e41 |
51 | use 5.006; |
b395063c |
52 | use warnings; |
17f410f9 |
53 | our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase); |
a0d0e21e |
54 | require Exporter; |
55 | @ISA = qw(Exporter); |
748a9306 |
56 | @EXPORT = qw(fileparse fileparse_set_fstype basename dirname); |
6eae9758 |
57 | $VERSION = "2.74"; |
7e2183d3 |
58 | |
767010ca |
59 | fileparse_set_fstype($^O); |
a0d0e21e |
60 | |
a0d0e21e |
61 | |
767010ca |
62 | =over 4 |
63 | |
64 | =item C<fileparse> |
65 | |
66 | my($filename, $directories, $suffix) = fileparse($path); |
67 | my($filename, $directories, $suffix) = fileparse($path, @suffixes); |
68 | my $filename = fileparse($path, @suffixes); |
69 | |
70 | The C<fileparse()> routine divides a file path into its $directories, $filename |
71 | and (optionally) the filename $suffix. |
72 | |
73 | $directories contains everything up to and including the last |
74 | directory separator in the $path including the volume (if applicable). |
75 | The remainder of the $path is the $filename. |
76 | |
77 | # On Unix returns ("baz", "/foo/bar/", "") |
78 | fileparse("/foo/bar/baz"); |
79 | |
80 | # On Windows returns ("baz", "C:\foo\bar\", "") |
81 | fileparse("C:\foo\bar\baz"); |
82 | |
83 | # On Unix returns ("", "/foo/bar/baz/", "") |
84 | fileparse("/foo/bar/baz/"); |
85 | |
86 | If @suffixes are given each element is a pattern (either a string or a |
87 | C<qr//>) matched against the end of the $filename. The matching |
88 | portion is removed and becomes the $suffix. |
89 | |
90 | # On Unix returns ("baz", "/foo/bar", ".txt") |
91 | fileparse("/foo/bar/baz", qr/\.[^.]*/); |
92 | |
3291253b |
93 | If type is non-Unix (see C<fileparse_set_fstype()>) then the pattern |
94 | matching for suffix removal is performed case-insensitively, since |
95 | those systems are not case-sensitive when opening existing files. |
767010ca |
96 | |
97 | You are guaranteed that C<$directories . $filename . $suffix> will |
98 | denote the same location as the original $path. |
a0d0e21e |
99 | |
767010ca |
100 | =cut |
a0d0e21e |
101 | |
102 | |
103 | sub fileparse { |
104 | my($fullname,@suffices) = @_; |
3291253b |
105 | |
978ae421 |
106 | unless (defined $fullname) { |
107 | require Carp; |
6286f723 |
108 | Carp::croak("fileparse(): need a valid pathname"); |
978ae421 |
109 | } |
3291253b |
110 | |
111 | my $orig_type = ''; |
112 | my($type,$igncase) = ($Fileparse_fstype, $Fileparse_igncase); |
113 | |
12cbd720 |
114 | my($taint) = substr($fullname,0,0); # Is $fullname tainted? |
a0d0e21e |
115 | |
3291253b |
116 | if ($type eq "VMS" and $fullname =~ m{/} ) { |
117 | # We're doing Unix emulation |
118 | $orig_type = $type; |
119 | $type = 'Unix'; |
a0d0e21e |
120 | } |
3291253b |
121 | |
122 | my($dirpath, $basename); |
123 | |
124 | if (grep { $type eq $_ } qw(MSDOS DOS MSWin32 Epoc)) { |
c7b9dd21 |
125 | ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s); |
126 | $dirpath .= '.\\' unless $dirpath =~ /[\\\/]\z/; |
a0d0e21e |
127 | } |
3291253b |
128 | elsif ($type eq "OS2") { |
f1e20921 |
129 | ($dirpath,$basename) = ($fullname =~ m#^((?:.*[:\\/])?)(.*)#s); |
130 | $dirpath = './' unless $dirpath; # Can't be 0 |
131 | $dirpath .= '/' unless $dirpath =~ m#[\\/]\z#; |
132 | } |
3291253b |
133 | elsif ($type eq "MacOS") { |
c7b9dd21 |
134 | ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/s); |
95e8664e |
135 | $dirpath = ':' unless $dirpath; |
a0d0e21e |
136 | } |
3291253b |
137 | elsif ($type eq "AmigaOS") { |
c7b9dd21 |
138 | ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s); |
a3156fc3 |
139 | $dirpath = './' unless $dirpath; |
55497cff |
140 | } |
3291253b |
141 | elsif ($type eq 'VMS' ) { |
142 | ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s); |
143 | $dirpath ||= ''; # should always be defined |
144 | } |
145 | else { # Default to Unix semantics. |
c7b9dd21 |
146 | ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s); |
3291253b |
147 | if ($orig_type eq 'VMS' and $fullname =~ m:^(/[^/]+/000000(/|$))(.*):) { |
491527d0 |
148 | # dev:[000000] is top of VMS tree, similar to Unix '/' |
e3830a4e |
149 | # so strip it off and treat the rest as "normal" |
150 | my $devspec = $1; |
151 | my $remainder = $3; |
152 | ($dirpath,$basename) = ($remainder =~ m#^(.*/)?(.*)#s); |
5fa137f1 |
153 | $dirpath ||= ''; # should always be defined |
e3830a4e |
154 | $dirpath = $devspec.$dirpath; |
491527d0 |
155 | } |
f0c6ccdf |
156 | $dirpath = './' unless $dirpath; |
a0d0e21e |
157 | } |
3291253b |
158 | |
a0d0e21e |
159 | |
08ea998e |
160 | my $tail = ''; |
161 | my $suffix = ''; |
a0d0e21e |
162 | if (@suffices) { |
163 | foreach $suffix (@suffices) { |
ee2ff9ea |
164 | my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$"; |
c7b9dd21 |
165 | if ($basename =~ s/$pat//s) { |
12cbd720 |
166 | $taint .= substr($suffix,0,0); |
44a8e56a |
167 | $tail = $1 . $tail; |
a0d0e21e |
168 | } |
169 | } |
170 | } |
171 | |
767010ca |
172 | # Ensure taint is propgated from the path to its pieces. |
08ea998e |
173 | $tail .= $taint; |
8d6d96c1 |
174 | wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail) |
d2ccd3cb |
175 | : ($basename .= $taint); |
a0d0e21e |
176 | } |
177 | |
178 | |
767010ca |
179 | |
180 | =item C<basename> |
181 | |
182 | my $filename = basename($path); |
183 | my $filename = basename($path, @suffixes); |
184 | |
e586b3eb |
185 | This function is provided for compatibility with the Unix shell command |
186 | C<basename(1)>. It does B<NOT> always return the file name portion of a |
187 | path as you might expect. To be safe, if you want the file name portion of |
188 | a path use C<fileparse()>. |
189 | |
190 | C<basename()> returns the last level of a filepath even if the last |
191 | level is clearly directory. In effect, it is acting like C<pop()> for |
192 | paths. This differs from C<fileparse()>'s behaviour. |
193 | |
194 | # Both return "bar" |
195 | basename("/foo/bar"); |
196 | basename("/foo/bar/"); |
197 | |
198 | @suffixes work as in C<fileparse()> except all regex metacharacters are |
199 | quoted. |
767010ca |
200 | |
201 | # These two function calls are equivalent. |
202 | my $filename = basename("/foo/bar/baz.txt", ".txt"); |
203 | my $filename = fileparse("/foo/bar/baz.txt", qr/\Q.txt\E/); |
204 | |
08ea998e |
205 | Also note that in order to be compatible with the shell command, |
206 | C<basename()> does not strip off a suffix if it is identical to the |
207 | remaining characters in the filename. |
208 | |
767010ca |
209 | =cut |
210 | |
a0d0e21e |
211 | |
212 | sub basename { |
08bc7695 |
213 | my($path) = shift; |
214 | |
08ea998e |
215 | # From BSD basename(1) |
216 | # The basename utility deletes any prefix ending with the last slash `/' |
217 | # character present in string (after first stripping trailing slashes) |
08bc7695 |
218 | _strip_trailing_sep($path); |
08ea998e |
219 | |
220 | my($basename, $dirname, $suffix) = fileparse( $path, map("\Q$_\E",@_) ); |
221 | |
222 | # From BSD basename(1) |
223 | # The suffix is not stripped if it is identical to the remaining |
224 | # characters in string. |
225 | if( length $suffix and !length $basename ) { |
226 | $basename = $suffix; |
227 | } |
228 | |
229 | # Ensure that basename '/' == '/' |
230 | if( !length $basename ) { |
231 | $basename = $dirname; |
232 | } |
08bc7695 |
233 | |
234 | return $basename; |
a0d0e21e |
235 | } |
7e2183d3 |
236 | |
a0d0e21e |
237 | |
767010ca |
238 | |
239 | =item C<dirname> |
240 | |
241 | This function is provided for compatibility with the Unix shell |
242 | command C<dirname(1)> and has inherited some of its quirks. In spite of |
243 | its name it does B<NOT> always return the directory name as you might |
244 | expect. To be safe, if you want the directory name of a path use |
245 | C<fileparse()>. |
246 | |
3291253b |
247 | Only on VMS (where there is no ambiguity between the file and directory |
248 | portions of a path) and AmigaOS (possibly due to an implementation quirk in |
249 | this module) does C<dirname()> work like C<fileparse($path)>, returning just the |
250 | $directories. |
767010ca |
251 | |
3291253b |
252 | # On VMS and AmigaOS |
253 | my $directories = dirname($path); |
767010ca |
254 | |
255 | When using Unix or MSDOS syntax this emulates the C<dirname(1)> shell function |
256 | which is subtly different from how C<fileparse()> works. It returns all but |
257 | the last level of a file path even if the last level is clearly a directory. |
258 | In effect, it is not returning the directory portion but simply the path one |
259 | level up acting like C<chop()> for file paths. |
260 | |
261 | Also unlike C<fileparse()>, C<dirname()> does not include a trailing slash on |
262 | its returned path. |
263 | |
264 | # returns /foo/bar. fileparse() would return /foo/bar/ |
265 | dirname("/foo/bar/baz"); |
266 | |
267 | # also returns /foo/bar despite the fact that baz is clearly a |
268 | # directory. fileparse() would return /foo/bar/baz/ |
269 | dirname("/foo/bar/baz/"); |
270 | |
271 | # returns '.'. fileparse() would return 'foo/' |
272 | dirname("foo/"); |
273 | |
274 | Under VMS, if there is no directory information in the $path, then the |
275 | current default device and directory is used. |
276 | |
277 | =cut |
278 | |
a0d0e21e |
279 | |
280 | sub dirname { |
3291253b |
281 | my $path = shift; |
a0d0e21e |
282 | |
3291253b |
283 | my($type) = $Fileparse_fstype; |
284 | |
285 | if( $type eq 'VMS' and $path =~ m{/} ) { |
767010ca |
286 | # Parse as Unix |
287 | local($File::Basename::Fileparse_fstype) = ''; |
3291253b |
288 | return dirname($path); |
767010ca |
289 | } |
290 | |
3291253b |
291 | my($basename, $dirname) = fileparse($path); |
767010ca |
292 | |
3291253b |
293 | if ($type eq 'VMS') { |
767010ca |
294 | $dirname ||= $ENV{DEFAULT}; |
a0d0e21e |
295 | } |
3291253b |
296 | elsif ($type eq 'MacOS') { |
084592ab |
297 | if( !length($basename) && $dirname !~ /^[^:]+:\z/) { |
e586b3eb |
298 | _strip_trailing_sep($dirname); |
084592ab |
299 | ($basename,$dirname) = fileparse $dirname; |
300 | } |
301 | $dirname .= ":" unless $dirname =~ /:\z/; |
302 | } |
3291253b |
303 | elsif (grep { $type eq $_ } qw(MSDOS DOS MSWin32 OS2)) { |
e586b3eb |
304 | _strip_trailing_sep($dirname); |
68dc0745 |
305 | unless( length($basename) ) { |
306 | ($basename,$dirname) = fileparse $dirname; |
e586b3eb |
307 | _strip_trailing_sep($dirname); |
68dc0745 |
308 | } |
309 | } |
3291253b |
310 | elsif ($type eq 'AmigaOS') { |
c7b9dd21 |
311 | if ( $dirname =~ /:\z/) { return $dirname } |
55497cff |
312 | chop $dirname; |
c7b9dd21 |
313 | $dirname =~ s#[^:/]+\z## unless length($basename); |
55497cff |
314 | } |
084592ab |
315 | else { |
e586b3eb |
316 | _strip_trailing_sep($dirname); |
42568e28 |
317 | unless( length($basename) ) { |
42568e28 |
318 | ($basename,$dirname) = fileparse $dirname; |
e586b3eb |
319 | _strip_trailing_sep($dirname); |
42568e28 |
320 | } |
a0d0e21e |
321 | } |
322 | |
323 | $dirname; |
324 | } |
325 | |
767010ca |
326 | |
e586b3eb |
327 | # Strip the trailing path separator. |
328 | sub _strip_trailing_sep { |
329 | my $type = $Fileparse_fstype; |
330 | |
331 | if ($type eq 'MacOS') { |
332 | $_[0] =~ s/([^:]):\z/$1/s; |
333 | } |
334 | elsif (grep { $type eq $_ } qw(MSDOS DOS MSWin32 OS2)) { |
335 | $_[0] =~ s/([^:])[\\\/]*\z/$1/; |
336 | } |
337 | else { |
338 | $_[0] =~ s{(.)/*\z}{$1}s; |
339 | } |
340 | } |
341 | |
342 | |
767010ca |
343 | =item C<fileparse_set_fstype> |
344 | |
3291253b |
345 | my $type = fileparse_set_fstype(); |
346 | my $previous_type = fileparse_set_fstype($type); |
767010ca |
347 | |
348 | Normally File::Basename will assume a file path type native to your current |
349 | operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...). |
350 | With this function you can override that assumption. |
351 | |
3291253b |
352 | Valid $types are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS", |
353 | "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility), |
354 | "Epoc" and "Unix" (all case-insensitive). If an unrecognized $type is |
355 | given "Unix" will be assumed. |
767010ca |
356 | |
357 | If you've selected VMS syntax, and the file specification you pass to |
358 | one of these routines contains a "/", they assume you are using Unix |
359 | emulation and apply the Unix syntax rules instead, for that function |
360 | call only. |
361 | |
362 | =back |
363 | |
364 | =cut |
365 | |
366 | |
3291253b |
367 | BEGIN { |
368 | |
369 | my @Ignore_Case = qw(MacOS VMS AmigaOS OS2 RISCOS MSWin32 MSDOS DOS Epoc); |
370 | my @Types = (@Ignore_Case, qw(Unix)); |
371 | |
767010ca |
372 | sub fileparse_set_fstype { |
3291253b |
373 | my $old = $Fileparse_fstype; |
374 | |
375 | if (@_) { |
376 | my $new_type = shift; |
377 | |
378 | $Fileparse_fstype = 'Unix'; # default |
379 | foreach my $type (@Types) { |
380 | $Fileparse_fstype = $type if $new_type =~ /^$type/i; |
381 | } |
382 | |
383 | $Fileparse_igncase = |
384 | (grep $Fileparse_fstype eq $_, @Ignore_Case) ? 1 : 0; |
385 | } |
386 | |
387 | return $old; |
388 | } |
389 | |
767010ca |
390 | } |
391 | |
a0d0e21e |
392 | |
393 | 1; |
6eae9758 |
394 | |
395 | |
396 | =head1 SEE ALSO |
397 | |
398 | L<dirname(1)>, L<basename(1)>, L<File::Spec> |