1 package File::Spec::Win32;
6 require File::Spec::Unix;
7 @ISA = qw(File::Spec::Unix);
11 File::Spec::Win32 - methods for Win32 file specs
15 require File::Spec::Win32; # Done internally by File::Spec if needed
19 See File::Spec::Unix for a documentation of the methods provided
20 there. This package overrides the implementation of these methods, not
27 Returns a string representation of the null device.
37 Returns a string representation of the first existing directory
38 from the following list:
50 return $tmpdir if defined $tmpdir;
52 foreach (@ENV{qw(TMPDIR TEMP TMP)}, qw(/tmp /)) {
53 next unless defined && -d;
57 $tmpdir = '' unless defined $tmpdir;
58 $tmpdir = $self->canonpath($tmpdir);
66 sub file_name_is_absolute {
67 my ($self,$file) = @_;
68 return scalar($file =~ m{^([a-z]:)?[\\/]}is);
73 Concatenate one or more directory names and a filename to form a
74 complete path ending with a filename
81 return $file unless @_;
82 my $dir = $self->catdir(@_);
83 $dir .= "\\" unless substr($dir,-1) eq "\\";
88 my $path = $ENV{'PATH'} || $ENV{'Path'} || $ENV{'path'};
89 my @path = split(';',$path);
90 foreach (@path) { $_ = '.' if $_ eq '' }
96 No physical check on the filesystem, but a logical cleanup of a
97 path. On UNIX eliminated successive slashes and successive "/.".
102 my ($self,$path) = @_;
103 $path =~ s/^([a-z]:)/\u$1/s;
105 $path =~ s|([^\\])\\+|$1\\|g; # xx////xx -> xx/xx
106 $path =~ s|(\\\.)+\\|\\|g; # xx/././xx -> xx/xx
107 $path =~ s|^(\.\\)+||s unless $path eq ".\\"; # ./xx -> xx
108 $path =~ s|\\\Z(?!\n)||
109 unless $path =~ m#^([A-Z]:)?\\\Z(?!\n)#s; # xx/ -> xx
115 ($volume,$directories,$file) = File::Spec->splitpath( $path );
116 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
118 Splits a path in to volume, directory, and filename portions. Assumes that
119 the last file is a path unless the path ends in '\\', '\\.', '\\..'
120 or $no_file is true. On Win32 this means that $no_file true makes this return
121 ( $volume, $path, undef ).
123 Separators accepted are \ and /.
125 Volumes can be drive letters or UNC sharenames (\\server\share).
127 The results can be passed to L</catpath> to get back a path equivalent to
128 (usually identical to) the original path.
133 my ($self,$path, $nofile) = @_;
134 my ($volume,$directory,$file) = ('','','');
137 m{^( (?:[a-zA-Z]:|(?:\\\\|//)[^\\/]+[\\/][^\\/]+)? )
145 m{^ ( (?: [a-zA-Z]: |
146 (?:\\\\|//)[^\\/]+[\\/][^\\/]+
149 ( (?:.*[\\\\/](?:\.\.?\Z(?!\n))?)? )
157 return ($volume,$directory,$file);
163 The opposite of L</catdir()>.
165 @dirs = File::Spec->splitdir( $directories );
167 $directories must be only the directory portion of the path on systems
168 that have the concept of a volume or that have path syntax that differentiates
169 files from directories.
171 Unlike just splitting the directories on the separator, leading empty and
172 trailing directory entries can be returned, because these are significant
175 File::Spec->splitdir( "/a/b/c" );
179 ( '', 'a', 'b', '', 'c', '' )
184 my ($self,$directories) = @_ ;
186 # split() likes to forget about trailing null fields, so here we
187 # check to be sure that there will not be any before handling the
190 if ( $directories !~ m|[\\/]\Z(?!\n)| ) {
191 return split( m|[\\/]|, $directories );
195 # since there was a trailing separator, add a file name to the end,
196 # then do the split, then replace it with ''.
198 my( @directories )= split( m|[\\/]|, "${directories}dummy" ) ;
199 $directories[ $#directories ]= '' ;
200 return @directories ;
207 Takes volume, directory and file portions and returns an entire path. Under
208 Unix, $volume is ignored, and this is just like catfile(). On other OSs,
209 the $volume become significant.
214 my ($self,$volume,$directory,$file) = @_;
216 # If it's UNC, make sure the glue separator is there, reusing
217 # whatever separator is first in the $volume
219 if ( $volume =~ m@^([\\/])[\\/][^\\/]+[\\/][^\\/]+\Z(?!\n)@s &&
220 $directory =~ m@^[^\\/]@s
223 $volume .= $directory ;
225 # If the volume is not just A:, make sure the glue separator is
226 # there, reusing whatever separator is first in the $volume if possible.
227 if ( $volume !~ m@^[a-zA-Z]:\Z(?!\n)@s &&
228 $volume =~ m@[^\\/]\Z(?!\n)@ &&
231 $volume =~ m@([\\/])@ ;
232 my $sep = $1 ? $1 : '\\' ;
244 Takes a destination path and an optional base path returns a relative path
245 from the base path to the destination path:
247 $rel_path = File::Spec->abs2rel( $destination ) ;
248 $rel_path = File::Spec->abs2rel( $destination, $base ) ;
250 If $base is not present or '', then L</cwd()> is used. If $base is relative,
251 then it is converted to absolute form using L</rel2abs()>. This means that it
252 is taken to be relative to L<cwd()>.
254 On systems with the concept of a volume, this assumes that both paths
255 are on the $destination volume, and ignores the $base volume.
257 On systems that have a grammar that indicates filenames, this ignores the
258 $base filename as well. Otherwise all path components are assumed to be
261 If $path is relative, it is converted to absolute form using L</rel2abs()>.
262 This means that it is taken to be relative to L</cwd()>.
264 Based on code written by Shigio Yamaguchi.
266 No checks against the filesystem are made.
271 my($self,$path,$base) = @_;
274 if ( ! $self->file_name_is_absolute( $path ) ) {
275 $path = $self->rel2abs( $path ) ;
278 $path = $self->canonpath( $path ) ;
281 # Figure out the effective $base and clean it up.
282 if ( ! $self->file_name_is_absolute( $base ) ) {
283 $base = $self->rel2abs( $base ) ;
285 elsif ( !defined( $base ) || $base eq '' ) {
289 $base = $self->canonpath( $base ) ;
293 my ( $path_volume, $path_directories, $path_file ) =
294 $self->splitpath( $path, 1 ) ;
296 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
298 # Now, remove all leading components that are the same
299 my @pathchunks = $self->splitdir( $path_directories );
300 my @basechunks = $self->splitdir( $base_directories );
302 while ( @pathchunks &&
304 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
310 # No need to catdir, we know these are well formed.
311 $path_directories = CORE::join( '\\', @pathchunks );
312 $base_directories = CORE::join( '\\', @basechunks );
314 # $base_directories now contains the directories the resulting relative
315 # path must ascend out of before it can descend to $path_directory. So,
316 # replace all names with $parentDir
318 #FA Need to replace between backslashes...
319 $base_directories =~ s|[^\\]+|..|g ;
321 # Glue the two together, using a separator if necessary, and preventing an
324 #FA Must check that new directories are not empty.
325 if ( $path_directories ne '' && $base_directories ne '' ) {
326 $path_directories = "$base_directories\\$path_directories" ;
328 $path_directories = "$base_directories$path_directories" ;
331 # It makes no sense to add a relative path to a UNC volume
332 $path_volume = '' unless $path_volume =~ m{^[A-Z]:}is ;
334 return $self->canonpath(
335 $self->catpath($path_volume, $path_directories, $path_file )
341 Converts a relative path to an absolute path.
343 $abs_path = File::Spec->rel2abs( $destination ) ;
344 $abs_path = File::Spec->rel2abs( $destination, $base ) ;
346 If $base is not present or '', then L<cwd()> is used. If $base is relative,
347 then it is converted to absolute form using L</rel2abs()>. This means that it
348 is taken to be relative to L</cwd()>.
350 Assumes that both paths are on the $base volume, and ignores the
353 On systems that have a grammar that indicates filenames, this ignores the
354 $base filename as well. Otherwise all path components are assumed to be
357 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
359 Based on code written by Shigio Yamaguchi.
361 No checks against the filesystem are made.
366 my ($self,$path,$base ) = @_;
368 if ( ! $self->file_name_is_absolute( $path ) ) {
370 if ( !defined( $base ) || $base eq '' ) {
373 elsif ( ! $self->file_name_is_absolute( $base ) ) {
374 $base = $self->rel2abs( $base ) ;
377 $base = $self->canonpath( $base ) ;
380 my ( $path_directories, $path_file ) =
381 ($self->splitpath( $path, 1 ))[1,2] ;
383 my ( $base_volume, $base_directories ) =
384 $self->splitpath( $base, 1 ) ;
386 $path = $self->catpath(
388 $self->catdir( $base_directories, $path_directories ),
393 return $self->canonpath( $path ) ;