File Coverage

blib/lib/LibZip/InitLib.pm
Criterion Covered Total %
statement 50 117 42.7
branch 14 64 21.8
condition 2 24 8.3
subroutine 10 13 76.9
pod 0 8 0.0
total 76 226 33.6


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: InitLib.pm
3             ## Purpose: LibZip::InitLib
4             ## Author: Graciliano M. P.
5             ## Modified by:
6             ## Created: 2004-06-06
7             ## RCS-ID:
8             ## Copyright: (c) 2004 Graciliano M. P.
9             ## Licence: This program is free software; you can redistribute it and/or
10             ## modify it under the same terms as Perl itself
11             #############################################################################
12              
13             package LibZip::InitLib ;
14              
15 2 50   2   79 BEGIN { $INC{'LibZip/InitLib.pm'} = 1 if !$INC{'LibZip/InitLib.pm'} ;}
16              
17             $VERSION = '0.01' ;
18              
19 2     2   12 no warnings ;
  2         4  
  2         86  
20              
21 2     2   10 use LibZip::CORE ;
  2         10  
  2         11  
22              
23             ########
24             # VARS #
25             ########
26              
27             my ( @INC_ORG , @DIRTMP ) ;
28              
29             $CALL_BEGIN = 0 if !defined $CALL_BEGIN ;
30              
31             ## $LIBZIP , $LIBTMP , $LIBTMPFIX , $FILETMP , @LYB
32              
33             ###########
34             # ALIASES #
35             ###########
36              
37 1     1 0 106 sub LIBZIP { begin() ; "$LIBZIP" ;}
  1         13  
38 0     0 0 0 sub LIBTMP { begin() ; "$LIBTMP" ;}
  0         0  
