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:
127 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
128 is tainted, it is not used.
134 return $tmpdir if defined $tmpdir;
135 my @dirlist = ($ENV{TMPDIR}, "/tmp");
138 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
139 require Scalar::Util;
140 shift @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
144 next unless defined && -d && -w _;
148 $tmpdir = '' unless defined $tmpdir;
154 Returns a string representation of the parent directory. ".." on UNIX.
164 Given a list of file names, strip out those that refer to a parent
165 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
171 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
176 Returns a true or false value indicating, respectively, that alphabetic
177 is not or is significant when comparing file specifications.
185 =item file_name_is_absolute
187 Takes as argument a path and returns true if it is an absolute path.
189 This does not consult the local filesystem on Unix, Win32, OS/2 or Mac
190 OS (Classic). It does consult the working environment for VMS (see
191 L<File::Spec::VMS/file_name_is_absolute>).
195 sub file_name_is_absolute {
196 my ($self,$file) = @_;
197 return scalar($file =~ m:^/:s);
202 Takes no argument, returns the environment variable PATH as an array.
207 my @path = split(':', $ENV{PATH});
208 foreach (@path) { $_ = '.' if $_ eq '' }
214 join is the same as catfile.
220 return $self->catfile(@_);
225 ($volume,$directories,$file) = File::Spec->splitpath( $path );
226 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
228 Splits a path in to volume, directory, and filename portions. On systems
229 with no concept of volume, returns undef for volume.
231 For systems with no syntax differentiating filenames from directories,
232 assumes that the last file is a path unless $no_file is true or a
233 trailing separator or /. or /.. is present. On Unix this means that $no_file
234 true makes this return ( '', $path, '' ).
236 The directory portion may or may not be returned with a trailing '/'.
238 The results can be passed to L</catpath()> to get back a path equivalent to
239 (usually identical to) the original path.
244 my ($self,$path, $nofile) = @_;
246 my ($volume,$directory,$file) = ('','','');
252 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
257 return ($volume,$directory,$file);
263 The opposite of L</catdir()>.
265 @dirs = File::Spec->splitdir( $directories );
267 $directories must be only the directory portion of the path on systems
268 that have the concept of a volume or that have path syntax that differentiates
269 files from directories.
271 Unlike just splitting the directories on the separator, empty
272 directory names (C<''>) can be returned, because these are significant
277 File::Spec->splitdir( "/a/b//c/" );
281 ( '', 'a', 'b', '', 'c', '' )
286 my ($self,$directories) = @_ ;
288 # split() likes to forget about trailing null fields, so here we
289 # check to be sure that there will not be any before handling the
292 if ( $directories !~ m|/\Z(?!\n)| ) {
293 return split( m|/|, $directories );
297 # since there was a trailing separator, add a file name to the end,
298 # then do the split, then replace it with ''.
300 my( @directories )= split( m|/|, "${directories}dummy" ) ;
301 $directories[ $#directories ]= '' ;
302 return @directories ;
309 Takes volume, directory and file portions and returns an entire path. Under
310 Unix, $volume is ignored, and directory and file are catenated. A '/' is
311 inserted if need be. On other OSs, $volume is significant.
316 my ($self,$volume,$directory,$file) = @_;
318 if ( $directory ne '' &&
320 substr( $directory, -1 ) ne '/' &&
321 substr( $file, 0, 1 ) ne '/'
323 $directory .= "/$file" ;
326 $directory .= $file ;
334 Takes a destination path and an optional base path returns a relative path
335 from the base path to the destination path:
337 $rel_path = File::Spec->abs2rel( $path ) ;
338 $rel_path = File::Spec->abs2rel( $path, $base ) ;
340 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
341 then it is converted to absolute form using L</rel2abs()>. This means that it
342 is taken to be relative to L<cwd()|Cwd>.
344 On systems with the concept of a volume, this assumes that both paths
345 are on the $destination volume, and ignores the $base volume.
347 On systems that have a grammar that indicates filenames, this ignores the
348 $base filename as well. Otherwise all path components are assumed to be
351 If $path is relative, it is converted to absolute form using L</rel2abs()>.
352 This means that it is taken to be relative to L<cwd()|Cwd>.
354 No checks against the filesystem are made. On VMS, there is
355 interaction with the working environment, as logicals and
358 Based on code written by Shigio Yamaguchi.
363 my($self,$path,$base) = @_;
366 if ( ! $self->file_name_is_absolute( $path ) ) {
367 $path = $self->rel2abs( $path ) ;
370 $path = $self->canonpath( $path ) ;
373 # Figure out the effective $base and clean it up.
374 if ( !defined( $base ) || $base eq '' ) {
377 elsif ( ! $self->file_name_is_absolute( $base ) ) {
378 $base = $self->rel2abs( $base ) ;
381 $base = $self->canonpath( $base ) ;
384 # Now, remove all leading components that are the same
385 my @pathchunks = $self->splitdir( $path);
386 my @basechunks = $self->splitdir( $base);
388 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
393 $path = CORE::join( '/', @pathchunks );
394 $base = CORE::join( '/', @basechunks );
396 # $base now contains the directories the resulting relative path
397 # must ascend out of before it can descend to $path_directory. So,
398 # replace all names with $parentDir
399 $base =~ s|[^/]+|..|g ;
401 # Glue the two together, using a separator if necessary, and preventing an
403 if ( $path ne '' && $base ne '' ) {
404 $path = "$base/$path" ;
406 $path = "$base$path" ;
409 return $self->canonpath( $path ) ;
414 Converts a relative path to an absolute path.
416 $abs_path = File::Spec->rel2abs( $path ) ;
417 $abs_path = File::Spec->rel2abs( $path, $base ) ;
419 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
420 then it is converted to absolute form using L</rel2abs()>. This means that it
421 is taken to be relative to L<cwd()|Cwd>.
423 On systems with the concept of a volume, this assumes that both paths
424 are on the $base volume, and ignores the $path volume.
426 On systems that have a grammar that indicates filenames, this ignores the
427 $base filename as well. Otherwise all path components are assumed to be
430 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
432 No checks against the filesystem are made. On VMS, there is
433 interaction with the working environment, as logicals and
436 Based on code written by Shigio Yamaguchi.
441 my ($self,$path,$base ) = @_;
444 if ( ! $self->file_name_is_absolute( $path ) ) {
445 # Figure out the effective $base and clean it up.
446 if ( !defined( $base ) || $base eq '' ) {
449 elsif ( ! $self->file_name_is_absolute( $base ) ) {
450 $base = $self->rel2abs( $base ) ;
453 $base = $self->canonpath( $base ) ;
457 $path = $self->catdir( $base, $path ) ;
460 return $self->canonpath( $path ) ;