1 package File::Spec::Unix;
12 File::Spec::Unix - methods used by File::Spec
16 require File::Spec::Unix; # Done automatically by File::Spec
20 Methods for manipulating file specifications.
28 No physical check on the filesystem, but a logical cleanup of a
29 path. On UNIX eliminated successive slashes and successive "/.".
31 $cpath = File::Spec->canonpath( $path ) ;
36 my ($self,$path) = @_;
38 # Handle POSIX-style node names beginning with double slash
40 if ( $^O =~ m/^(?:qnx|nto)$/ && $path =~ s:^(//[^/]+)(/|\z):/:s ) {
43 $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
44 $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
45 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
46 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
47 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
53 Concatenate two or more directory names to form a complete path ending
54 with a directory. But remove the trailing slash from the resulting
55 string, because it doesn't look good, isn't necessary and confuses
56 OS2. Of course, if this is the root directory, don't cut off the
65 # append a slash to each argument unless it has one there
66 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
68 return $self->canonpath(join('', @args));
73 Concatenate one or more directory names and a filename to form a
74 complete path ending with a filename
81 return $file unless @_;
82 my $dir = $self->catdir(@_);
83 $dir .= "/" unless substr($dir,-1) eq "/";
89 Returns a string representation of the current directory. "." on UNIX.
99 Returns a string representation of the null device. "/dev/null" on UNIX.
109 Returns a string representation of the root directory. "/" on UNIX.
119 Returns a string representation of the first writable directory
120 from the following list or "" if none are writable:
129 return $tmpdir if defined $tmpdir;
130 foreach ($ENV{TMPDIR}, "/tmp") {
131 next unless defined && -d && -w _;
135 $tmpdir = '' unless defined $tmpdir;
141 Returns a string representation of the parent directory. ".." on UNIX.
151 Given a list of file names, strip out those that refer to a parent
152 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
158 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
163 Returns a true or false value indicating, respectively, that alphabetic
164 is not or is significant when comparing file specifications.
172 =item file_name_is_absolute
174 Takes as argument a path and returns true if it is an absolute path.
176 This does not consult the local filesystem on Unix, Win32, or OS/2. It
177 does sometimes on MacOS (see L<File::Spec::MacOS/file_name_is_absolute>).
178 It does consult the working environment for VMS (see
179 L<File::Spec::VMS/file_name_is_absolute>).
183 sub file_name_is_absolute {
184 my ($self,$file) = @_;
185 return scalar($file =~ m:^/:s);
190 Takes no argument, returns the environment variable PATH as an array.
195 my @path = split(':', $ENV{PATH});
196 foreach (@path) { $_ = '.' if $_ eq '' }
202 join is the same as catfile.
208 return $self->catfile(@_);
213 ($volume,$directories,$file) = File::Spec->splitpath( $path );
214 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
216 Splits a path in to volume, directory, and filename portions. On systems
217 with no concept of volume, returns undef for volume.
219 For systems with no syntax differentiating filenames from directories,
220 assumes that the last file is a path unless $no_file is true or a
221 trailing separator or /. or /.. is present. On Unix this means that $no_file
222 true makes this return ( '', $path, '' ).
224 The directory portion may or may not be returned with a trailing '/'.
226 The results can be passed to L</catpath()> to get back a path equivalent to
227 (usually identical to) the original path.
232 my ($self,$path, $nofile) = @_;
234 my ($volume,$directory,$file) = ('','','');
240 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
245 return ($volume,$directory,$file);
251 The opposite of L</catdir()>.
253 @dirs = File::Spec->splitdir( $directories );
255 $directories must be only the directory portion of the path on systems
256 that have the concept of a volume or that have path syntax that differentiates
257 files from directories.
259 Unlike just splitting the directories on the separator, empty
260 directory names (C<''>) can be returned, because these are significant
261 on some OSs (e.g. MacOS).
265 File::Spec->splitdir( "/a/b//c/" );
269 ( '', 'a', 'b', '', 'c', '' )
274 my ($self,$directories) = @_ ;
276 # split() likes to forget about trailing null fields, so here we
277 # check to be sure that there will not be any before handling the
280 if ( $directories !~ m|/\Z(?!\n)| ) {
281 return split( m|/|, $directories );
285 # since there was a trailing separator, add a file name to the end,
286 # then do the split, then replace it with ''.
288 my( @directories )= split( m|/|, "${directories}dummy" ) ;
289 $directories[ $#directories ]= '' ;
290 return @directories ;
297 Takes volume, directory and file portions and returns an entire path. Under
298 Unix, $volume is ignored, and directory and file are catenated. A '/' is
299 inserted if need be. On other OSs, $volume is significant.
304 my ($self,$volume,$directory,$file) = @_;
306 if ( $directory ne '' &&
308 substr( $directory, -1 ) ne '/' &&
309 substr( $file, 0, 1 ) ne '/'
311 $directory .= "/$file" ;
314 $directory .= $file ;
322 Takes a destination path and an optional base path returns a relative path
323 from the base path to the destination path:
325 $rel_path = File::Spec->abs2rel( $path ) ;
326 $rel_path = File::Spec->abs2rel( $path, $base ) ;
328 If $base is not present or '', then L<cwd()> is used. If $base is relative,
329 then it is converted to absolute form using L</rel2abs()>. This means that it
330 is taken to be relative to L<cwd()>.
332 On systems with the concept of a volume, this assumes that both paths
333 are on the $destination volume, and ignores the $base volume.
335 On systems that have a grammar that indicates filenames, this ignores the
336 $base filename as well. Otherwise all path components are assumed to be
339 If $path is relative, it is converted to absolute form using L</rel2abs()>.
340 This means that it is taken to be relative to L<cwd()>.
342 No checks against the filesystem are made on most systems. On MacOS,
343 the filesystem may be consulted (see
344 L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
345 interaction with the working environment, as logicals and
348 Based on code written by Shigio Yamaguchi.
353 my($self,$path,$base) = @_;
356 if ( ! $self->file_name_is_absolute( $path ) ) {
357 $path = $self->rel2abs( $path ) ;
360 $path = $self->canonpath( $path ) ;
363 # Figure out the effective $base and clean it up.
364 if ( !defined( $base ) || $base eq '' ) {
367 elsif ( ! $self->file_name_is_absolute( $base ) ) {
368 $base = $self->rel2abs( $base ) ;
371 $base = $self->canonpath( $base ) ;
374 # Now, remove all leading components that are the same
375 my @pathchunks = $self->splitdir( $path);
376 my @basechunks = $self->splitdir( $base);
378 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
383 $path = CORE::join( '/', @pathchunks );
384 $base = CORE::join( '/', @basechunks );
386 # $base now contains the directories the resulting relative path
387 # must ascend out of before it can descend to $path_directory. So,
388 # replace all names with $parentDir
389 $base =~ s|[^/]+|..|g ;
391 # Glue the two together, using a separator if necessary, and preventing an
393 if ( $path ne '' && $base ne '' ) {
394 $path = "$base/$path" ;
396 $path = "$base$path" ;
399 return $self->canonpath( $path ) ;
404 Converts a relative path to an absolute path.
406 $abs_path = File::Spec->rel2abs( $path ) ;
407 $abs_path = File::Spec->rel2abs( $path, $base ) ;
409 If $base is not present or '', then L<cwd()> is used. If $base is relative,
410 then it is converted to absolute form using L</rel2abs()>. This means that it
411 is taken to be relative to L<cwd()>.
413 On systems with the concept of a volume, this assumes that both paths
414 are on the $base volume, and ignores the $path volume.
416 On systems that have a grammar that indicates filenames, this ignores the
417 $base filename as well. Otherwise all path components are assumed to be
420 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
422 No checks against the filesystem are made on most systems. On MacOS,
423 the filesystem may be consulted (see
424 L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
425 interaction with the working environment, as logicals and
428 Based on code written by Shigio Yamaguchi.
433 my ($self,$path,$base ) = @_;
436 if ( ! $self->file_name_is_absolute( $path ) ) {
437 # Figure out the effective $base and clean it up.
438 if ( !defined( $base ) || $base eq '' ) {
441 elsif ( ! $self->file_name_is_absolute( $base ) ) {
442 $base = $self->rel2abs( $base ) ;
445 $base = $self->canonpath( $base ) ;
449 $path = $self->catdir( $base, $path ) ;
452 return $self->canonpath( $path ) ;