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
42 if ( $^O =~ m/^(?:qnx|nto)$/ && $path =~ s:^(//[^/]+)(/|\z):/:s ) {
45 $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx
46 $path =~ s@(/\.)+(/|\Z(?!\n))@/@g; # xx/././xx -> xx/xx
47 $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
48 $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
49 $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx
55 Concatenate two or more directory names to form a complete path ending
56 with a directory. But remove the trailing slash from the resulting
57 string, because it doesn't look good, isn't necessary and confuses
58 OS2. Of course, if this is the root directory, don't cut off the
67 # append a slash to each argument unless it has one there
68 $_ .= "/" if $_ eq '' || substr($_,-1) ne "/";
70 return $self->canonpath(join('', @args));
75 Concatenate one or more directory names and a filename to form a
76 complete path ending with a filename
83 return $file unless @_;
84 my $dir = $self->catdir(@_);
85 $dir .= "/" unless substr($dir,-1) eq "/";
91 Returns a string representation of the current directory. "." on UNIX.
101 Returns a string representation of the null device. "/dev/null" on UNIX.
111 Returns a string representation of the root directory. "/" on UNIX.
121 Returns a string representation of the first writable directory
122 from the following list or "" if none are writable:
131 return $tmpdir if defined $tmpdir;
132 foreach ($ENV{TMPDIR}, "/tmp") {
133 next unless defined && -d && -w _;
137 $tmpdir = '' unless defined $tmpdir;
143 Returns a string representation of the parent directory. ".." on UNIX.
153 Given a list of file names, strip out those that refer to a parent
154 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
160 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
165 Returns a true or false value indicating, respectively, that alphabetic
166 is not or is significant when comparing file specifications.
174 =item file_name_is_absolute
176 Takes as argument a path and returns true if it is an absolute path.
178 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
179 OS (Classic). It does consult the working environment for VMS (see
180 L<File::Spec::VMS/file_name_is_absolute>).
184 sub file_name_is_absolute {
185 my ($self,$file) = @_;
186 return scalar($file =~ m:^/:s);
191 Takes no argument, returns the environment variable PATH as an array.
196 my @path = split(':', $ENV{PATH});
197 foreach (@path) { $_ = '.' if $_ eq '' }
203 join is the same as catfile.
209 return $self->catfile(@_);
214 ($volume,$directories,$file) = File::Spec->splitpath( $path );
215 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
217 Splits a path in to volume, directory, and filename portions. On systems
218 with no concept of volume, returns undef for volume.
220 For systems with no syntax differentiating filenames from directories,
221 assumes that the last file is a path unless $no_file is true or a
222 trailing separator or /. or /.. is present. On Unix this means that $no_file
223 true makes this return ( '', $path, '' ).
225 The directory portion may or may not be returned with a trailing '/'.
227 The results can be passed to L</catpath()> to get back a path equivalent to
228 (usually identical to) the original path.
233 my ($self,$path, $nofile) = @_;
235 my ($volume,$directory,$file) = ('','','');
241 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
246 return ($volume,$directory,$file);
252 The opposite of L</catdir()>.
254 @dirs = File::Spec->splitdir( $directories );
256 $directories must be only the directory portion of the path on systems
257 that have the concept of a volume or that have path syntax that differentiates
258 files from directories.
260 Unlike just splitting the directories on the separator, empty
261 directory names (C<''>) can be returned, because these are significant
266 File::Spec->splitdir( "/a/b//c/" );
270 ( '', 'a', 'b', '', 'c', '' )
275 my ($self,$directories) = @_ ;
277 # split() likes to forget about trailing null fields, so here we
278 # check to be sure that there will not be any before handling the
281 if ( $directories !~ m|/\Z(?!\n)| ) {
282 return split( m|/|, $directories );
286 # since there was a trailing separator, add a file name to the end,
287 # then do the split, then replace it with ''.
289 my( @directories )= split( m|/|, "${directories}dummy" ) ;
290 $directories[ $#directories ]= '' ;
291 return @directories ;
298 Takes volume, directory and file portions and returns an entire path. Under
299 Unix, $volume is ignored, and directory and file are catenated. A '/' is
300 inserted if need be. On other OSs, $volume is significant.
305 my ($self,$volume,$directory,$file) = @_;
307 if ( $directory ne '' &&
309 substr( $directory, -1 ) ne '/' &&
310 substr( $file, 0, 1 ) ne '/'
312 $directory .= "/$file" ;
315 $directory .= $file ;
323 Takes a destination path and an optional base path returns a relative path
324 from the base path to the destination path:
326 $rel_path = File::Spec->abs2rel( $path ) ;
327 $rel_path = File::Spec->abs2rel( $path, $base ) ;
329 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
330 then it is converted to absolute form using L</rel2abs()>. This means that it
331 is taken to be relative to L<cwd()|Cwd>.
333 On systems with the concept of a volume, this assumes that both paths
334 are on the $destination volume, and ignores the $base volume.
336 On systems that have a grammar that indicates filenames, this ignores the
337 $base filename as well. Otherwise all path components are assumed to be
340 If $path is relative, it is converted to absolute form using L</rel2abs()>.
341 This means that it is taken to be relative to L<cwd()|Cwd>.
343 No checks against the filesystem are made. On VMS, there is
344 interaction with the working environment, as logicals and
347 Based on code written by Shigio Yamaguchi.
352 my($self,$path,$base) = @_;
355 if ( ! $self->file_name_is_absolute( $path ) ) {
356 $path = $self->rel2abs( $path ) ;
359 $path = $self->canonpath( $path ) ;
362 # Figure out the effective $base and clean it up.
363 if ( !defined( $base ) || $base eq '' ) {
366 elsif ( ! $self->file_name_is_absolute( $base ) ) {
367 $base = $self->rel2abs( $base ) ;
370 $base = $self->canonpath( $base ) ;
373 # Now, remove all leading components that are the same
374 my @pathchunks = $self->splitdir( $path);
375 my @basechunks = $self->splitdir( $base);
377 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
382 $path = CORE::join( '/', @pathchunks );
383 $base = CORE::join( '/', @basechunks );
385 # $base now contains the directories the resulting relative path
386 # must ascend out of before it can descend to $path_directory. So,
387 # replace all names with $parentDir
388 $base =~ s|[^/]+|..|g ;
390 # Glue the two together, using a separator if necessary, and preventing an
392 if ( $path ne '' && $base ne '' ) {
393 $path = "$base/$path" ;
395 $path = "$base$path" ;
398 return $self->canonpath( $path ) ;
403 Converts a relative path to an absolute path.
405 $abs_path = File::Spec->rel2abs( $path ) ;
406 $abs_path = File::Spec->rel2abs( $path, $base ) ;
408 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
409 then it is converted to absolute form using L</rel2abs()>. This means that it
410 is taken to be relative to L<cwd()|Cwd>.
412 On systems with the concept of a volume, this assumes that both paths
413 are on the $base volume, and ignores the $path volume.
415 On systems that have a grammar that indicates filenames, this ignores the
416 $base filename as well. Otherwise all path components are assumed to be
419 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
421 No checks against the filesystem are made. On VMS, there is
422 interaction with the working environment, as logicals and
425 Based on code written by Shigio Yamaguchi.
430 my ($self,$path,$base ) = @_;
433 if ( ! $self->file_name_is_absolute( $path ) ) {
434 # Figure out the effective $base and clean it up.
435 if ( !defined( $base ) || $base eq '' ) {
438 elsif ( ! $self->file_name_is_absolute( $base ) ) {
439 $base = $self->rel2abs( $base ) ;
442 $base = $self->canonpath( $base ) ;
446 $path = $self->catdir( $base, $path ) ;
449 return $self->canonpath( $path ) ;