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 ) ;
33 my ($self,$path) = @_;
34 $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
35 $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
36 $path =~ s|^(\./)+|| unless $path eq "./"; # ./xx -> xx
37 $path =~ s|^/(\.\./)+|/|; # /../../xx -> xx
38 $path =~ s|/$|| unless $path eq "/"; # xx/ -> xx
44 Concatenate two or more directory names to form a complete path ending
45 with a directory. But remove the trailing slash from the resulting
46 string, because it doesn't look good, isn't necessary and confuses
47 OS2. Of course, if this is the root directory, don't cut off the
56 # append a slash to each argument unless it has one there
57 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
59 return $self->canonpath(join('', @args));
64 Concatenate one or more directory names and a filename to form a
65 complete path ending with a filename
72 return $file unless @_;
73 my $dir = $self->catdir(@_);
74 $dir .= "/" unless substr($dir,-1) eq "/";
80 Returns a string representation of the current directory. "." on UNIX.
90 Returns a string representation of the null device. "/dev/null" on UNIX.
100 Returns a string representation of the root directory. "/" on UNIX.
110 Returns a string representation of the first writable directory
111 from the following list or "" if none are writable:
120 return $tmpdir if defined $tmpdir;
121 foreach ($ENV{TMPDIR}, "/tmp") {
122 next unless defined && -d && -w _;
126 $tmpdir = '' unless defined $tmpdir;
132 Returns a string representation of the parent directory. ".." on UNIX.
142 Given a list of file names, strip out those that refer to a parent
143 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
149 return grep(!/^\.{1,2}$/, @_);
154 Returns a true or false value indicating, respectively, that alphabetic
155 is not or is significant when comparing file specifications.
163 =item file_name_is_absolute
165 Takes as argument a path and returns true, if it is an absolute path.
169 sub file_name_is_absolute {
170 my ($self,$file) = @_;
171 return scalar($file =~ m:^/:);
176 Takes no argument, returns the environment variable PATH as an array.
181 my @path = split(':', $ENV{PATH});
182 foreach (@path) { $_ = '.' if $_ eq '' }
188 join is the same as catfile.
194 return $self->catfile(@_);
199 ($volume,$directories,$file) = File::Spec->splitpath( $path );
200 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
202 Splits a path in to volume, directory, and filename portions. On systems
203 with no concept of volume, returns undef for volume.
205 For systems with no syntax differentiating filenames from directories,
206 assumes that the last file is a path unless $no_file is true or a
207 trailing separator or /. or /.. is present. On Unix this means that $no_file
208 true makes this return ( '', $path, '' ).
210 The directory portion may or may not be returned with a trailing '/'.
212 The results can be passed to L</catpath()> to get back a path equivalent to
213 (usually identical to) the original path.
218 my ($self,$path, $nofile) = @_;
220 my ($volume,$directory,$file) = ('','','');
226 $path =~ m|^ ( (?: .* / (?: \.\.?$ )? )? ) ([^/]*) |x;
231 return ($volume,$directory,$file);
237 The opposite of L</catdir()>.
239 @dirs = File::Spec->splitdir( $directories );
241 $directories must be only the directory portion of the path on systems
242 that have the concept of a volume or that have path syntax that differentiates
243 files from directories.
245 Unlike just splitting the directories on the separator, leading empty and
246 trailing directory entries can be returned, because these are significant
249 File::Spec->splitdir( "/a/b/c" );
253 ( '', 'a', 'b', '', 'c', '' )
258 my ($self,$directories) = @_ ;
260 # split() likes to forget about trailing null fields, so here we
261 # check to be sure that there will not be any before handling the
264 if ( $directories !~ m|/$| ) {
265 return split( m|/|, $directories );
269 # since there was a trailing separator, add a file name to the end,
270 # then do the split, then replace it with ''.
272 my( @directories )= split( m|/|, "${directories}dummy" ) ;
273 $directories[ $#directories ]= '' ;
274 return @directories ;
281 Takes volume, directory and file portions and returns an entire path. Under
282 Unix, $volume is ignored, and directory and file are catenated. A '/' is
283 inserted if need be. On other OSs, $volume is significant.
288 my ($self,$volume,$directory,$file) = @_;
290 if ( $directory ne '' &&
292 substr( $directory, -1 ) ne '/' &&
293 substr( $file, 0, 1 ) ne '/'
295 $directory .= "/$file" ;
298 $directory .= $file ;
306 Takes a destination path and an optional base path returns a relative path
307 from the base path to the destination path:
309 $rel_path = File::Spec->abs2rel( $destination ) ;
310 $rel_path = File::Spec->abs2rel( $destination, $base ) ;
312 If $base is not present or '', then L<cwd()> is used. If $base is relative,
313 then it is converted to absolute form using L</rel2abs()>. This means that it
314 is taken to be relative to L<cwd()>.
316 On systems with the concept of a volume, this assumes that both paths
317 are on the $destination volume, and ignores the $base volume.
319 On systems that have a grammar that indicates filenames, this ignores the
320 $base filename as well. Otherwise all path components are assumed to be
323 If $path is relative, it is converted to absolute form using L</rel2abs()>.
324 This means that it is taken to be relative to L<cwd()>.
326 Based on code written by Shigio Yamaguchi.
328 No checks against the filesystem are made.
333 my($self,$path,$base) = @_;
336 if ( ! $self->file_name_is_absolute( $path ) ) {
337 $path = $self->rel2abs( $path ) ;
340 $path = $self->canonpath( $path ) ;
343 # Figure out the effective $base and clean it up.
344 if ( !defined( $base ) || $base eq '' ) {
347 elsif ( ! $self->file_name_is_absolute( $base ) ) {
348 $base = $self->rel2abs( $base ) ;
351 $base = $self->canonpath( $base ) ;
354 # Now, remove all leading components that are the same
355 my @pathchunks = $self->splitpath( $path );
356 my @basechunks = ($self->splitpath( $base, 1 ))[0,1];
358 # Insure same device; case-insensitive since those filesystems
359 # which use device semantics (VMS and Win32) are case-tolerant
360 return undef unless lc($pathchunks[0]) eq lc($basechunks[0]);
361 $path = $pathchunks[0] || '';
362 @pathchunks = ( $self->splitdir( $pathchunks[1] ), $pathchunks[2] );
363 @basechunks = $self->splitdir( $basechunks[1] );
365 # We do case-insensitive comparisons rather than just flattening case
366 # so caller gets back same case as was sent in
367 my $lc = $self->case_tolerant;
368 while (@pathchunks && @basechunks &&
369 ($lc ? lc($pathchunks[0]) eq lc($basechunks[0])
370 : $pathchunks[0] eq $basechunks[0] ) ) {
375 # @basechunks now contains the directories the resulting relative path
376 # must ascend out of before it can descend to $path_directory. So,
377 # replace all names with $parentDir
378 @basechunks = ($self->updir()) x @basechunks;
380 # Glue the two together, using a separator if necessary, and preventing an
382 $path = $self->catfile($path,@basechunks,@pathchunks);
383 $path = $self->curdir unless $path;
385 return $self->canonpath( $path ) ;
390 Converts a relative path to an absolute path.
392 $abs_path = $File::Spec->rel2abs( $destination ) ;
393 $abs_path = $File::Spec->rel2abs( $destination, $base ) ;
395 If $base is not present or '', then L<cwd()> is used. If $base is relative,
396 then it is converted to absolute form using L</rel2abs()>. This means that it
397 is taken to be relative to L<cwd()>.
399 On systems with the concept of a volume, this assumes that both paths
400 are on the $base volume, and ignores the $destination volume.
402 On systems that have a grammar that indicates filenames, this ignores the
403 $base filename as well. Otherwise all path components are assumed to be
406 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
408 Based on code written by Shigio Yamaguchi.
410 No checks against the filesystem are made.
415 my ($self,$path,$base ) = @_;
418 if ( ! $self->file_name_is_absolute( $path ) ) {
419 # Figure out the effective $base and clean it up.
420 if ( !defined( $base ) || $base eq '' ) {
423 elsif ( ! $self->file_name_is_absolute( $base ) ) {
424 $base = $self->rel2abs( $base ) ;
427 $base = $self->canonpath( $base ) ;
431 my($pdev,$pdir,$pfile) = $self->splitpath( $path );
432 my($bdev,$bdir,$bfile) = $self->splitpath( $base );
433 $path = $self->catpath( $bdev, $self->catdir( $bdir, $pdir ), $pfile );
436 return $self->canonpath( $path ) ;