39              
40             #########
41             # BEGIN #
42             #########
43              
44             sub begin {
45 3 100   3 0 18 return if $CALL_BEGIN ;
46 2         11 $CALL_BEGIN = 1 ;
47              
48 2 50       443 if (-d './lib') { splice(@INC,-1,0,'./lib') ;}
  0         0  
49            
50 2         42 @LYB = (0..9,'a'..'z','A'..'Z') ;
51            
52 2         9 define_lib() ;
53             }
54              
55             ####################
56             # DEFINE_REAL_PATH #
57             ####################
58              
59             sub define_real_path {
60 0     0 0 0 eval{ require FindBin ;};
  0         0  
61              
62 0 0       0 if ( !$@ ) {
63 0 0       0 if ( $LIBZIP !~ /^(?:\w+:\/|\/)/ ) {
64 0         0 $LIBZIP =~ s/^\.?\/*// ;
65 0         0 $LIBZIP = "$FindBin::RealBin/$LIBZIP" ;
66             }
67            
68 0 0       0 if ( $LIBTMP !~ /^(?:\w+:\/|\/)/ ) {
69 0         0 my $real_libtmp = $LIBTMP ;
70 0         0 $real_libtmp =~ s/^\.?\/*// ;
71 0         0 $real_libtmp = "$FindBin::RealBin/$real_libtmp" ;
72            
73 0         0 foreach my $INC_i ( @INC ) {
74 0 0       0 next if ref $INC_i ;
75 0 0       0 if ( $INC_i eq $LIBTMP ) { $INC_i = $real_libtmp ;}
  0 0       0  
76 0         0 elsif ( $INC_i =~ /^\Q$LIBTMP\E[\\\/](.*)/ ) { $INC_i = "$real_libtmp/$1" ;}
77             }
78            
79 0         0 $LIBTMP = $real_libtmp ;
80             }
81            
82 0 0       0 if ( $FILETMP !~ /^(?:\w+:\/|\/)/ ) {
83 0         0 $FILETMP =~ s/^\.?\/*// ;
84 0         0 $FILETMP = "$FindBin::RealBin/$FILETMP" ;
85             }
86            
87 0 0       0 if ( $^X !~ /^(?:\w+:\/|\/)/ ) {
88 0         0 my ($name) = ( $^X =~ /([^\\\/]+)$/ );
89 0         0 $^X = "$FindBin::RealBin/$name" ;
90             }
91            
92 0         0 $0 = $FindBin::RealScript = $FindBin::Script = $^X ;
93            
94 0         0 my $fix_lib = "$FindBin::RealBin/lib" ;
95 0 0       0 if ( -d $fix_lib ) { unshift (@INC, $fix_lib) ;}
  0         0  
96             }
97              
98 0 0 0     0 if ($^O=~/(msw|win|dos)/i && $^X !~ /\.exe$/ && -e "$^X.exe") { $^X .= '.exe' ;}
  0   0     0  
99            
100 0 0       0 if ( LibZip::lib_has_dynaLoader() ) {
101 0         0 delete $INC{'DynaLoader.pm'} ;
102 0         0 require DynaLoader ;
103 0         0 my $bootstrap = \&DynaLoader::bootstrap ;
104            
105             *DynaLoader::bootstrap = sub {
106 0 0   0   0 LibZip::check_pack_dep("$_[0].pm") if defined &LibZip::check_pack_dep ;
107 0         0 &$bootstrap(@_) ;
108             }
109 0         0 }
110            
111 0         0 $@ = undef ;
112            
113 0         0 my (@inc_ok , %inc_ok) ;
114 0         0 foreach my $INC_i ( @INC ) {
115 0 0       0 if ( ref $INC_i ) { push(@inc_ok , $INC_i) ;}
  0         0  
116             else {
117 0         0 $INC_i =~ s/[\\\/]+$// ;
118 0 0       0 push(@inc_ok , $INC_i) if !$inc_ok{$INC_i}++ ;
119             }
120             }
121 0         0 @INC = @inc_ok ;
122              
123 0         0 return ;
124             }
125              
126             ##############
127             # DEFINE_LIB #
128             ##############
129              
130             sub define_lib {
131 2     2 0 11 my @find_lib = find_lib() ;
132            
133 2         14 $LIBZIP = find_file('lib.zip',@find_lib,'.') ;
134            
135 2         5 my $libtmp ;
136            
137 2         5 foreach my $find_lib_i ( @find_lib ) {
138 24 50 33     76 if ($find_lib_i =~ /[\\\/]site$/i && $find_lib_i =~ /perl/) {
139 0         0 my $tmp_lib = "$find_lib_i/libzip-tmp" ;
140 0 0       0 if (! -d $tmp_lib) { mkdir($tmp_lib,0775) ;}
  0         0  
141 0 0 0     0 if (-d $tmp_lib && -r $tmp_lib && -w $tmp_lib) { $libtmp = $tmp_lib ; last ;}
  0   0     0  
  0         0  
142             }
143             }
144            
145 2 50       9 if ($libtmp eq '') {
146 2         6 foreach my $find_lib_i ( @find_lib ) {
147 24 50 33     89 if ($find_lib_i =~ /[\\\/]lib$/i && $find_lib_i =~ /perl/) {
148 0         0 my $tmp_lib = "$find_lib_i/libzip-tmp" ;
149 0 0       0 if (! -d $tmp_lib) { mkdir($tmp_lib,0775) ;}
  0         0  
150 0 0 0     0 if (-d $tmp_lib && -r $tmp_lib && -w $tmp_lib) { $libtmp = $tmp_lib ; last ;}
  0   0     0  
  0         0  
151             }
152             }
153             }
154              
155 2 50       19 if ($libtmp eq '') {
156 2         11 my $tmp = find_file('libzip-tmp',@find_lib,'.') ;
157 2 50       15 if ( -d $tmp ) {
158 0         0 $libtmp = $tmp ;
159 0         0 $LIBTMPFIX = 1 ;
160             }
161             }
162              
163 2 50       10 if ($libtmp eq '') { $libtmp = new_tempdir('.') ;}
  2         16  
164            
165 2         6 $LIBTMP = $libtmp ;
166 2         5 push(@INC , $LIBTMP) ;
167            
168 2         10 $FILETMP = "$LIBTMP/pm-$$-zip.tmp" ;
169 2         159 open (FILETMP,">$FILETMP") ;
170            
171             ##print "DEFINED>> $LIBZIP >> $LIBTMP >>$^X\n" ;
172            
173             }
174              
175             ###############
176             # NEW_TEMPDIR #
177             ###############
178              
179             sub new_tempdir {
180 2     2 0 6 my ( $lib ) = @_ ;
181              
182 2         4 my $rand ;
183 2         12 while(length($rand) < 4) { $rand .= $LYB[rand(@LYB)] ;}
  8         242  
184            
185 2         31 my $file = "$lib/libzip-$$-$rand-tmp" ;
186            
187 2 50       59 if (-e $file) { $file = &new_tempdir($_[0],1) ;}
  0         0  
188            
189 2 50       9 if (! $_[1]) {
190 2         275 mkdir($file,0775) ;
191 2         6 push(@DIRTMP , $file) ;
192             }
193            
194 2         5 return( $file ) ;
195             }
196              
197             ############
198             # FIND_LIB #
199             ############
200              
201             sub find_lib {
202 2 50   2 0 23 @INC_ORG = @INC if !@INC_ORG ;
203              
204 2         10 my @find_lib = @INC_ORG ;
205            
206 2 50       34 if ( $^X !~ /(?:^|[\\\/])perl(?:\.\w+)?$/i ) {
207 0         0 my $exec_path = $^X ;
208 0         0 $exec_path =~ s/[\\\/]+[^\\\/]+$//gs ;
209 0         0 $exec_path =~ s/[\\\/]+$// ;
210 0         0 $exec_path .= "/lib" ;
211 0 0       0 push(@find_lib , $exec_path) if -d $exec_path ;
212             }
213            
214 2         8 foreach my $find_lib_i ( @find_lib ) { $find_lib_i =~ s/[\\\/]+[^\\\/]+[\\\/]*$// ;}
  24         166  
215            
216 2         28 return @find_lib ;
217             }
218              
219             #######
220             # END #
221             #######
222              
223             sub end {
224 4     4 0 11 foreach my $DIRTMP_i ( @DIRTMP ) { rmdir($DIRTMP_i) ;}
  4         316  
225             }
226              
227 2     2   6 sub END { &end ;}
228              
229             #######
230             # END #
231             #######
232              
233             1;
234              
235