line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Stable::Module; |
2
|
|
|
|
|
|
|
###################################################################### |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Stable::Module - frequently used modules on Perl5 application |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# http://search.cpan.org/dist/Stable-Module/ |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# Copyright (c) 2014, 2016, 2017, 2018 INABA Hitoshi in a CPAN |
9
|
|
|
|
|
|
|
###################################################################### |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.08'; |
12
|
|
|
|
|
|
|
$VERSION = $VERSION; |
13
|
|
|
|
|
|
|
|
14
|
25
|
|
|
25
|
|
72285
|
use 5.00503; |
|
25
|
|
|
|
|
174
|
|
15
|
25
|
|
|
25
|
|
107
|
use strict; |
|
25
|
|
|
|
|
42
|
|
|
25
|
|
|
|
|
792
|
|
16
|
|
|
|
|
|
|
local $^W = 1; |
17
|
25
|
|
|
25
|
|
121
|
use Carp; |
|
25
|
|
|
|
|
36
|
|
|
25
|
|
|
|
|
1114
|
|
18
|
25
|
|
|
25
|
|
124
|
use FindBin; |
|
25
|
|
|
|
|
55
|
|
|
25
|
|
|
|
|
692
|
|
19
|
25
|
|
|
25
|
|
9308
|
use IO::File; |
|
25
|
|
|
|
|
167302
|
|
|
25
|
|
|
|
|
2308
|
|
20
|
|
|
|
|
|
|
|
21
|
25
|
|
|
25
|
|
152
|
use vars qw($re_char $hide_stderr); |
|
25
|
|
|
|
|
41
|
|
|
25
|
|
|
|
|
3943
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub VERSION { |
24
|
0
|
|
|
0
|
0
|
0
|
my($self,$version) = @_; |
25
|
0
|
0
|
|
|
|
0
|
if ($version != $Stable::Module::VERSION) { |
26
|
0
|
|
|
|
|
0
|
my($package,$filename,$line) = caller; |
27
|
0
|
|
|
|
|
0
|
die "$filename requires Stable::Module $version, this is version $Stable::Module::VERSION, stopped at $filename line $line.\n"; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub BEGIN { |
32
|
25
|
50
|
33
|
25
|
|
228
|
if (($^O eq 'MSWin32') and (defined $ENV{'OS'}) and ($ENV{'OS'} eq 'Windows_NT')) { |
|
|
|
33
|
|
|
|
|
33
|
0
|
|
|
|
|
0
|
$hide_stderr = '2>NUL'; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
else { |
36
|
25
|
|
|
|
|
7552
|
$hide_stderr = ''; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub import { |
41
|
25
|
|
|
25
|
|
225
|
my($caller,$filename,$line) = caller; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# verify that we're called correctly so that strictures will work. |
44
|
25
|
50
|
|
|
|
171
|
if (__FILE__ !~ m{ \b Stable[/\\]Module\.pm \z}x) { |
45
|
0
|
|
|
|
|
0
|
die "Incorrect use of module '${\__PACKAGE__}' at $filename line $line.\n"; |
|
0
|
|
|
|
|
0
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# get /./ (dot: one character) regexp |
49
|
25
|
|
|
|
|
91
|
$re_char = qr/[\x00-\xFF]/; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Code Page Identifiers (Microsoft Windows) |
52
|
|
|
|
|
|
|
# Identifier .NET Name Additional information |
53
|
25
|
|
|
|
|
267
|
my %re_char = ( |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Sjis shift_jis ANSI/OEM Japanese; Japanese (Shift-JIS) |
56
|
|
|
|
|
|
|
'932' => qr{[\x81-\x9F\xE0-\xFC][\x00-\xFF]|[\x00-\xFF]}, |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# GBK gb2312 ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) |
59
|
|
|
|
|
|
|
'936' => qr{[\x81-\xFE][\x00-\xFF]|[\x00-\xFF]}, |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# UHC ks_c_5601-1987 ANSI/OEM Korean (Unified Hangul Code) |
62
|
|
|
|
|
|
|
'949' => qr{[\x81-\xFE][\x00-\xFF]|[\x00-\xFF]}, |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Big5Plus big5 ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) |
65
|
|
|
|
|
|
|
'950' => qr{[\x81-\xFE][\x00-\xFF]|[\x00-\xFF]}, |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Big5HKSCS HKSCS support on top of traditional Chinese Windows |
68
|
|
|
|
|
|
|
'951' => qr{[\x81-\xFE][\x00-\xFF]|[\x00-\xFF]}, |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# GB18030 GB18030 Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030) |
71
|
|
|
|
|
|
|
'54936' => qr{[\x81-\xFE][\x30-\x39][\x81-\xFE][\x30-\x39]|[\x81-\xFE][\x00-\xFF]|[\x00-\xFF]}, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
25
|
|
|
|
|
47
|
my $codepage = 'File System Safe'; |
75
|
25
|
50
|
|
|
|
111
|
if ($^O =~ m{\A (?:MSWin32|NetWare|symbian|dos) \z}oxms) { |
76
|
0
|
|
|
|
|
0
|
$codepage = (qx{chcp} =~ m{ ([0-9]{3,5}) \Z}oxms)[0]; |
77
|
0
|
|
0
|
|
|
0
|
$re_char = $re_char{$codepage} || $re_char; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
25
|
|
|
25
|
|
162
|
no strict 'refs'; |
|
25
|
|
|
|
|
41
|
|
|
25
|
|
|
|
|
67318
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# use Cwd qw(cwd); |
83
|
25
|
|
|
|
|
116
|
require Cwd; |
84
|
25
|
50
|
|
|
|
87
|
if ($^O =~ m{\A (?:MSWin32|NetWare|symbian|dos) \z}oxms) { |
85
|
0
|
|
|
0
|
|
0
|
*{$caller.'::cwd'} = sub { _dos_path(Cwd::cwd) }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
25
|
|
|
|
|
51
|
*{$caller.'::cwd'} = \&Cwd::cwd; |
|
25
|
|
|
|
|
136
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# use FindBin qw($Bin); |
92
|
25
|
|
|
|
|
85
|
require FindBin; |
93
|
25
|
50
|
|
|
|
90
|
if ($^O =~ m{\A (?:MSWin32|NetWare|symbian|dos) \z}oxms) { |
94
|
0
|
|
|
|
|
0
|
$FindBin::Bin = _dos_path($FindBin::Bin); |
95
|
|
|
|
|
|
|
} |
96
|
25
|
|
|
|
|
43
|
*{$caller.'::Bin'} = \$FindBin::Bin; |
|
25
|
|
|
|
|
79
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# use File::Basename qw(fileparse basename dirname); |
99
|
|
|
|
|
|
|
# use internal routines always for compatibility -- for example, perl 5.6 has incompatible File::Basename |
100
|
25
|
|
|
|
|
40
|
if (1 or exists $re_char{$codepage}) { |
101
|
25
|
|
|
|
|
242
|
$INC{'File/Basename.pm'} = join '/', $FindBin::Bin, __FILE__; |
102
|
25
|
|
|
|
|
62
|
*{$caller.'::fileparse'} = \&_fileparse; |
|
25
|
|
|
|
|
99
|
|
103
|
25
|
|
|
|
|
46
|
*{$caller.'::basename'} = \&_basename; |
|
25
|
|
|
|
|
70
|
|
104
|
25
|
|
|
|
|
42
|
*{$caller.'::dirname'} = \&_dirname; |
|
25
|
|
|
|
|
72
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
|
|
|
|
|
|
require File::Basename; |
108
|
|
|
|
|
|
|
*{$caller.'::fileparse'} = \&File::Basename::fileparse; |
109
|
|
|
|
|
|
|
*{$caller.'::basename'} = \&File::Basename::basename; |
110
|
|
|
|
|
|
|
*{$caller.'::dirname'} = \&File::Basename::dirname; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# use File::Path qw(mkpath rmtree); |
114
|
25
|
50
|
|
|
|
68
|
if (exists $re_char{$codepage}) { |
115
|
0
|
|
|
|
|
0
|
$INC{'File/Path.pm'} = join '/', $FindBin::Bin, __FILE__; |
116
|
0
|
|
|
|
|
0
|
*{$caller.'::mkpath'} = \&_mkpath; |
|
0
|
|
|
|
|
0
|
|
117
|
0
|
|
|
|
|
0
|
*{$caller.'::rmtree'} = \&_rmtree; |
|
0
|
|
|
|
|
0
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
25
|
|
|
|
|
93
|
require File::Path; |
121
|
25
|
|
|
|
|
47
|
*{$caller.'::mkpath'} = \&File::Path::mkpath; |
|
25
|
|
|
|
|
177
|
|
122
|
25
|
|
|
|
|
66
|
*{$caller.'::rmtree'} = \&File::Path::rmtree; |
|
25
|
|
|
|
|
171
|
|
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# use File::Copy qw(copy move); |
126
|
25
|
50
|
|
|
|
79
|
if (exists $re_char{$codepage}) { |
127
|
0
|
|
|
|
|
0
|
$INC{'File/Copy.pm'} = join '/', $FindBin::Bin, __FILE__; |
128
|
0
|
|
|
|
|
0
|
*{$caller.'::copy'} = \&_copy; |
|
0
|
|
|
|
|
0
|
|
129
|
0
|
|
|
|
|
0
|
*{$caller.'::move'} = \&_move; |
|
0
|
|
|
|
|
0
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
else { |
132
|
25
|
|
|
|
|
10290
|
require File::Copy; |
133
|
25
|
|
|
|
|
89818
|
*{$caller.'::copy'} = \&File::Copy::copy; |
|
25
|
|
|
|
|
124
|
|
134
|
25
|
|
|
|
|
49
|
*{$caller.'::move'} = \&File::Copy::move; |
|
25
|
|
|
|
|
67
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# use File::Compare qw(compare); |
138
|
25
|
|
|
|
|
8778
|
require File::Compare; |
139
|
25
|
|
|
|
|
19185
|
*{$caller.'::compare'} = \&File::Compare::compare; |
|
25
|
|
|
|
|
127
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# use Sys::Hostname qw(hostname); |
142
|
25
|
|
|
|
|
8541
|
require Sys::Hostname; |
143
|
25
|
|
|
|
|
20395
|
*{$caller.'::hostname'} = \&Sys::Hostname::hostname; |
|
25
|
|
|
|
|
155
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# use Time::Local qw(timelocal); |
146
|
25
|
|
|
|
|
9299
|
require Time::Local; |
147
|
25
|
|
|
|
|
58804
|
*{$caller.'::timelocal'} = \&Time::Local::timelocal; |
|
25
|
|
|
|
|
142
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# internal List::Util::reduce |
150
|
|
|
|
|
|
|
sub _reduce (&@) { |
151
|
5
|
|
|
5
|
|
11
|
my $coderef = shift @_; |
152
|
5
|
|
|
|
|
9
|
local $a = shift @_; |
153
|
5
|
|
|
|
|
15
|
for $b (@_) { |
154
|
77
|
|
|
|
|
89
|
$a = $coderef->(); |
155
|
|
|
|
|
|
|
} |
156
|
5
|
|
|
|
|
25
|
return $a; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# use List::Util qw(reduce first shuffle max maxstr min minstr sum); |
160
|
25
|
|
|
|
|
1388
|
*{$caller.'::first'} = \&_first; |
|
25
|
|
|
|
|
3071
|
|
161
|
25
|
|
|
|
|
1255
|
*{$caller.'::shuffle'} = \&_shuffle; |
|
25
|
|
|
|
|
140
|
|
162
|
25
|
50
|
|
9
|
|
1341
|
*{$caller.'::max'} = sub { _reduce { $a > $b ? $a : $b } @_ }; |
|
25
|
|
|
|
|
1358
|
|
|
1
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
13
|
|
163
|
25
|
50
|
|
1
|
|
2962
|
*{$caller.'::maxstr'} = sub { _reduce { $a gt $b ? $a : $b } @_ }; |
|
25
|
|
|
|
|
1300
|
|
|
1
|
|
|
|
|
19
|
|
|
25
|
|
|
|
|
40
|
|
164
|
25
|
50
|
|
1
|
|
1276
|
*{$caller.'::min'} = sub { _reduce { $a < $b ? $a : $b } @_ }; |
|
25
|
|
|
|
|
75
|
|
|
1
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
15
|
|
165
|
25
|
50
|
|
1
|
|
3123
|
*{$caller.'::minstr'} = sub { _reduce { $a lt $b ? $a : $b } @_ }; |
|
25
|
|
|
|
|
1808
|
|
|
1
|
|
|
|
|
17
|
|
|
25
|
|
|
|
|
36
|
|
166
|
25
|
|
|
1
|
|
84
|
*{$caller.'::sum'} = sub { _reduce { $a + $b } @_ }; |
|
25
|
|
|
|
|
69
|
|
|
1
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
11
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# use List::MoreUtils qw(all any none notall uniq); |
169
|
25
|
100
|
|
4
|
|
64
|
*{$caller.'::all'} = sub (&@) { my $coderef = shift @_; for (@_) { return 0 if not $coderef->($_) } return 1; }; # All arguments are true |
|
25
|
|
|
|
|
61
|
|
|
4
|
|
|
|
|
106
|
|
|
4
|
|
|
|
|
8
|
|
|
10
|
|
|
|
|
27
|
|
|
1
|
|
|
|
|
4
|
|
170
|
25
|
100
|
|
4
|
|
64
|
*{$caller.'::any'} = sub (&@) { my $coderef = shift @_; for (@_) { return 1 if $coderef->($_) } return 0; }; # One argument is true |
|
25
|
|
|
|
|
64
|
|
|
4
|
|
|
|
|
104
|
|
|
4
|
|
|
|
|
6
|
|
|
10
|
|
|
|
|
26
|
|
|
1
|
|
|
|
|
4
|
|
171
|
25
|
100
|
|
4
|
|
58
|
*{$caller.'::none'} = sub (&@) { my $coderef = shift @_; for (@_) { return 0 if $coderef->($_) } return 1; }; # All arguments are false |
|
25
|
|
|
|
|
208
|
|
|
4
|
|
|
|
|
108
|
|
|
4
|
|
|
|
|
9
|
|
|
10
|
|
|
|
|
22
|
|
|
1
|
|
|
|
|
5
|
|
172
|
25
|
100
|
|
4
|
|
122
|
*{$caller.'::notall'} = sub (&@) { my $coderef = shift @_; for (@_) { return 1 if not $coderef->($_) } return 0; }; # One argument is false |
|
25
|
|
|
|
|
74
|
|
|
4
|
|
|
|
|
101
|
|
|
4
|
|
|
|
|
7
|
|
|
10
|
|
|
|
|
25
|
|
|
1
|
|
|
|
|
3
|
|
173
|
25
|
|
|
|
|
40
|
*{$caller.'::uniq'} = \&_uniq; |
|
25
|
|
|
|
|
55
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# use feature qw(say); |
176
|
25
|
|
|
|
|
47
|
*{$caller.'::say'} = \&_say; |
|
25
|
|
|
|
|
63
|
|
177
|
25
|
50
|
|
|
|
28334
|
*IO::Handle::say = \&_say if not defined(*IO::Handle::say); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
0
|
|
|
sub unimport { |
181
|
|
|
|
|
|
|
# nothing |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _fileparse { |
185
|
8
|
|
|
8
|
|
2224
|
my($fullname,@suffixlist) = @_; |
186
|
|
|
|
|
|
|
|
187
|
8
|
50
|
|
|
|
20
|
if (not defined $fullname) { |
188
|
0
|
|
|
|
|
0
|
croak "fileparse(): need a valid pathname"; |
189
|
|
|
|
|
|
|
} |
190
|
8
|
|
|
|
|
13
|
my $taint = substr($fullname,0,0); # Is $fullname tainted? |
191
|
|
|
|
|
|
|
|
192
|
8
|
|
|
|
|
11
|
my $dirname = ''; |
193
|
8
|
|
|
|
|
9
|
my $subdir = ''; |
194
|
8
|
|
|
|
|
64
|
while ($fullname =~ m{\G ($re_char) }oxmsgc) { |
195
|
163
|
|
|
|
|
201
|
my $char = $1; |
196
|
163
|
100
|
|
|
|
229
|
if ($char =~ m{\A [:\\/] \z}oxms) { |
197
|
31
|
|
|
|
|
40
|
$dirname .= $subdir; |
198
|
31
|
|
|
|
|
32
|
$dirname .= $char; |
199
|
31
|
|
|
|
|
60
|
$subdir = ''; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
else { |
202
|
132
|
|
|
|
|
233
|
$subdir .= $char; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
8
|
|
|
|
|
11
|
my $name = $subdir; |
206
|
|
|
|
|
|
|
|
207
|
8
|
50
|
33
|
|
|
32
|
if (($dirname eq '') or ($dirname =~ m{ : \z}oxms)) { |
208
|
0
|
|
|
|
|
0
|
$dirname .= '.\\'; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# ignore case of name |
212
|
8
|
|
|
|
|
55
|
my @char = $name =~ m{\G ($re_char) }oxmsgc; |
213
|
8
|
|
|
|
|
16
|
my $name_lc = join '', map { lcfirst } @char; |
|
47
|
|
|
|
|
71
|
|
214
|
8
|
|
|
|
|
15
|
my $suffix = ''; |
215
|
8
|
100
|
|
|
|
15
|
if (@suffixlist) { |
216
|
3
|
|
|
|
|
6
|
for my $s (@suffixlist) { |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# ignore case of suffix |
219
|
3
|
|
|
|
|
23
|
my @char = $s =~ m{\G ($re_char) }oxmsgc; |
220
|
3
|
|
|
|
|
5
|
my $s_lc = join '', map { lcfirst } @char; |
|
11
|
|
|
|
|
18
|
|
221
|
3
|
100
|
|
|
|
9
|
if (substr($name_lc,-length($s_lc),length($s_lc)) eq $s_lc) { |
222
|
2
|
|
|
|
|
3
|
$taint .= substr($s,0,0); |
223
|
2
|
|
|
|
|
5
|
$suffix = substr($name,-length($s_lc),length($s_lc)) . $suffix; |
224
|
2
|
|
|
|
|
5
|
$name = substr($name,0,length($name)-length($s_lc)); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# Ensure taint is propagated from the path to its pieces. |
230
|
8
|
|
|
|
|
10
|
$name .= $taint; |
231
|
8
|
|
|
|
|
10
|
$dirname .= $taint; |
232
|
8
|
|
|
|
|
8
|
$suffix .= $taint; |
233
|
8
|
50
|
|
|
|
16
|
if (wantarray) { |
234
|
8
|
|
|
|
|
33
|
return ($name,$dirname,$suffix); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
else { |
237
|
0
|
|
|
|
|
0
|
return $name; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub _basename { |
242
|
5
|
|
|
5
|
|
168
|
my($fullname,@suffixlist) = @_; |
243
|
|
|
|
|
|
|
|
244
|
5
|
50
|
|
|
|
11
|
if (not defined $fullname) { |
245
|
0
|
|
|
|
|
0
|
croak "basename(): need a valid pathname"; |
246
|
|
|
|
|
|
|
} |
247
|
5
|
|
|
|
|
9
|
my $taint = substr($fullname,0,0); # Is $fullname tainted? |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# From BSD basename(1) |
250
|
|
|
|
|
|
|
# The basename utility deletes any prefix ending with the last slash '/' |
251
|
|
|
|
|
|
|
# character present in string (after first stripping trailing slashes) |
252
|
5
|
|
|
|
|
10
|
$fullname = _strip_trailing_sep($fullname); |
253
|
|
|
|
|
|
|
|
254
|
5
|
|
|
|
|
6
|
my $dirname = ''; |
255
|
5
|
|
|
|
|
6
|
my $subdir = ''; |
256
|
5
|
|
|
|
|
24
|
while ($fullname =~ m{\G ($re_char) }oxmsgc) { |
257
|
76
|
|
|
|
|
94
|
my $char = $1; |
258
|
76
|
100
|
|
|
|
106
|
if ($char =~ m{\A [:\\/] \z}oxms) { |
259
|
15
|
|
|
|
|
20
|
$dirname .= $subdir; |
260
|
15
|
|
|
|
|
13
|
$dirname .= $char; |
261
|
15
|
|
|
|
|
29
|
$subdir = ''; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
else { |
264
|
61
|
|
|
|
|
108
|
$subdir .= $char; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
} |
267
|
5
|
|
|
|
|
6
|
my $name = $subdir; |
268
|
|
|
|
|
|
|
|
269
|
5
|
50
|
33
|
|
|
18
|
if (($dirname eq '') or ($dirname =~ m{ : \z}oxms)) { |
270
|
0
|
|
|
|
|
0
|
$dirname .= '.\\'; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# ignore case of name |
274
|
5
|
|
|
|
|
30
|
my @char = $name =~ m{\G ($re_char) }oxmsgc; |
275
|
5
|
|
|
|
|
8
|
my $name_lc = join '', map { lcfirst } @char; |
|
27
|
|
|
|
|
39
|
|
276
|
5
|
|
|
|
|
11
|
my $suffix = ''; |
277
|
5
|
100
|
|
|
|
10
|
if (@suffixlist) { |
278
|
2
|
|
|
|
|
4
|
for my $s (@suffixlist) { |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
# ignore case of suffix |
281
|
2
|
|
|
|
|
19
|
my @char = $s =~ m{\G ($re_char) }oxmsgc; |
282
|
2
|
|
|
|
|
5
|
my $s_lc = join '', map { lcfirst } @char; |
|
8
|
|
|
|
|
12
|
|
283
|
2
|
100
|
|
|
|
8
|
if (substr($name_lc,-length($s_lc),length($s_lc)) eq $s_lc) { |
284
|
1
|
|
|
|
|
2
|
$taint .= substr($s,0,0); |
285
|
1
|
|
|
|
|
2
|
$suffix = substr($name,-length($s_lc),length($s_lc)) . $suffix; |
286
|
1
|
|
|
|
|
4
|
$name = substr($name,0,length($name)-length($s_lc)); |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# From BSD basename(1) |
292
|
|
|
|
|
|
|
# The suffix is not stripped if it is identical to the remaining |
293
|
|
|
|
|
|
|
# characters in string. |
294
|
5
|
50
|
33
|
|
|
9
|
if (($name eq '') and ($suffix ne '')) { |
295
|
0
|
|
|
|
|
0
|
$name = $suffix; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# Ensure that basename '/' == '/' |
299
|
5
|
50
|
|
|
|
8
|
if ($name eq '') { |
300
|
0
|
|
|
|
|
0
|
$name = $dirname; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# Ensure taint is propagated from the path to its pieces. |
304
|
5
|
|
|
|
|
7
|
$name .= $taint; |
305
|
5
|
|
|
|
|
17
|
return $name; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub _dirname { |
309
|
2
|
|
|
2
|
|
61
|
my($fullname) = @_; |
310
|
|
|
|
|
|
|
|
311
|
2
|
50
|
|
|
|
8
|
if (not defined $fullname) { |
312
|
0
|
|
|
|
|
0
|
croak "dirname(): need a valid pathname"; |
313
|
|
|
|
|
|
|
} |
314
|
2
|
|
|
|
|
4
|
my $taint = substr($fullname,0,0); # Is $fullname tainted? |
315
|
|
|
|
|
|
|
|
316
|
2
|
|
|
|
|
6
|
my($basename,$dirname) = _fileparse($fullname); |
317
|
2
|
|
|
|
|
5
|
$dirname = _strip_trailing_sep($dirname); |
318
|
|
|
|
|
|
|
|
319
|
2
|
50
|
|
|
|
6
|
if ($basename eq '') { |
320
|
0
|
|
|
|
|
0
|
($basename,$dirname) = _fileparse($dirname); |
321
|
0
|
|
|
|
|
0
|
$dirname = _strip_trailing_sep($dirname); |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
# Ensure taint is propagated from the path to its pieces. |
325
|
2
|
|
|
|
|
3
|
$dirname .= $taint; |
326
|
2
|
|
|
|
|
5
|
return $dirname; |
327
|
|
|
|
|
|
|
} |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
sub _strip_trailing_sep { |
330
|
7
|
|
|
7
|
|
11
|
my($dirname) = @_; |
331
|
|
|
|
|
|
|
|
332
|
7
|
|
|
|
|
106
|
my @char = $dirname =~ m{\G ([\\\/]+|$re_char) }oxmsgc; |
333
|
7
|
50
|
|
|
|
18
|
if (scalar(@char) >= 2) { |
334
|
7
|
100
|
66
|
|
|
28
|
if (($char[-1] =~ m{\A [\\\/]+ \z}oxms) and ($char[-2] ne ':')) { |
335
|
3
|
|
|
|
|
6
|
pop @char; |
336
|
|
|
|
|
|
|
} |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
7
|
|
|
|
|
26
|
return join '', @char; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
sub _mkpath { |
343
|
0
|
|
|
0
|
|
0
|
my $path = _dos_path($_[0]); |
344
|
|
|
|
|
|
|
|
345
|
0
|
0
|
0
|
|
|
0
|
if (_is_directory($_[0])) { # *NOT* _is_directory($path) |
|
|
0
|
|
|
|
|
|
346
|
0
|
|
|
|
|
0
|
return 1; |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
# cmd.exe on Windows NT, Windows 2000, Windows XP, Windows 2003 or later |
350
|
|
|
|
|
|
|
elsif ((defined $ENV{'OS'}) and ($ENV{'OS'} eq 'Windows_NT')) { |
351
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{cmd.exe /E:ON /C mkdir $path >NUL $hide_stderr}) == 0) { |
352
|
0
|
|
|
|
|
0
|
return 1; |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# COMMAND.COM on Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition |
357
|
|
|
|
|
|
|
else { |
358
|
0
|
|
|
|
|
0
|
my @subdir = (); |
359
|
0
|
|
|
|
|
0
|
my $i = 0; |
360
|
0
|
|
|
|
|
0
|
while ($_[0] =~ m{\G ($re_char) }oxmsgc) { |
361
|
0
|
|
|
|
|
0
|
my $char = $1; |
362
|
0
|
0
|
|
|
|
0
|
if ($char =~ m{\A [\\/] \z}oxms) { |
363
|
0
|
|
|
|
|
0
|
$i++; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
else { |
366
|
0
|
|
|
|
|
0
|
$subdir[$i] .= $char; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
} |
369
|
0
|
0
|
|
|
|
0
|
if (@subdir >= 2) { |
370
|
0
|
|
|
|
|
0
|
for my $i (0 .. $#subdir-1) { |
371
|
0
|
|
|
|
|
0
|
my $path = _dos_path(join '\\',@subdir[0..$i]); |
372
|
0
|
0
|
|
|
|
0
|
if (not _is_directory($path)) { |
373
|
0
|
|
|
|
|
0
|
CORE::system(qq{mkdir $path >NUL}); |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
} |
377
|
0
|
|
|
|
|
0
|
my $path = _dos_path(join '\\',@subdir); |
378
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{mkdir $path >NUL}) == 0) { |
379
|
0
|
|
|
|
|
0
|
return 1; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
} |
382
|
|
|
|
|
|
|
|
383
|
0
|
0
|
|
|
|
0
|
if (exists $INC{'Strict/Perl.pm'}) { |
384
|
0
|
|
|
|
|
0
|
croak "mkpath: $path"; |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
else { |
387
|
0
|
|
|
|
|
0
|
return undef; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
sub _rmtree { |
392
|
0
|
|
|
0
|
|
0
|
my $root = _dos_path($_[0]); |
393
|
|
|
|
|
|
|
|
394
|
0
|
0
|
0
|
|
|
0
|
if (not _is_directory($_[0])) { # *NOT* not _is_directory($root) |
|
|
0
|
|
|
|
|
|
395
|
0
|
|
|
|
|
0
|
return 1; |
396
|
|
|
|
|
|
|
} |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
# cmd.exe on Windows NT, Windows 2000, Windows XP, Windows 2003 or later |
399
|
|
|
|
|
|
|
elsif ((defined $ENV{'OS'}) and ($ENV{'OS'} eq 'Windows_NT')) { |
400
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{rmdir /S /Q $root >NUL $hide_stderr}) == 0) { |
401
|
0
|
|
|
|
|
0
|
return 1; |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# COMMAND.COM on Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition |
406
|
|
|
|
|
|
|
else { |
407
|
0
|
|
|
|
|
0
|
my @file = split /\n/, qx{dir /s /b /a-d $root}; |
408
|
0
|
|
|
|
|
0
|
for my $file (@file) { |
409
|
0
|
|
|
|
|
0
|
$file = _dos_path($file); |
410
|
0
|
|
|
|
|
0
|
CORE::system(qq{del $file >NUL}); |
411
|
|
|
|
|
|
|
} |
412
|
0
|
|
|
|
|
0
|
my @dir = split /\n/, qx{dir /s /b /ad $root}; |
413
|
0
|
|
|
|
|
0
|
for my $dir (sort { length($b) <=> length($a) } @dir) { |
|
0
|
|
|
|
|
0
|
|
414
|
0
|
|
|
|
|
0
|
$dir = _dos_path($dir); |
415
|
0
|
|
|
|
|
0
|
CORE::system(qq{rmdir $dir >NUL}); |
416
|
|
|
|
|
|
|
} |
417
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{rmdir $root >NUL}) == 0) { |
418
|
0
|
|
|
|
|
0
|
return 1; |
419
|
|
|
|
|
|
|
} |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
0
|
0
|
|
|
|
0
|
if (exists $INC{'Strict/Perl.pm'}) { |
423
|
0
|
|
|
|
|
0
|
croak "rmdir: $root"; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
else { |
426
|
0
|
|
|
|
|
0
|
return undef; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
sub _copy { |
431
|
0
|
|
|
0
|
|
0
|
my $source = _dos_path($_[0]); |
432
|
0
|
|
|
|
|
0
|
my $dest = _dos_path($_[1]); |
433
|
|
|
|
|
|
|
|
434
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{copy /Y $source $dest >NUL $hide_stderr}) == 0) { |
|
|
0
|
|
|
|
|
|
435
|
0
|
|
|
|
|
0
|
return 1; |
436
|
|
|
|
|
|
|
} |
437
|
|
|
|
|
|
|
elsif (exists $INC{'Strict/Perl.pm'}) { |
438
|
0
|
|
|
|
|
0
|
croak "copy: $source $dest"; |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
else { |
441
|
0
|
|
|
|
|
0
|
return undef; |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
sub _move { |
446
|
0
|
|
|
0
|
|
0
|
my $source = _dos_path($_[0]); |
447
|
0
|
|
|
|
|
0
|
my $dest = _dos_path($_[1]); |
448
|
|
|
|
|
|
|
|
449
|
0
|
0
|
|
|
|
0
|
if (CORE::system(qq{move /Y $source $dest >NUL $hide_stderr}) == 0) { |
|
|
0
|
|
|
|
|
|
450
|
0
|
|
|
|
|
0
|
return 1; |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
elsif (exists $INC{'Strict/Perl.pm'}) { |
453
|
0
|
|
|
|
|
0
|
croak "move: $source $dest"; |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
else { |
456
|
0
|
|
|
|
|
0
|
return undef; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
sub _dos_path { |
461
|
0
|
|
|
0
|
|
0
|
my($path) = @_; |
462
|
|
|
|
|
|
|
|
463
|
0
|
|
|
|
|
0
|
my @char = $path =~ m{\G ($re_char) }oxmsg; |
464
|
0
|
0
|
|
|
|
0
|
$path = join '', map {{'/' => '\\'}->{$_} || $_} @char; |
|
0
|
|
|
|
|
0
|
|
465
|
0
|
0
|
|
|
|
0
|
$path = qq{"$path"} if $path =~ m{ }; |
466
|
0
|
|
|
|
|
0
|
return $path; |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
sub _is_directory { |
470
|
0
|
|
|
0
|
|
0
|
my($unknown) = @_; |
471
|
|
|
|
|
|
|
|
472
|
0
|
0
|
|
|
|
0
|
if (-e $unknown) { |
|
|
0
|
|
|
|
|
|
473
|
0
|
|
|
|
|
0
|
return -d _; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
elsif (_MSWin32_5Cended_path($unknown)) { |
476
|
0
|
|
|
|
|
0
|
return -d "$unknown/."; |
477
|
|
|
|
|
|
|
} |
478
|
0
|
|
|
|
|
0
|
return undef; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
sub _MSWin32_5Cended_path { |
482
|
0
|
0
|
0
|
0
|
|
0
|
if ((@_ >= 1) and ($_[0] ne '')) { |
483
|
0
|
0
|
|
|
|
0
|
if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) { |
484
|
0
|
|
|
|
|
0
|
my @char = $_[0] =~ m{\G ($re_char) }oxmsg; |
485
|
0
|
0
|
|
|
|
0
|
if ($char[-1] =~ m{ \x5C \z}oxms) { |
486
|
0
|
|
|
|
|
0
|
return 1; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
} |
490
|
0
|
|
|
|
|
0
|
return undef; |
491
|
|
|
|
|
|
|
} |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
sub _first(&@) { |
494
|
1
|
|
|
1
|
|
9
|
my $coderef = shift @_; |
495
|
1
|
|
|
|
|
3
|
for (@_) { |
496
|
4
|
100
|
|
|
|
14
|
if ($coderef->()) { |
497
|
1
|
|
|
|
|
5
|
return $_; |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
} |
500
|
0
|
|
|
|
|
0
|
return undef; |
501
|
|
|
|
|
|
|
} |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
sub _shuffle(@) { |
504
|
1
|
|
|
1
|
|
19
|
my @a = \(@_); |
505
|
1
|
|
|
|
|
1
|
my $n; |
506
|
1
|
|
|
|
|
2
|
my $i=@_; |
507
|
1
|
|
|
|
|
37
|
return map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; |
|
26
|
|
|
|
|
50
|
|
|
26
|
|
|
|
|
25
|
|
|
26
|
|
|
|
|
44
|
|
508
|
|
|
|
|
|
|
} |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
sub _uniq { |
511
|
1
|
|
|
1
|
|
10
|
my %seen = (); |
512
|
1
|
|
|
|
|
3
|
return grep { not $seen{$_}++ } @_; |
|
10
|
|
|
|
|
20
|
|
513
|
|
|
|
|
|
|
} |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
sub _say { |
516
|
1
|
|
|
1
|
|
44
|
my $currfh = select(); |
517
|
1
|
|
|
|
|
2
|
my $handle; |
518
|
|
|
|
|
|
|
{ |
519
|
25
|
|
|
25
|
|
168
|
no strict 'refs'; |
|
25
|
|
|
|
|
40
|
|
|
25
|
|
|
|
|
6278
|
|
|
1
|
|
|
|
|
2
|
|
520
|
1
|
50
|
|
|
|
6
|
$handle = defined(fileno($_[0])) ? shift : \*$currfh; |
521
|
|
|
|
|
|
|
} |
522
|
1
|
50
|
|
|
|
5
|
@_ = ($_) unless @_; |
523
|
1
|
|
|
|
|
2
|
return print {$handle} @_, "\n"; |
|
1
|
|
|
|
|
40
|
|
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
1; |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
__END__ |