1 package File::Spec::Unix;
12 File::Spec::Unix - File::Spec for Unix, base for other File::Spec modules
16 require File::Spec::Unix; # Done automatically by File::Spec
20 Methods for manipulating file specifications. Other File::Spec
21 modules, such as File::Spec::Mac, inherit from File::Spec::Unix and
22 override specific methods.
30 No physical check on the filesystem, but a logical cleanup of a
31 path. On UNIX eliminates successive slashes and successive "/.".
33 $cpath = File::Spec->canonpath( $path ) ;
38 my ($self,$path) = @_;
40 # Handle POSIX-style node names beginning with double slash
42 if ( $^O =~ m/^(?:qnx|nto)$/ && $path =~ s:^(//[^/]+)(/|\z):/:s ) {
46 # $path =~ s|/+|/|g unless($^O eq 'cygwin');
47 # but that made tests 29, 30, 35, 46, and 213 (as of #13272) to fail
48 # (Mainly because trailing "" directories didn't get stripped).
49 # Why would cygwin avoid collapsing multiple slashes into one? --jhi
50 $path =~ s|/+|/|g; # xx////xx -> xx/xx
51 $path =~ s@(/\.)+(/|\Z(?!\n))@/@g; # xx/././xx -> xx/xx
52 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
53 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
54 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
60 Concatenate two or more directory names to form a complete path ending
61 with a directory. But remove the trailing slash from the resulting
62 string, because it doesn't look good, isn't necessary and confuses
63 OS2. Of course, if this is the root directory, don't cut off the
72 # append a slash to each argument unless it has one there
73 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
75 return $self->canonpath(join('', @args));
80 Concatenate one or more directory names and a filename to form a
81 complete path ending with a filename
88 return $file unless @_;
89 my $dir = $self->catdir(@_);
90 $dir .= "/" unless substr($dir,-1) eq "/";
96 Returns a string representation of the current directory. "." on UNIX.
106 Returns a string representation of the null device. "/dev/null" on UNIX.
116 Returns a string representation of the root directory. "/" on UNIX.
126 Returns a string representation of the first writable directory
127 from the following list or "" if none are writable:
132 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
133 is tainted, it is not used.
139 return $tmpdir if defined $tmpdir;
140 my @dirlist = ($ENV{TMPDIR}, "/tmp");
143 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
144 require Scalar::Util;
145 shift @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
149 next unless defined && -d && -w _;
153 $tmpdir = '' unless defined $tmpdir;
159 Returns a string representation of the parent directory. ".." on UNIX.
169 Given a list of file names, strip out those that refer to a parent
170 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
176 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
181 Returns a true or false value indicating, respectively, that alphabetic
182 is not or is significant when comparing file specifications.
190 =item file_name_is_absolute
192 Takes as argument a path and returns true if it is an absolute path.
194 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
195 OS (Classic). It does consult the working environment for VMS (see
196 L<File::Spec::VMS/file_name_is_absolute>).
200 sub file_name_is_absolute {
201 my ($self,$file) = @_;
202 return scalar($file =~ m:^/:s);
207 Takes no argument, returns the environment variable PATH as an array.
212 my @path = split(':', $ENV{PATH});
213 foreach (@path) { $_ = '.' if $_ eq '' }
219 join is the same as catfile.
225 return $self->catfile(@_);
230 ($volume,$directories,$file) = File::Spec->splitpath( $path );
231 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
233 Splits a path in to volume, directory, and filename portions. On systems
234 with no concept of volume, returns undef for volume.
236 For systems with no syntax differentiating filenames from directories,
237 assumes that the last file is a path unless $no_file is true or a
238 trailing separator or /. or /.. is present. On Unix this means that $no_file
239 true makes this return ( '', $path, '' ).
241 The directory portion may or may not be returned with a trailing '/'.
243 The results can be passed to L</catpath()> to get back a path equivalent to
244 (usually identical to) the original path.
249 my ($self,$path, $nofile) = @_;
251 my ($volume,$directory,$file) = ('','','');
257 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
262 return ($volume,$directory,$file);
268 The opposite of L</catdir()>.
270 @dirs = File::Spec->splitdir( $directories );
272 $directories must be only the directory portion of the path on systems
273 that have the concept of a volume or that have path syntax that differentiates
274 files from directories.
276 Unlike just splitting the directories on the separator, empty
277 directory names (C<''>) can be returned, because these are significant
282 File::Spec->splitdir( "/a/b//c/" );
286 ( '', 'a', 'b', '', 'c', '' )
291 my ($self,$directories) = @_ ;
293 # split() likes to forget about trailing null fields, so here we
294 # check to be sure that there will not be any before handling the
297 if ( $directories !~ m|/\Z(?!\n)| ) {
298 return split( m|/|, $directories );
302 # since there was a trailing separator, add a file name to the end,
303 # then do the split, then replace it with ''.
305 my( @directories )= split( m|/|, "${directories}dummy" ) ;
306 $directories[ $#directories ]= '' ;
307 return @directories ;
314 Takes volume, directory and file portions and returns an entire path. Under
315 Unix, $volume is ignored, and directory and file are catenated. A '/' is
316 inserted if need be. On other OSs, $volume is significant.
321 my ($self,$volume,$directory,$file) = @_;
323 if ( $directory ne '' &&
325 substr( $directory, -1 ) ne '/' &&
326 substr( $file, 0, 1 ) ne '/'
328 $directory .= "/$file" ;
331 $directory .= $file ;
339 Takes a destination path and an optional base path returns a relative path
340 from the base path to the destination path:
342 $rel_path = File::Spec->abs2rel( $path ) ;
343 $rel_path = File::Spec->abs2rel( $path, $base ) ;
345 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
346 then it is converted to absolute form using L</rel2abs()>. This means that it
347 is taken to be relative to L<cwd()|Cwd>.
349 On systems with the concept of a volume, this assumes that both paths
350 are on the $destination volume, and ignores the $base volume.
352 On systems that have a grammar that indicates filenames, this ignores the
353 $base filename as well. Otherwise all path components are assumed to be
356 If $path is relative, it is converted to absolute form using L</rel2abs()>.
357 This means that it is taken to be relative to L<cwd()|Cwd>.
359 No checks against the filesystem are made. On VMS, there is
360 interaction with the working environment, as logicals and
363 Based on code written by Shigio Yamaguchi.
368 my($self,$path,$base) = @_;
371 if ( ! $self->file_name_is_absolute( $path ) ) {
372 $path = $self->rel2abs( $path ) ;
375 $path = $self->canonpath( $path ) ;
378 # Figure out the effective $base and clean it up.
379 if ( !defined( $base ) || $base eq '' ) {
382 elsif ( ! $self->file_name_is_absolute( $base ) ) {
383 $base = $self->rel2abs( $base ) ;
386 $base = $self->canonpath( $base ) ;
389 # Now, remove all leading components that are the same
390 my @pathchunks = $self->splitdir( $path);
391 my @basechunks = $self->splitdir( $base);
393 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
398 $path = CORE::join( '/', @pathchunks );
399 $base = CORE::join( '/', @basechunks );
401 # $base now contains the directories the resulting relative path
402 # must ascend out of before it can descend to $path_directory. So,
403 # replace all names with $parentDir
404 $base =~ s|[^/]+|..|g ;
406 # Glue the two together, using a separator if necessary, and preventing an
408 if ( $path ne '' && $base ne '' ) {
409 $path = "$base/$path" ;
411 $path = "$base$path" ;
414 return $self->canonpath( $path ) ;
419 Converts a relative path to an absolute path.
421 $abs_path = File::Spec->rel2abs( $path ) ;
422 $abs_path = File::Spec->rel2abs( $path, $base ) ;
424 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
425 then it is converted to absolute form using L</rel2abs()>. This means that it
426 is taken to be relative to L<cwd()|Cwd>.
428 On systems with the concept of a volume, this assumes that both paths
429 are on the $base volume, and ignores the $path volume.
431 On systems that have a grammar that indicates filenames, this ignores the
432 $base filename as well. Otherwise all path components are assumed to be
435 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
437 No checks against the filesystem are made. On VMS, there is
438 interaction with the working environment, as logicals and
441 Based on code written by Shigio Yamaguchi.
446 my ($self,$path,$base ) = @_;
449 if ( ! $self->file_name_is_absolute( $path ) ) {
450 # Figure out the effective $base and clean it up.
451 if ( !defined( $base ) || $base eq '' ) {
454 elsif ( ! $self->file_name_is_absolute( $base ) ) {
455 $base = $self->rel2abs( $base ) ;
458 $base = $self->canonpath( $base ) ;
462 $path = $self->catdir( $base, $path ) ;
465 return $self->canonpath( $path ) ;