1 package File::Spec::Unix;
10 File::Spec::Unix - File::Spec for Unix, base for other File::Spec modules
14 require File::Spec::Unix; # Done automatically by File::Spec
18 Methods for manipulating file specifications. Other File::Spec
19 modules, such as File::Spec::Mac, inherit from File::Spec::Unix and
20 override specific methods.
28 No physical check on the filesystem, but a logical cleanup of a
29 path. On UNIX eliminates successive slashes and successive "/.".
31 $cpath = File::Spec->canonpath( $path ) ;
36 my ($self,$path) = @_;
38 # Handle POSIX-style node names beginning with double slash (qnx, nto)
39 # Handle network path names beginning with double slash (cygwin)
40 # (POSIX says: "a pathname that begins with two successive slashes
41 # may be interpreted in an implementation-defined manner, although
42 # more than two leading slashes shall be treated as a single slash.")
44 if ( $^O =~ m/^(?:qnx|nto|cygwin)$/ && $path =~ s:^(//[^/]+)(/|\z):/:s ) {
48 # $path =~ s|/+|/|g unless($^O eq 'cygwin');
49 # but that made tests 29, 30, 35, 46, and 213 (as of #13272) to fail
50 # (Mainly because trailing "" directories didn't get stripped).
51 # Why would cygwin avoid collapsing multiple slashes into one? --jhi
52 $path =~ s|/+|/|g; # xx////xx -> xx/xx
53 $path =~ s@(/\.)+(/|\Z(?!\n))@/@g; # xx/././xx -> xx/xx
54 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
55 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
56 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
62 Concatenate two or more directory names to form a complete path ending
63 with a directory. But remove the trailing slash from the resulting
64 string, because it doesn't look good, isn't necessary and confuses
65 OS2. Of course, if this is the root directory, don't cut off the
74 # append a slash to each argument unless it has one there
75 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
77 return $self->canonpath(join('', @args));
82 Concatenate one or more directory names and a filename to form a
83 complete path ending with a filename
89 my $file = $self->canonpath(pop @_);
90 return $file unless @_;
91 my $dir = $self->catdir(@_);
92 $dir .= "/" unless substr($dir,-1) eq "/";
98 Returns a string representation of the current directory. "." on UNIX.
108 Returns a string representation of the null device. "/dev/null" on UNIX.
118 Returns a string representation of the root directory. "/" on UNIX.
128 Returns a string representation of the first writable directory from
129 the following list or the current directory if none from the list are
135 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
136 is tainted, it is not used.
142 return $tmpdir if defined $tmpdir;
147 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
148 require Scalar::Util;
149 @dirlist = grep { ! Scalar::Util::tainted($_) } @dirlist;
153 next unless defined && -d && -w _;
157 $tmpdir = $self->curdir unless defined $tmpdir;
158 $tmpdir = defined $tmpdir && $self->canonpath($tmpdir);
163 return $tmpdir if defined $tmpdir;
165 $tmpdir = $self->_tmpdir( $ENV{TMPDIR}, "/tmp" );
170 Returns a string representation of the parent directory. ".." on UNIX.
180 Given a list of file names, strip out those that refer to a parent
181 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
187 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
192 Returns a true or false value indicating, respectively, that alphabetic
193 is not or is significant when comparing file specifications.
201 =item file_name_is_absolute
203 Takes as argument a path and returns true if it is an absolute path.
205 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
206 OS (Classic). It does consult the working environment for VMS (see
207 L<File::Spec::VMS/file_name_is_absolute>).
211 sub file_name_is_absolute {
212 my ($self,$file) = @_;
213 return scalar($file =~ m:^/:s);
218 Takes no argument, returns the environment variable PATH as an array.
223 return () unless exists $ENV{PATH};
224 my @path = split(':', $ENV{PATH});
225 foreach (@path) { $_ = '.' if $_ eq '' }
231 join is the same as catfile.
237 return $self->catfile(@_);
242 ($volume,$directories,$file) = File::Spec->splitpath( $path );
243 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
245 Splits a path into volume, directory, and filename portions. On systems
246 with no concept of volume, returns '' for volume.
248 For systems with no syntax differentiating filenames from directories,
249 assumes that the last file is a path unless $no_file is true or a
250 trailing separator or /. or /.. is present. On Unix this means that $no_file
251 true makes this return ( '', $path, '' ).
253 The directory portion may or may not be returned with a trailing '/'.
255 The results can be passed to L</catpath()> to get back a path equivalent to
256 (usually identical to) the original path.
261 my ($self,$path, $nofile) = @_;
263 my ($volume,$directory,$file) = ('','','');
269 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
274 return ($volume,$directory,$file);
280 The opposite of L</catdir()>.
282 @dirs = File::Spec->splitdir( $directories );
284 $directories must be only the directory portion of the path on systems
285 that have the concept of a volume or that have path syntax that differentiates
286 files from directories.
288 Unlike just splitting the directories on the separator, empty
289 directory names (C<''>) can be returned, because these are significant
294 File::Spec->splitdir( "/a/b//c/" );
298 ( '', 'a', 'b', '', 'c', '' )
303 return split m|/|, $_[1], -1; # Preserve trailing fields
309 Takes volume, directory and file portions and returns an entire path. Under
310 Unix, $volume is ignored, and directory and file are concatenated. A '/' is
311 inserted if needed (though if the directory portion doesn't start with
312 '/' it is not added). On other OSs, $volume is significant.
317 my ($self,$volume,$directory,$file) = @_;
319 if ( $directory ne '' &&
321 substr( $directory, -1 ) ne '/' &&
322 substr( $file, 0, 1 ) ne '/'
324 $directory .= "/$file" ;
327 $directory .= $file ;
335 Takes a destination path and an optional base path returns a relative path
336 from the base path to the destination path:
338 $rel_path = File::Spec->abs2rel( $path ) ;
339 $rel_path = File::Spec->abs2rel( $path, $base ) ;
341 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is
342 relative, then it is converted to absolute form using
343 L</rel2abs()>. This means that it is taken to be relative to
346 On systems with the concept of a volume, this assumes that both paths
347 are on the $destination volume, and ignores the $base volume.
349 On systems that have a grammar that indicates filenames, this ignores the
350 $base filename as well. Otherwise all path components are assumed to be
353 If $path is relative, it is converted to absolute form using L</rel2abs()>.
354 This means that it is taken to be relative to L<cwd()|Cwd>.
356 No checks against the filesystem are made. On VMS, there is
357 interaction with the working environment, as logicals and
360 Based on code written by Shigio Yamaguchi.
365 my($self,$path,$base) = @_;
368 if ( ! $self->file_name_is_absolute( $path ) ) {
369 $path = $self->rel2abs( $path ) ;
372 $path = $self->canonpath( $path ) ;
375 # Figure out the effective $base and clean it up.
376 if ( !defined( $base ) || $base eq '' ) {
377 $base = $self->cwd();
379 elsif ( ! $self->file_name_is_absolute( $base ) ) {
380 $base = $self->rel2abs( $base ) ;
383 $base = $self->canonpath( $base ) ;
386 # Now, remove all leading components that are the same
387 my @pathchunks = $self->splitdir( $path);
388 my @basechunks = $self->splitdir( $base);
390 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
395 $path = CORE::join( '/', @pathchunks );
396 $base = CORE::join( '/', @basechunks );
398 # $base now contains the directories the resulting relative path
399 # must ascend out of before it can descend to $path_directory. So,
400 # replace all names with $parentDir
401 $base =~ s|[^/]+|..|g ;
403 # Glue the two together, using a separator if necessary, and preventing an
405 if ( $path ne '' && $base ne '' ) {
406 $path = "$base/$path" ;
408 $path = "$base$path" ;
411 return $self->canonpath( $path ) ;
416 Converts a relative path to an absolute path.
418 $abs_path = File::Spec->rel2abs( $path ) ;
419 $abs_path = File::Spec->rel2abs( $path, $base ) ;
421 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
422 then it is converted to absolute form using L</rel2abs()>. This means that it
423 is taken to be relative to L<cwd()|Cwd>.
425 On systems with the concept of a volume, this assumes that both paths
426 are on the $base volume, and ignores the $path volume.
428 On systems that have a grammar that indicates filenames, this ignores the
429 $base filename as well. Otherwise all path components are assumed to be
432 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
434 No checks against the filesystem are made. On VMS, there is
435 interaction with the working environment, as logicals and
438 Based on code written by Shigio Yamaguchi.
443 my ($self,$path,$base ) = @_;
446 if ( ! $self->file_name_is_absolute( $path ) ) {
447 # Figure out the effective $base and clean it up.
448 if ( !defined( $base ) || $base eq '' ) {
449 $base = $self->cwd();
451 elsif ( ! $self->file_name_is_absolute( $base ) ) {
452 $base = $self->rel2abs( $base ) ;
455 $base = $self->canonpath( $base ) ;
459 $path = $self->catdir( $base, $path ) ;
462 return $self->canonpath( $path ) ;
473 # Internal routine to File::Spec, no point in publicly documenting
474 # this interface since it's the standard Cwd interface. Some of the
475 # platform-specific File::Spec subclasses use this.