1 package File::Spec::OS2;
4 use vars qw(@ISA $VERSION);
5 require File::Spec::Unix;
8 $VERSION = eval $VERSION;
10 @ISA = qw(File::Spec::Unix);
20 sub file_name_is_absolute {
21 my ($self,$file) = @_;
22 return scalar($file =~ m{^([a-z]:)?[\\/]}is);
26 my $path = $ENV{PATH};
28 my @path = split(';',$path);
29 foreach (@path) { $_ = '.' if $_ eq '' }
34 # In OS/2 the "require Cwd" is unnecessary bloat.
35 return Cwd::sys_cwd();
40 return $tmpdir if defined $tmpdir;
41 my @d = @ENV{qw(TMPDIR TEMP TMP)}; # function call could autovivivy
42 $tmpdir = $_[0]->_tmpdir( @d, '/tmp', '/' );
50 # append a backslash to each argument unless it has one there
51 $_ .= "/" unless m{/$};
53 return $self->canonpath(join('', @args));
57 my ($self,$path) = @_;
58 return unless defined $path;
60 $path =~ s/^([a-z]:)/\l$1/s;
62 $path =~ s|([^/])/+|$1/|g; # xx////xx -> xx/xx
63 $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
64 $path =~ s|^(\./)+(?=[^/])||s; # ./xx -> xx
65 $path =~ s|/\Z(?!\n)||
66 unless $path =~ m#^([a-z]:)?/\Z(?!\n)#si;# xx/ -> xx
67 $path =~ s{^/\.\.$}{/}; # /.. -> /
68 1 while $path =~ s{^/\.\.}{}; # /../xx -> /xx
74 my ($self,$path, $nofile) = @_;
75 my ($volume,$directory,$file) = ('','','');
78 m{^( (?:[a-zA-Z]:|(?:\\\\|//)[^\\/]+[\\/][^\\/]+)? )
87 (?:\\\\|//)[^\\/]+[\\/][^\\/]+
90 ( (?:.*[\\\\/](?:\.\.?\Z(?!\n))?)? )
98 return ($volume,$directory,$file);
103 my ($self,$directories) = @_ ;
104 split m|[\\/]|, $directories, -1;
109 my ($self,$volume,$directory,$file) = @_;
111 # If it's UNC, make sure the glue separator is there, reusing
112 # whatever separator is first in the $volume
114 if ( $volume =~ m@^([\\/])[\\/][^\\/]+[\\/][^\\/]+\Z(?!\n)@s &&
115 $directory =~ m@^[^\\/]@s
118 $volume .= $directory ;
120 # If the volume is not just A:, make sure the glue separator is
121 # there, reusing whatever separator is first in the $volume if possible.
122 if ( $volume !~ m@^[a-zA-Z]:\Z(?!\n)@s &&
123 $volume =~ m@[^\\/]\Z(?!\n)@ &&
126 $volume =~ m@([\\/])@ ;
127 my $sep = $1 ? $1 : '/' ;
138 my($self,$path,$base) = @_;
141 if ( ! $self->file_name_is_absolute( $path ) ) {
142 $path = $self->rel2abs( $path ) ;
144 $path = $self->canonpath( $path ) ;
147 # Figure out the effective $base and clean it up.
148 if ( !defined( $base ) || $base eq '' ) {
149 $base = $self->_cwd();
150 } elsif ( ! $self->file_name_is_absolute( $base ) ) {
151 $base = $self->rel2abs( $base ) ;
153 $base = $self->canonpath( $base ) ;
157 my ( $path_volume, $path_directories, $path_file ) = $self->splitpath( $path, 1 ) ;
158 my ( $base_volume, $base_directories ) = $self->splitpath( $base, 1 ) ;
159 return $path unless $path_volume eq $base_volume;
161 # Now, remove all leading components that are the same
162 my @pathchunks = $self->splitdir( $path_directories );
163 my @basechunks = $self->splitdir( $base_directories );
165 while ( @pathchunks &&
167 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
173 # No need to catdir, we know these are well formed.
174 $path_directories = CORE::join( '/', @pathchunks );
175 $base_directories = CORE::join( '/', @basechunks );
177 # $base_directories now contains the directories the resulting relative
178 # path must ascend out of before it can descend to $path_directory. So,
179 # replace all names with $parentDir
181 #FA Need to replace between backslashes...
182 $base_directories =~ s|[^\\/]+|..|g ;
184 # Glue the two together, using a separator if necessary, and preventing an
187 #FA Must check that new directories are not empty.
188 if ( $path_directories ne '' && $base_directories ne '' ) {
189 $path_directories = "$base_directories/$path_directories" ;
191 $path_directories = "$base_directories$path_directories" ;
194 return $self->canonpath(
195 $self->catpath( "", $path_directories, $path_file )
201 my ($self,$path,$base ) = @_;
203 if ( ! $self->file_name_is_absolute( $path ) ) {
205 if ( !defined( $base ) || $base eq '' ) {
206 $base = $self->_cwd();
208 elsif ( ! $self->file_name_is_absolute( $base ) ) {
209 $base = $self->rel2abs( $base ) ;
212 $base = $self->canonpath( $base ) ;
215 my ( $path_directories, $path_file ) =
216 ($self->splitpath( $path, 1 ))[1,2] ;
218 my ( $base_volume, $base_directories ) =
219 $self->splitpath( $base, 1 ) ;
221 $path = $self->catpath(
223 $self->catdir( $base_directories, $path_directories ),
228 return $self->canonpath( $path ) ;
236 File::Spec::OS2 - methods for OS/2 file specs
240 require File::Spec::OS2; # Done internally by File::Spec if needed
244 See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
245 implementation of these methods, not the semantics.
247 Amongst the changes made for OS/2 are...
253 Modifies the list of places temp directory information is looked for.
263 Volumes can be drive letters or UNC sharenames (\\server\share).
269 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
271 This program is free software; you can redistribute it and/or modify
272 it under the same terms as Perl itself.