1 package File::Spec::Unix;
9 File::Spec::Unix - methods used by File::Spec
13 require File::Spec::Unix; # Done automatically by File::Spec
17 Methods for manipulating file specifications.
25 No physical check on the filesystem, but a logical cleanup of a
26 path. On UNIX eliminated successive slashes and successive "/.".
28 $cpath = File::Spec->canonpath( $path ) ;
29 $cpath = File::Spec->canonpath( $path, $reduce_ricochet ) ;
31 If $reduce_ricochet is present and true, then "dirname/.."
32 constructs are eliminated from the path. Without $reduce_ricochet,
33 if dirname is a symbolic link, then "a/dirname/../b" will often
34 take you to someplace other than "a/b". This is sometimes desirable.
35 If it's not, setting $reduce_ricochet causes the "dirname/.." to
36 be removed from this path, resulting in "a/b". This may make
37 your perl more portable and robust, unless you want to
38 ricochet (some scripts depend on it).
43 my ($self,$path,$reduce_ricochet) = @_;
44 $path =~ s|/+|/|g unless($^O =~ /cygwin/); # xx////xx -> xx/xx
45 $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
46 $path =~ s|^(\./)+|| unless $path eq "./"; # ./xx -> xx
47 $path =~ s|^/(\.\./)+|/|; # /../../xx -> xx
48 if ( $reduce_ricochet ) {
49 while ( $path =~ s@[^/]+/\.\.(?:/|$)@@ ) {}# xx/.. -> xx
51 $path =~ s|/$|| unless $path eq "/"; # xx/ -> xx
57 Concatenate two or more directory names to form a complete path ending
58 with a directory. But remove the trailing slash from the resulting
59 string, because it doesn't look good, isn't necessary and confuses
60 OS2. Of course, if this is the root directory, don't cut off the
69 # append a slash to each argument unless it has one there
70 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
72 return $self->canonpath(join('', @args));
77 Concatenate one or more directory names and a filename to form a
78 complete path ending with a filename
85 return $file unless @_;
86 my $dir = $self->catdir(@_);
87 $dir .= "/" unless substr($dir,-1) eq "/";
93 Returns a string representation of the current directory. "." on UNIX.
103 Returns a string representation of the null device. "/dev/null" on UNIX.
113 Returns a string representation of the root directory. "/" on UNIX.
123 Returns a string representation of the first writable directory
124 from the following list or "" if none are writable:
133 return $tmpdir if defined $tmpdir;
134 foreach ($ENV{TMPDIR}, "/tmp") {
135 next unless defined && -d && -w _;
139 $tmpdir = '' unless defined $tmpdir;
145 Returns a string representation of the parent directory. ".." on UNIX.
155 Given a list of file names, strip out those that refer to a parent
156 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
162 return grep(!/^\.{1,2}$/, @_);
165 =item file_name_is_absolute
167 Takes as argument a path and returns true, if it is an absolute path.
171 sub file_name_is_absolute {
172 my ($self,$file) = @_;
173 return scalar($file =~ m:^/:);
178 Takes no argument, returns the environment variable PATH as an array.
183 my @path = split(':', $ENV{PATH});
184 foreach (@path) { $_ = '.' if $_ eq '' }
190 join is the same as catfile.
196 return $self->catfile(@_);
201 ($volume,$directories,$file) = File::Spec->splitpath( $path );
202 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
204 Splits a path in to volume, directory, and filename portions. On systems
205 with no concept of volume, returns undef for volume.
207 For systems with no syntax differentiating filenames from directories,
208 assumes that the last file is a path unless $no_file is true or a
209 trailing separator or /. or /.. is present. On Unix this means that $no_file
210 true makes this return ( '', $path, '' ).
212 The directory portion may or may not be returned with a trailing '/'.
214 The results can be passed to L</catpath()> to get back a path equivalent to
215 (usually identical to) the original path.
220 my ($self,$path, $nofile) = @_;
222 my ($volume,$directory,$file) = ('','','');
228 $path =~ m|^ ( (?: .* / (?: \.\.?$ )? )? ) ([^/]*) |x;
233 return ($volume,$directory,$file);
239 The opposite of L</catdir()>.
241 @dirs = File::Spec->splitdir( $directories );
243 $directories must be only the directory portion of the path on systems
244 that have the concept of a volume or that have path syntax that differentiates
245 files from directories.
247 Unlike just splitting the directories on the separator, leading empty and
248 trailing directory entries can be returned, because these are significant
251 File::Spec->splitdir( "/a/b/c" );
255 ( '', 'a', 'b', '', 'c', '' )
260 my ($self,$directories) = @_ ;
262 # split() likes to forget about trailing null fields, so here we
263 # check to be sure that there will not be any before handling the
266 if ( $directories !~ m|/$| ) {
267 return split( m|/|, $directories );
271 # since there was a trailing separator, add a file name to the end,
272 # then do the split, then replace it with ''.
274 my( @directories )= split( m|/|, "${directories}dummy" ) ;
275 $directories[ $#directories ]= '' ;
276 return @directories ;
283 Takes volume, directory and file portions and returns an entire path. Under
284 Unix, $volume is ignored, and this is just like catfile(). On other OSs,
285 the $volume become significant.
290 my ($self,$volume,$directory,$file) = @_;
292 if ( $directory ne '' &&
294 substr( $directory, -1 ) ne '/' &&
295 substr( $file, 0, 1 ) ne '/'
297 $directory .= "/$file" ;
300 $directory .= $file ;
308 Takes a destination path and an optional base path returns a relative path
309 from the base path to the destination path:
311 $rel_path = File::Spec->abs2rel( $destination ) ;
312 $rel_path = File::Spec->abs2rel( $destination, $base ) ;
314 If $base is not present or '', then L<cwd()> is used. If $base is relative,
315 then it is converted to absolute form using L</rel2abs()>. This means that it
316 is taken to be relative to L<cwd()>.
318 On systems with the concept of a volume, this assumes that both paths
319 are on the $destination volume, and ignores the $base volume.
321 On systems that have a grammar that indicates filenames, this ignores the
322 $base filename as well. Otherwise all path components are assumed to be
325 If $path is relative, it is converted to absolute form using L</rel2abs()>.
326 This means that it is taken to be relative to L<cwd()>.
328 Based on code written by Shigio Yamaguchi.
330 No checks against the filesystem are made.
335 my($self,$path,$base) = @_;
338 if ( ! $self->file_name_is_absolute( $path ) ) {
339 $path = $self->rel2abs( $path ) ;
342 $path = $self->canonpath( $path ) ;
345 # Figure out the effective $base and clean it up.
346 if ( !defined( $base ) || $base eq '' ) {
349 elsif ( ! $self->file_name_is_absolute( $base ) ) {
350 $base = $self->rel2abs( $base ) ;
353 $base = $self->canonpath( $base ) ;
356 # Now, remove all leading components that are the same
357 my @pathchunks = $self->splitdir( $path);
358 my @basechunks = $self->splitdir( $base);
360 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
365 $path = CORE::join( '/', @pathchunks );
366 $base = CORE::join( '/', @basechunks );
368 # $base now contains the directories the resulting relative path
369 # must ascend out of before it can descend to $path_directory. So,
370 # replace all names with $parentDir
371 $base =~ s|[^/]+|..|g ;
373 # Glue the two together, using a separator if necessary, and preventing an
375 if ( $path ne '' && $base ne '' ) {
376 $path = "$base/$path" ;
378 $path = "$base$path" ;
381 return $self->canonpath( $path ) ;
386 Converts a relative path to an absolute path.
388 $abs_path = $File::Spec->rel2abs( $destination ) ;
389 $abs_path = $File::Spec->rel2abs( $destination, $base ) ;
391 If $base is not present or '', then L<cwd()> is used. If $base is relative,
392 then it is converted to absolute form using L</rel2abs()>. This means that it
393 is taken to be relative to L<cwd()>.
395 On systems with the concept of a volume, this assumes that both paths
396 are on the $base volume, and ignores the $destination volume.
398 On systems that have a grammar that indicates filenames, this ignores the
399 $base filename as well. Otherwise all path components are assumed to be
402 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
404 Based on code written by Shigio Yamaguchi.
406 No checks against the filesystem are made.
411 my ($self,$path,$base ) = @_;
414 if ( ! $self->file_name_is_absolute( $path ) ) {
415 # Figure out the effective $base and clean it up.
416 if ( !defined( $base ) || $base eq '' ) {
419 elsif ( ! $self->file_name_is_absolute( $base ) ) {
420 $base = $self->rel2abs( $base ) ;
423 $base = $self->canonpath( $base ) ;
427 $path = $self->catdir( $base, $path ) ;
430 return $self->canonpath( $path ) ;