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 (qnx, nto)
41 # Handle network path names beginning with double slash (cygwin)
42 # (POSIX says: "a pathname that begins with two successive slashes
43 # may be interpreted in an implementation-defined manner, although
44 # more than two leading slashes shall be treated as a single slash.")
46 if ( $^O =~ m/^(?:qnx|nto|cygwin)$/ && $path =~ s:^(//[^/]+)(/|\z):/:s ) {
50 # $path =~ s|/+|/|g unless($^O eq 'cygwin');
51 # but that made tests 29, 30, 35, 46, and 213 (as of #13272) to fail
52 # (Mainly because trailing "" directories didn't get stripped).
53 # Why would cygwin avoid collapsing multiple slashes into one? --jhi
54 $path =~ s|/+|/|g; # xx////xx -> xx/xx
55 $path =~ s@(/\.)+(/|\Z(?!\n))@/@g; # xx/././xx -> xx/xx
56 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
57 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
58 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
64 Concatenate two or more directory names to form a complete path ending
65 with a directory. But remove the trailing slash from the resulting
66 string, because it doesn't look good, isn't necessary and confuses
67 OS2. Of course, if this is the root directory, don't cut off the
76 # append a slash to each argument unless it has one there
77 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
79 return $self->canonpath(join('', @args));
84 Concatenate one or more directory names and a filename to form a
85 complete path ending with a filename
91 my $file = $self->canonpath(pop @_);
92 return $file unless @_;
93 my $dir = $self->catdir(@_);
94 $dir .= "/" unless substr($dir,-1) eq "/";
100 Returns a string representation of the current directory. "." on UNIX.
110 Returns a string representation of the null device. "/dev/null" on UNIX.
120 Returns a string representation of the root directory. "/" on UNIX.
130 Returns a string representation of the first writable directory
131 from the following list or "" if none are writable:
136 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
137 is tainted, it is not used.
143 return $tmpdir if defined $tmpdir;
144 my @dirlist = ($ENV{TMPDIR}, "/tmp");
147 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
148 require Scalar::Util;
149 shift @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
153 next unless defined && -d && -w _;
157 $tmpdir = '' unless defined $tmpdir;
163 Returns a string representation of the parent directory. ".." on UNIX.
173 Given a list of file names, strip out those that refer to a parent
174 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
180 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
185 Returns a true or false value indicating, respectively, that alphabetic
186 is not or is significant when comparing file specifications.
194 =item file_name_is_absolute
196 Takes as argument a path and returns true if it is an absolute path.
198 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
199 OS (Classic). It does consult the working environment for VMS (see
200 L<File::Spec::VMS/file_name_is_absolute>).
204 sub file_name_is_absolute {
205 my ($self,$file) = @_;
206 return scalar($file =~ m:^/:s);
211 Takes no argument, returns the environment variable PATH as an array.
216 return () unless exists $ENV{PATH};
217 my @path = split(':', $ENV{PATH});
218 foreach (@path) { $_ = '.' if $_ eq '' }
224 join is the same as catfile.
230 return $self->catfile(@_);
235 ($volume,$directories,$file) = File::Spec->splitpath( $path );
236 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
238 Splits a path into volume, directory, and filename portions. On systems
239 with no concept of volume, returns '' for volume.
241 For systems with no syntax differentiating filenames from directories,
242 assumes that the last file is a path unless $no_file is true or a
243 trailing separator or /. or /.. is present. On Unix this means that $no_file
244 true makes this return ( '', $path, '' ).
246 The directory portion may or may not be returned with a trailing '/'.
248 The results can be passed to L</catpath()> to get back a path equivalent to
249 (usually identical to) the original path.
254 my ($self,$path, $nofile) = @_;
256 my ($volume,$directory,$file) = ('','','');
262 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
267 return ($volume,$directory,$file);
273 The opposite of L</catdir()>.
275 @dirs = File::Spec->splitdir( $directories );
277 $directories must be only the directory portion of the path on systems
278 that have the concept of a volume or that have path syntax that differentiates
279 files from directories.
281 Unlike just splitting the directories on the separator, empty
282 directory names (C<''>) can be returned, because these are significant
287 File::Spec->splitdir( "/a/b//c/" );
291 ( '', 'a', 'b', '', 'c', '' )
296 my ($self,$directories) = @_ ;
298 # split() likes to forget about trailing null fields, so here we
299 # check to be sure that there will not be any before handling the
302 if ( $directories !~ m|/\Z(?!\n)| ) {
303 return split( m|/|, $directories );
307 # since there was a trailing separator, add a file name to the end,
308 # then do the split, then replace it with ''.
310 my( @directories )= split( m|/|, "${directories}dummy" ) ;
311 $directories[ $#directories ]= '' ;
312 return @directories ;
319 Takes volume, directory and file portions and returns an entire path. Under
320 Unix, $volume is ignored, and directory and file are catenated. A '/' is
321 inserted if needed (though if the directory portion doesn't start with
322 '/' it is not added). On other OSs, $volume is significant.
327 my ($self,$volume,$directory,$file) = @_;
329 if ( $directory ne '' &&
331 substr( $directory, -1 ) ne '/' &&
332 substr( $file, 0, 1 ) ne '/'
334 $directory .= "/$file" ;
337 $directory .= $file ;
345 Takes a destination path and an optional base path returns a relative path
346 from the base path to the destination path:
348 $rel_path = File::Spec->abs2rel( $path ) ;
349 $rel_path = File::Spec->abs2rel( $path, $base ) ;
351 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
352 then it is converted to absolute form using L</rel2abs()>. This means that it
353 is taken to be relative to L<cwd()|Cwd>.
355 On systems with the concept of a volume, this assumes that both paths
356 are on the $destination volume, and ignores the $base volume.
358 On systems that have a grammar that indicates filenames, this ignores the
359 $base filename as well. Otherwise all path components are assumed to be
362 If $path is relative, it is converted to absolute form using L</rel2abs()>.
363 This means that it is taken to be relative to L<cwd()|Cwd>.
365 No checks against the filesystem are made. On VMS, there is
366 interaction with the working environment, as logicals and
369 Based on code written by Shigio Yamaguchi.
374 my($self,$path,$base) = @_;
377 if ( ! $self->file_name_is_absolute( $path ) ) {
378 $path = $self->rel2abs( $path ) ;
381 $path = $self->canonpath( $path ) ;
384 # Figure out the effective $base and clean it up.
385 if ( !defined( $base ) || $base eq '' ) {
388 elsif ( ! $self->file_name_is_absolute( $base ) ) {
389 $base = $self->rel2abs( $base ) ;
392 $base = $self->canonpath( $base ) ;
395 # Now, remove all leading components that are the same
396 my @pathchunks = $self->splitdir( $path);
397 my @basechunks = $self->splitdir( $base);
399 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
404 $path = CORE::join( '/', @pathchunks );
405 $base = CORE::join( '/', @basechunks );
407 # $base now contains the directories the resulting relative path
408 # must ascend out of before it can descend to $path_directory. So,
409 # replace all names with $parentDir
410 $base =~ s|[^/]+|..|g ;
412 # Glue the two together, using a separator if necessary, and preventing an
414 if ( $path ne '' && $base ne '' ) {
415 $path = "$base/$path" ;
417 $path = "$base$path" ;
420 return $self->canonpath( $path ) ;
425 Converts a relative path to an absolute path.
427 $abs_path = File::Spec->rel2abs( $path ) ;
428 $abs_path = File::Spec->rel2abs( $path, $base ) ;
430 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
431 then it is converted to absolute form using L</rel2abs()>. This means that it
432 is taken to be relative to L<cwd()|Cwd>.
434 On systems with the concept of a volume, this assumes that both paths
435 are on the $base volume, and ignores the $path volume.
437 On systems that have a grammar that indicates filenames, this ignores the
438 $base filename as well. Otherwise all path components are assumed to be
441 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
443 No checks against the filesystem are made. On VMS, there is
444 interaction with the working environment, as logicals and
447 Based on code written by Shigio Yamaguchi.
452 my ($self,$path,$base ) = @_;
455 if ( ! $self->file_name_is_absolute( $path ) ) {
456 # Figure out the effective $base and clean it up.
457 if ( !defined( $base ) || $base eq '' ) {
460 elsif ( ! $self->file_name_is_absolute( $base ) ) {
461 $base = $self->rel2abs( $base ) ;
464 $base = $self->canonpath( $base ) ;
468 $path = $self->catdir( $base, $path ) ;
471 return $self->canonpath( $path ) ;