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
73 $self->canonpath(join('/', @_, '')); # '' because need a trailing '/'
78 Concatenate one or more directory names and a filename to form a
79 complete path ending with a filename
85 my $file = $self->canonpath(pop @_);
86 return $file unless @_;
87 my $dir = $self->catdir(@_);
88 $dir .= "/" unless substr($dir,-1) eq "/";
94 Returns a string representation of the current directory. "." on UNIX.
102 Returns a string representation of the null device. "/dev/null" on UNIX.
106 sub devnull () { '/dev/null' }
110 Returns a string representation of the root directory. "/" on UNIX.
114 sub rootdir () { '/' }
118 Returns a string representation of the first writable directory from
119 the following list or the current directory if none from the list are
125 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
126 is tainted, it is not used.
132 return $tmpdir if defined $tmpdir;
137 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
138 require Scalar::Util;
139 @dirlist = grep { ! Scalar::Util::tainted($_) } @dirlist;
143 next unless defined && -d && -w _;
147 $tmpdir = $self->curdir unless defined $tmpdir;
148 $tmpdir = defined $tmpdir && $self->canonpath($tmpdir);
153 return $tmpdir if defined $tmpdir;
155 $tmpdir = $self->_tmpdir( $ENV{TMPDIR}, "/tmp" );
160 Returns a string representation of the parent directory. ".." on UNIX.
164 sub updir () { '..' }
168 Given a list of file names, strip out those that refer to a parent
169 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
175 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
180 Returns a true or false value indicating, respectively, that alphabetic
181 is not or is significant when comparing file specifications.
185 sub case_tolerant () { 0 }
187 =item file_name_is_absolute
189 Takes as argument a path and returns true if it is an absolute path.
191 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
192 OS (Classic). It does consult the working environment for VMS (see
193 L<File::Spec::VMS/file_name_is_absolute>).
197 sub file_name_is_absolute {
198 my ($self,$file) = @_;
199 return scalar($file =~ m:^/:s);
204 Takes no argument, returns the environment variable PATH as an array.
209 return () unless exists $ENV{PATH};
210 my @path = split(':', $ENV{PATH});
211 foreach (@path) { $_ = '.' if $_ eq '' }
217 join is the same as catfile.
223 return $self->catfile(@_);
228 ($volume,$directories,$file) = File::Spec->splitpath( $path );
229 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
231 Splits a path into volume, directory, and filename portions. On systems
232 with no concept of volume, returns '' for volume.
234 For systems with no syntax differentiating filenames from directories,
235 assumes that the last file is a path unless $no_file is true or a
236 trailing separator or /. or /.. is present. On Unix this means that $no_file
237 true makes this return ( '', $path, '' ).
239 The directory portion may or may not be returned with a trailing '/'.
241 The results can be passed to L</catpath()> to get back a path equivalent to
242 (usually identical to) the original path.
247 my ($self,$path, $nofile) = @_;
249 my ($volume,$directory,$file) = ('','','');
255 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
260 return ($volume,$directory,$file);
266 The opposite of L</catdir()>.
268 @dirs = File::Spec->splitdir( $directories );
270 $directories must be only the directory portion of the path on systems
271 that have the concept of a volume or that have path syntax that differentiates
272 files from directories.
274 Unlike just splitting the directories on the separator, empty
275 directory names (C<''>) can be returned, because these are significant
280 File::Spec->splitdir( "/a/b//c/" );
284 ( '', 'a', 'b', '', 'c', '' )
289 return split m|/|, $_[1], -1; # Preserve trailing fields
295 Takes volume, directory and file portions and returns an entire path. Under
296 Unix, $volume is ignored, and directory and file are concatenated. A '/' is
297 inserted if needed (though if the directory portion doesn't start with
298 '/' it is not added). On other OSs, $volume is significant.
303 my ($self,$volume,$directory,$file) = @_;
305 if ( $directory ne '' &&
307 substr( $directory, -1 ) ne '/' &&
308 substr( $file, 0, 1 ) ne '/'
310 $directory .= "/$file" ;
313 $directory .= $file ;
321 Takes a destination path and an optional base path returns a relative path
322 from the base path to the destination path:
324 $rel_path = File::Spec->abs2rel( $path ) ;
325 $rel_path = File::Spec->abs2rel( $path, $base ) ;
327 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is
328 relative, then it is converted to absolute form using
329 L</rel2abs()>. This means that it is taken to be relative to
332 On systems that have a grammar that indicates filenames, this ignores the
333 $base filename. Otherwise all path components are assumed to be
336 If $path is relative, it is converted to absolute form using L</rel2abs()>.
337 This means that it is taken to be relative to L<cwd()|Cwd>.
339 No checks against the filesystem are made. On VMS, there is
340 interaction with the working environment, as logicals and
343 Based on code written by Shigio Yamaguchi.
348 my($self,$path,$base) = @_;
351 if ( ! $self->file_name_is_absolute( $path ) ) {
352 $path = $self->rel2abs( $path ) ;
355 $path = $self->canonpath( $path ) ;
358 # Figure out the effective $base and clean it up.
359 if ( !defined( $base ) || $base eq '' ) {
360 $base = $self->_cwd();
362 elsif ( ! $self->file_name_is_absolute( $base ) ) {
363 $base = $self->rel2abs( $base ) ;
366 $base = $self->canonpath( $base ) ;
369 # Now, remove all leading components that are the same
370 my @pathchunks = $self->splitdir( $path);
371 my @basechunks = $self->splitdir( $base);
373 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
378 $path = CORE::join( '/', @pathchunks );
379 $base = CORE::join( '/', @basechunks );
381 # $base now contains the directories the resulting relative path
382 # must ascend out of before it can descend to $path_directory. So,
383 # replace all names with $parentDir
384 $base =~ s|[^/]+|..|g ;
386 # Glue the two together, using a separator if necessary, and preventing an
388 if ( $path ne '' && $base ne '' ) {
389 $path = "$base/$path" ;
391 $path = "$base$path" ;
394 return $self->canonpath( $path ) ;
399 Converts a relative path to an absolute path.
401 $abs_path = File::Spec->rel2abs( $path ) ;
402 $abs_path = File::Spec->rel2abs( $path, $base ) ;
404 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is
405 relative, then it is converted to absolute form using
406 L</rel2abs()>. This means that it is taken to be relative to
409 On systems that have a grammar that indicates filenames, this ignores
410 the $base filename. Otherwise all path components are assumed to be
413 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
415 No checks against the filesystem are made. On VMS, there is
416 interaction with the working environment, as logicals and
419 Based on code written by Shigio Yamaguchi.
424 my ($self,$path,$base ) = @_;
427 if ( ! $self->file_name_is_absolute( $path ) ) {
428 # Figure out the effective $base and clean it up.
429 if ( !defined( $base ) || $base eq '' ) {
430 $base = $self->_cwd();
432 elsif ( ! $self->file_name_is_absolute( $base ) ) {
433 $base = $self->rel2abs( $base ) ;
436 $base = $self->canonpath( $base ) ;
440 $path = $self->catdir( $base, $path ) ;
443 return $self->canonpath( $path ) ;
454 # Internal routine to File::Spec, no point in making this public since
455 # it is the standard Cwd interface. Most of the platform-specific
456 # File::Spec subclasses use this.