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) = @_;
37 $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
38 $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
39 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
40 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
41 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
47 Concatenate two or more directory names to form a complete path ending
48 with a directory. But remove the trailing slash from the resulting
49 string, because it doesn't look good, isn't necessary and confuses
50 OS2. Of course, if this is the root directory, don't cut off the
59 # append a slash to each argument unless it has one there
60 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
62 return $self->canonpath(join('', @args));
67 Concatenate one or more directory names and a filename to form a
68 complete path ending with a filename
75 return $file unless @_;
76 my $dir = $self->catdir(@_);
77 $dir .= "/" unless substr($dir,-1) eq "/";
83 Returns a string representation of the current directory. "." on UNIX.
93 Returns a string representation of the null device. "/dev/null" on UNIX.
103 Returns a string representation of the root directory. "/" on UNIX.
113 Returns a string representation of the first writable directory
114 from the following list or "" if none are writable:
123 return $tmpdir if defined $tmpdir;
124 foreach ($ENV{TMPDIR}, "/tmp") {
125 next unless defined && -d && -w _;
129 $tmpdir = '' unless defined $tmpdir;
135 Returns a string representation of the parent directory. ".." on UNIX.
145 Given a list of file names, strip out those that refer to a parent
146 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
152 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
157 Returns a true or false value indicating, respectively, that alphabetic
158 is not or is significant when comparing file specifications.
166 =item file_name_is_absolute
168 Takes as argument a path and returns true, if it is an absolute path.
172 sub file_name_is_absolute {
173 my ($self,$file) = @_;
174 return scalar($file =~ m:^/:s);
179 Takes no argument, returns the environment variable PATH as an array.
184 my @path = split(':', $ENV{PATH});
185 foreach (@path) { $_ = '.' if $_ eq '' }
191 join is the same as catfile.
197 return $self->catfile(@_);
202 ($volume,$directories,$file) = File::Spec->splitpath( $path );
203 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
205 Splits a path in to volume, directory, and filename portions. On systems
206 with no concept of volume, returns undef for volume.
208 For systems with no syntax differentiating filenames from directories,
209 assumes that the last file is a path unless $no_file is true or a
210 trailing separator or /. or /.. is present. On Unix this means that $no_file
211 true makes this return ( '', $path, '' ).
213 The directory portion may or may not be returned with a trailing '/'.
215 The results can be passed to L</catpath()> to get back a path equivalent to
216 (usually identical to) the original path.
221 my ($self,$path, $nofile) = @_;
223 my ($volume,$directory,$file) = ('','','');
229 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
234 return ($volume,$directory,$file);
240 The opposite of L</catdir()>.
242 @dirs = File::Spec->splitdir( $directories );
244 $directories must be only the directory portion of the path on systems
245 that have the concept of a volume or that have path syntax that differentiates
246 files from directories.
248 Unlike just splitting the directories on the separator, empty
249 directory names (C<''>) can be returned, because these are significant
250 on some OSs (e.g. MacOS).
254 File::Spec->splitdir( "/a/b//c/" );
258 ( '', 'a', 'b', '', 'c', '' )
263 my ($self,$directories) = @_ ;
265 # split() likes to forget about trailing null fields, so here we
266 # check to be sure that there will not be any before handling the
269 if ( $directories !~ m|/\Z(?!\n)| ) {
270 return split( m|/|, $directories );
274 # since there was a trailing separator, add a file name to the end,
275 # then do the split, then replace it with ''.
277 my( @directories )= split( m|/|, "${directories}dummy" ) ;
278 $directories[ $#directories ]= '' ;
279 return @directories ;
286 Takes volume, directory and file portions and returns an entire path. Under
287 Unix, $volume is ignored, and directory and file are catenated. A '/' is
288 inserted if need be. On other OSs, $volume is significant.
293 my ($self,$volume,$directory,$file) = @_;
295 if ( $directory ne '' &&
297 substr( $directory, -1 ) ne '/' &&
298 substr( $file, 0, 1 ) ne '/'
300 $directory .= "/$file" ;
303 $directory .= $file ;
311 Takes a destination path and an optional base path returns a relative path
312 from the base path to the destination path:
314 $rel_path = File::Spec->abs2rel( $destination ) ;
315 $rel_path = File::Spec->abs2rel( $destination, $base ) ;
317 If $base is not present or '', then L<cwd()> is used. If $base is relative,
318 then it is converted to absolute form using L</rel2abs()>. This means that it
319 is taken to be relative to L<cwd()>.
321 On systems with the concept of a volume, this assumes that both paths
322 are on the $destination volume, and ignores the $base volume.
324 On systems that have a grammar that indicates filenames, this ignores the
325 $base filename as well. Otherwise all path components are assumed to be
328 If $path is relative, it is converted to absolute form using L</rel2abs()>.
329 This means that it is taken to be relative to L<cwd()>.
331 Based on code written by Shigio Yamaguchi.
333 No checks against the filesystem are made.
338 my($self,$path,$base) = @_;
341 if ( ! $self->file_name_is_absolute( $path ) ) {
342 $path = $self->rel2abs( $path ) ;
345 $path = $self->canonpath( $path ) ;
348 # Figure out the effective $base and clean it up.
349 if ( !defined( $base ) || $base eq '' ) {
352 elsif ( ! $self->file_name_is_absolute( $base ) ) {
353 $base = $self->rel2abs( $base ) ;
356 $base = $self->canonpath( $base ) ;
359 # Now, remove all leading components that are the same
360 my @pathchunks = $self->splitdir( $path);
361 my @basechunks = $self->splitdir( $base);
363 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
368 $path = CORE::join( '/', @pathchunks );
369 $base = CORE::join( '/', @basechunks );
371 # $base now contains the directories the resulting relative path
372 # must ascend out of before it can descend to $path_directory. So,
373 # replace all names with $parentDir
374 $base =~ s|[^/]+|..|g ;
376 # Glue the two together, using a separator if necessary, and preventing an
378 if ( $path ne '' && $base ne '' ) {
379 $path = "$base/$path" ;
381 $path = "$base$path" ;
384 return $self->canonpath( $path ) ;
389 Converts a relative path to an absolute path.
391 $abs_path = File::Spec->rel2abs( $destination ) ;
392 $abs_path = File::Spec->rel2abs( $destination, $base ) ;
394 If $base is not present or '', then L<cwd()> is used. If $base is relative,
395 then it is converted to absolute form using L</rel2abs()>. This means that it
396 is taken to be relative to L<cwd()>.
398 On systems with the concept of a volume, this assumes that both paths
399 are on the $base volume, and ignores the $destination volume.
401 On systems that have a grammar that indicates filenames, this ignores the
402 $base filename as well. Otherwise all path components are assumed to be
405 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
407 Based on code written by Shigio Yamaguchi.
409 No checks against the filesystem are made.
414 my ($self,$path,$base ) = @_;
417 if ( ! $self->file_name_is_absolute( $path ) ) {
418 # Figure out the effective $base and clean it up.
419 if ( !defined( $base ) || $base eq '' ) {
422 elsif ( ! $self->file_name_is_absolute( $base ) ) {
423 $base = $self->rel2abs( $base ) ;
426 $base = $self->canonpath( $base ) ;
430 $path = $self->catdir( $base, $path ) ;
433 return $self->canonpath( $path ) ;