1 | @rem = '--*-Perl-*--
|
---|
2 | @echo off
|
---|
3 | if "%OS%" == "Windows_NT" goto WinNT
|
---|
4 | perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
|
---|
5 | goto endofperl
|
---|
6 | :WinNT
|
---|
7 | perl -x -S %0 %*
|
---|
8 | if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
|
---|
9 | if %errorlevel% == 9009 echo You do not have Perl in your PATH.
|
---|
10 | if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
|
---|
11 | goto endofperl
|
---|
12 | @rem ';
|
---|
13 | #!perl
|
---|
14 | #line 15
|
---|
15 | eval 'exec c:\shaoqunWu\perl\bin\perl.exe -S $0 ${1+"$@"}'
|
---|
16 | if $running_under_some_shell;
|
---|
17 | (my $perlpath = <<'/../') =~ s/\s*\z//;
|
---|
18 | c:\shaoqunWu\perl\bin\perl.exe
|
---|
19 | /../
|
---|
20 | use strict;
|
---|
21 | use vars qw/$statdone/;
|
---|
22 | use File::Spec::Functions 'curdir';
|
---|
23 | my $startperl = "#! $perlpath -w";
|
---|
24 |
|
---|
25 | #
|
---|
26 | # Modified September 26, 1993 to provide proper handling of years after 1999
|
---|
27 | # Tom Link <[email protected]>
|
---|
28 | # University of Pittsburgh
|
---|
29 | #
|
---|
30 | # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
|
---|
31 | # Billy Constantine <[email protected]> <[email protected]>
|
---|
32 | # University of Adelaide, Adelaide, South Australia
|
---|
33 | #
|
---|
34 | # Modified 1999-06-10, 1999-07-07 to migrate to cleaner perl5 usage
|
---|
35 | # Ken Pizzini <[email protected]>
|
---|
36 | #
|
---|
37 | # Modified 2000-01-28 to use the 'follow' option of File::Find
|
---|
38 |
|
---|
39 | sub tab ();
|
---|
40 | sub n ($$);
|
---|
41 | sub fileglob_to_re ($);
|
---|
42 | sub quote ($);
|
---|
43 |
|
---|
44 | my @roots = ();
|
---|
45 | while ($ARGV[0] =~ /^[^-!(]/) {
|
---|
46 | push(@roots, shift);
|
---|
47 | }
|
---|
48 | @roots = (curdir()) unless @roots;
|
---|
49 | for (@roots) { $_ = quote($_) }
|
---|
50 | my $roots = join(', ', @roots);
|
---|
51 |
|
---|
52 | my $find = "find";
|
---|
53 | my $indent_depth = 1;
|
---|
54 | my $stat = 'lstat';
|
---|
55 | my $decl = '';
|
---|
56 | my $flushall = '';
|
---|
57 | my $initfile = '';
|
---|
58 | my $initnewer = '';
|
---|
59 | my $out = '';
|
---|
60 | my $declaresubs = "sub wanted;\n";
|
---|
61 | my %init = ();
|
---|
62 | my ($follow_in_effect,$Skip_And) = (0,0);
|
---|
63 | my $print_needed = 1;
|
---|
64 |
|
---|
65 | while (@ARGV) {
|
---|
66 | $_ = shift;
|
---|
67 | s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
|
---|
68 | if ($_ eq '(') {
|
---|
69 | $out .= tab . "(\n";
|
---|
70 | $indent_depth++;
|
---|
71 | next;
|
---|
72 | } elsif ($_ eq ')') {
|
---|
73 | --$indent_depth;
|
---|
74 | $out .= tab . ")";
|
---|
75 | } elsif ($_ eq 'follow') {
|
---|
76 | $follow_in_effect= 1;
|
---|
77 | $stat = 'stat';
|
---|
78 | $Skip_And= 1;
|
---|
79 | } elsif ($_ eq '!') {
|
---|
80 | $out .= tab . "!";
|
---|
81 | next;
|
---|
82 | } elsif (/^(i)?name$/) {
|
---|
83 | $out .= tab . '/' . fileglob_to_re(shift) . "/s$1";
|
---|
84 | } elsif (/^(i)?path$/) {
|
---|
85 | $out .= tab . '$File::Find::name =~ /' . fileglob_to_re(shift) . "/s$1";
|
---|
86 | } elsif ($_ eq 'perm') {
|
---|
87 | my $onum = shift;
|
---|
88 | $onum =~ /^-?[0-7]+$/
|
---|
89 | || die "Malformed -perm argument: $onum\n";
|
---|
90 | $out .= tab;
|
---|
91 | if ($onum =~ s/^-//) {
|
---|
92 | $onum = sprintf("0%o", oct($onum) & 07777);
|
---|
93 | $out .= "((\$mode & $onum) == $onum)";
|
---|
94 | } else {
|
---|
95 | $onum =~ s/^0*/0/;
|
---|
96 | $out .= "((\$mode & 0777) == $onum)";
|
---|
97 | }
|
---|
98 | } elsif ($_ eq 'type') {
|
---|
99 | (my $filetest = shift) =~ tr/s/S/;
|
---|
100 | $out .= tab . "-$filetest _";
|
---|
101 | } elsif ($_ eq 'print') {
|
---|
102 | $out .= tab . 'print("$name\n")';
|
---|
103 | $print_needed = 0;
|
---|
104 | } elsif ($_ eq 'print0') {
|
---|
105 | $out .= tab . 'print("$name\0")';
|
---|
106 | $print_needed = 0;
|
---|
107 | } elsif ($_ eq 'fstype') {
|
---|
108 | my $type = shift;
|
---|
109 | $out .= tab;
|
---|
110 | if ($type eq 'nfs') {
|
---|
111 | $out .= '($dev < 0)';
|
---|
112 | } else {
|
---|
113 | $out .= '($dev >= 0)'; #XXX
|
---|
114 | }
|
---|
115 | } elsif ($_ eq 'user') {
|
---|
116 | my $uname = shift;
|
---|
117 | $out .= tab . "(\$uid == \$uid{'$uname'})";
|
---|
118 | $init{user} = 1;
|
---|
119 | } elsif ($_ eq 'group') {
|
---|
120 | my $gname = shift;
|
---|
121 | $out .= tab . "(\$gid == \$gid{'$gname'})";
|
---|
122 | $init{group} = 1;
|
---|
123 | } elsif ($_ eq 'nouser') {
|
---|
124 | $out .= tab . '!exists $uid{$uid}';
|
---|
125 | $init{user} = 1;
|
---|
126 | } elsif ($_ eq 'nogroup') {
|
---|
127 | $out .= tab . '!exists $gid{$gid}';
|
---|
128 | $init{group} = 1;
|
---|
129 | } elsif ($_ eq 'links') {
|
---|
130 | $out .= tab . n('$nlink', shift);
|
---|
131 | } elsif ($_ eq 'inum') {
|
---|
132 | $out .= tab . n('$ino', shift);
|
---|
133 | } elsif ($_ eq 'size') {
|
---|
134 | $_ = shift;
|
---|
135 | my $n = 'int(((-s _) + 511) / 512)';
|
---|
136 | if (s/c\z//) {
|
---|
137 | $n = 'int(-s _)';
|
---|
138 | } elsif (s/k\z//) {
|
---|
139 | $n = 'int(((-s _) + 1023) / 1024)';
|
---|
140 | }
|
---|
141 | $out .= tab . n($n, $_);
|
---|
142 | } elsif ($_ eq 'atime') {
|
---|
143 | $out .= tab . n('int(-A _)', shift);
|
---|
144 | } elsif ($_ eq 'mtime') {
|
---|
145 | $out .= tab . n('int(-M _)', shift);
|
---|
146 | } elsif ($_ eq 'ctime') {
|
---|
147 | $out .= tab . n('int(-C _)', shift);
|
---|
148 | } elsif ($_ eq 'exec') {
|
---|
149 | my @cmd = ();
|
---|
150 | while (@ARGV && $ARGV[0] ne ';')
|
---|
151 | { push(@cmd, shift) }
|
---|
152 | shift;
|
---|
153 | $out .= tab;
|
---|
154 | if ($cmd[0] =~m#^(?:(?:/usr)?/bin/)?rm$#
|
---|
155 | && $cmd[$#cmd] eq '{}'
|
---|
156 | && (@cmd == 2 || (@cmd == 3 && $cmd[1] eq '-f'))) {
|
---|
157 | if (@cmd == 2) {
|
---|
158 | $out .= '(unlink($_) || warn "$name: $!\n")';
|
---|
159 | } elsif (!@ARGV) {
|
---|
160 | $out .= 'unlink($_)';
|
---|
161 | } else {
|
---|
162 | $out .= '(unlink($_) || 1)';
|
---|
163 | }
|
---|
164 | } else {
|
---|
165 | for (@cmd)
|
---|
166 | { s/'/\\'/g }
|
---|
167 | { local $" = "','"; $out .= "doexec(0, '@cmd')"; }
|
---|
168 | $declaresubs .= "sub doexec (\$\@);\n";
|
---|
169 | $init{doexec} = 1;
|
---|
170 | }
|
---|
171 | $print_needed = 0;
|
---|
172 | } elsif ($_ eq 'ok') {
|
---|
173 | my @cmd = ();
|
---|
174 | while (@ARGV && $ARGV[0] ne ';')
|
---|
175 | { push(@cmd, shift) }
|
---|
176 | shift;
|
---|
177 | $out .= tab;
|
---|
178 | for (@cmd)
|
---|
179 | { s/'/\\'/g }
|
---|
180 | { local $" = "','"; $out .= "doexec(1, '@cmd')"; }
|
---|
181 | $declaresubs .= "sub doexec (\$\@);\n";
|
---|
182 | $init{doexec} = 1;
|
---|
183 | $print_needed = 0;
|
---|
184 | } elsif ($_ eq 'prune') {
|
---|
185 | $out .= tab . '($File::Find::prune = 1)';
|
---|
186 | } elsif ($_ eq 'xdev') {
|
---|
187 | $out .= tab . '!($File::Find::prune |= ($dev != $File::Find::topdev))'
|
---|
188 | ;
|
---|
189 | } elsif ($_ eq 'newer') {
|
---|
190 | my $file = shift;
|
---|
191 | my $newername = 'AGE_OF' . $file;
|
---|
192 | $newername =~ s/\W/_/g;
|
---|
193 | $newername = '$' . $newername;
|
---|
194 | $out .= tab . "(-M _ < $newername)";
|
---|
195 | $initnewer .= "my $newername = -M " . quote($file) . ";\n";
|
---|
196 | } elsif ($_ eq 'eval') {
|
---|
197 | my $prog = shift;
|
---|
198 | $prog =~ s/'/\\'/g;
|
---|
199 | $out .= tab . "eval {$prog}";
|
---|
200 | $print_needed = 0;
|
---|
201 | } elsif ($_ eq 'depth') {
|
---|
202 | $find = 'finddepth';
|
---|
203 | next;
|
---|
204 | } elsif ($_ eq 'ls') {
|
---|
205 | $out .= tab . "ls";
|
---|
206 | $declaresubs .= "sub ls ();\n";
|
---|
207 | $init{ls} = 1;
|
---|
208 | $print_needed = 0;
|
---|
209 | } elsif ($_ eq 'tar') {
|
---|
210 | die "-tar must have a filename argument\n" unless @ARGV;
|
---|
211 | my $file = shift;
|
---|
212 | my $fh = 'FH' . $file;
|
---|
213 | $fh =~ s/\W/_/g;
|
---|
214 | $out .= tab . "tar(*$fh, \$name)";
|
---|
215 | $flushall .= "tflushall;\n";
|
---|
216 | $declaresubs .= "sub tar;\nsub tflushall ();\n";
|
---|
217 | $initfile .= "open($fh, " . quote('> ' . $file) .
|
---|
218 | qq{) || die "Can't open $fh: \$!\\n";\n};
|
---|
219 | $init{tar} = 1;
|
---|
220 | } elsif (/^(n?)cpio\z/) {
|
---|
221 | die "-$_ must have a filename argument\n" unless @ARGV;
|
---|
222 | my $file = shift;
|
---|
223 | my $fh = 'FH' . $file;
|
---|
224 | $fh =~ s/\W/_/g;
|
---|
225 | $out .= tab . "cpio(*$fh, \$name, '$1')";
|
---|
226 | $find = 'finddepth';
|
---|
227 | $flushall .= "cflushall;\n";
|
---|
228 | $declaresubs .= "sub cpio;\nsub cflushall ();\n";
|
---|
229 | $initfile .= "open($fh, " . quote('> ' . $file) .
|
---|
230 | qq{) || die "Can't open $fh: \$!\\n";\n};
|
---|
231 | $init{cpio} = 1;
|
---|
232 | } else {
|
---|
233 | die "Unrecognized switch: -$_\n";
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (@ARGV) {
|
---|
237 | if ($ARGV[0] eq '-o') {
|
---|
238 | { local($statdone) = 1; $out .= "\n" . tab . "||\n"; }
|
---|
239 | $statdone = 0 if $indent_depth == 1 && exists $init{delayedstat};
|
---|
240 | $init{saw_or} = 1;
|
---|
241 | shift;
|
---|
242 | } else {
|
---|
243 | $out .= " &&" unless $Skip_And || $ARGV[0] eq ')';
|
---|
244 | $out .= "\n";
|
---|
245 | shift if $ARGV[0] eq '-a';
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | if ($print_needed) {
|
---|
251 | my $t = tab;
|
---|
252 | if ($t !~ /&&\s*$/) { $t .= '&& ' }
|
---|
253 | $out .= "\n" . $t . 'print("$name\n")';
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | print <<"END";
|
---|
258 | $startperl
|
---|
259 | eval 'exec $perlpath -S \$0 \${1+"\$@"}'
|
---|
260 | if 0; #\$running_under_some_shell
|
---|
261 |
|
---|
262 | use strict;
|
---|
263 | use File::Find ();
|
---|
264 |
|
---|
265 | # Set the variable \$File::Find::dont_use_nlink if you're using AFS,
|
---|
266 | # since AFS cheats.
|
---|
267 |
|
---|
268 | # for the convenience of &wanted calls, including -eval statements:
|
---|
269 | use vars qw/*name *dir *prune/;
|
---|
270 | *name = *File::Find::name;
|
---|
271 | *dir = *File::Find::dir;
|
---|
272 | *prune = *File::Find::prune;
|
---|
273 |
|
---|
274 | $declaresubs
|
---|
275 |
|
---|
276 | END
|
---|
277 |
|
---|
278 | if (exists $init{doexec}) {
|
---|
279 | print <<'END';
|
---|
280 | use Cwd ();
|
---|
281 | my $cwd = Cwd::cwd();
|
---|
282 |
|
---|
283 | END
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (exists $init{ls}) {
|
---|
287 | print <<'END';
|
---|
288 | my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx);
|
---|
289 | my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
|
---|
290 |
|
---|
291 | END
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (exists $init{user} || exists $init{ls} || exists $init{tar}) {
|
---|
295 | print "my (%uid, %user);\n";
|
---|
296 | print "while (my (\$name, \$pw, \$uid) = getpwent) {\n";
|
---|
297 | print ' $uid{$name} = $uid{$uid} = $uid;', "\n"
|
---|
298 | if exists $init{user};
|
---|
299 | print ' $user{$uid} = $name unless exists $user{$uid};', "\n"
|
---|
300 | if exists $init{ls} || exists $init{tar};
|
---|
301 | print "}\n\n";
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (exists $init{group} || exists $init{ls} || exists $init{tar}) {
|
---|
305 | print "my (%gid, %group);\n";
|
---|
306 | print "while (my (\$name, \$pw, \$gid) = getgrent) {\n";
|
---|
307 | print ' $gid{$name} = $gid{$gid} = $gid;', "\n"
|
---|
308 | if exists $init{group};
|
---|
309 | print ' $group{$gid} = $name unless exists $group{$gid};', "\n"
|
---|
310 | if exists $init{ls} || exists $init{tar};
|
---|
311 | print "}\n\n";
|
---|
312 | }
|
---|
313 |
|
---|
314 | print $initnewer, "\n" if $initnewer ne '';
|
---|
315 | print $initfile, "\n" if $initfile ne '';
|
---|
316 | $flushall .= "exit;\n";
|
---|
317 | if (exists $init{declarestat}) {
|
---|
318 | $out = <<'END' . $out;
|
---|
319 | my ($dev,$ino,$mode,$nlink,$uid,$gid);
|
---|
320 |
|
---|
321 | END
|
---|
322 | }
|
---|
323 |
|
---|
324 | if ( $follow_in_effect ) {
|
---|
325 | $out =~ s/lstat\(\$_\)/lstat(_)/;
|
---|
326 | print <<"END";
|
---|
327 | $decl
|
---|
328 | # Traverse desired filesystems
|
---|
329 | File::Find::$find( {wanted => \\&wanted, follow => 1}, $roots);
|
---|
330 | $flushall
|
---|
331 |
|
---|
332 | sub wanted {
|
---|
333 | $out;
|
---|
334 | }
|
---|
335 |
|
---|
336 | END
|
---|
337 | } else {
|
---|
338 | print <<"END";
|
---|
339 | $decl
|
---|
340 | # Traverse desired filesystems
|
---|
341 | File::Find::$find({wanted => \\&wanted}, $roots);
|
---|
342 | $flushall
|
---|
343 |
|
---|
344 | sub wanted {
|
---|
345 | $out;
|
---|
346 | }
|
---|
347 |
|
---|
348 | END
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (exists $init{doexec}) {
|
---|
352 | print <<'END';
|
---|
353 |
|
---|
354 | sub doexec ($@) {
|
---|
355 | my $ok = shift;
|
---|
356 | my @command = @_; # copy so we don't try to s/// aliases to constants
|
---|
357 | for my $word (@command)
|
---|
358 | { $word =~ s#{}#$name#g }
|
---|
359 | if ($ok) {
|
---|
360 | my $old = select(STDOUT);
|
---|
361 | $| = 1;
|
---|
362 | print "@command";
|
---|
363 | select($old);
|
---|
364 | return 0 unless <STDIN> =~ /^y/;
|
---|
365 | }
|
---|
366 | chdir $cwd; #sigh
|
---|
367 | system @command;
|
---|
368 | chdir $File::Find::dir;
|
---|
369 | return !$?;
|
---|
370 | }
|
---|
371 |
|
---|
372 | END
|
---|
373 | }
|
---|
374 |
|
---|
375 | if (exists $init{ls}) {
|
---|
376 | print <<'INTRO', <<"SUB", <<'END';
|
---|
377 |
|
---|
378 | sub sizemm {
|
---|
379 | my $rdev = shift;
|
---|
380 | sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff);
|
---|
381 | }
|
---|
382 |
|
---|
383 | sub ls () {
|
---|
384 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
---|
385 | INTRO
|
---|
386 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
|
---|
387 | SUB
|
---|
388 | my $pname = $name;
|
---|
389 |
|
---|
390 | $blocks
|
---|
391 | or $blocks = int(($size + 1023) / 1024);
|
---|
392 |
|
---|
393 | my $perms = $rwx[$mode & 7];
|
---|
394 | $mode >>= 3;
|
---|
395 | $perms = $rwx[$mode & 7] . $perms;
|
---|
396 | $mode >>= 3;
|
---|
397 | $perms = $rwx[$mode & 7] . $perms;
|
---|
398 | substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _;
|
---|
399 | substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _;
|
---|
400 | substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _;
|
---|
401 | if (-f _) { $perms = '-' . $perms; }
|
---|
402 | elsif (-d _) { $perms = 'd' . $perms; }
|
---|
403 | elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($_); }
|
---|
404 | elsif (-c _) { $perms = 'c' . $perms; $size = sizemm($rdev); }
|
---|
405 | elsif (-b _) { $perms = 'b' . $perms; $size = sizemm($rdev); }
|
---|
406 | elsif (-p _) { $perms = 'p' . $perms; }
|
---|
407 | elsif (-S _) { $perms = 's' . $perms; }
|
---|
408 | else { $perms = '?' . $perms; }
|
---|
409 |
|
---|
410 | my $user = $user{$uid} || $uid;
|
---|
411 | my $group = $group{$gid} || $gid;
|
---|
412 |
|
---|
413 | my ($sec,$min,$hour,$mday,$mon,$timeyear) = localtime($mtime);
|
---|
414 | if (-M _ > 365.25 / 2) {
|
---|
415 | $timeyear += 1900;
|
---|
416 | } else {
|
---|
417 | $timeyear = sprintf("%02d:%02d", $hour, $min);
|
---|
418 | }
|
---|
419 |
|
---|
420 | printf "%5lu %4ld %-10s %3d %-8s %-8s %8s %s %2d %5s %s\n",
|
---|
421 | $ino,
|
---|
422 | $blocks,
|
---|
423 | $perms,
|
---|
424 | $nlink,
|
---|
425 | $user,
|
---|
426 | $group,
|
---|
427 | $size,
|
---|
428 | $moname[$mon],
|
---|
429 | $mday,
|
---|
430 | $timeyear,
|
---|
431 | $pname;
|
---|
432 | 1;
|
---|
433 | }
|
---|
434 |
|
---|
435 | END
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | if (exists $init{cpio} || exists $init{tar}) {
|
---|
440 | print <<'END';
|
---|
441 |
|
---|
442 | my %blocks = ();
|
---|
443 |
|
---|
444 | sub flush {
|
---|
445 | my ($fh, $varref, $blksz) = @_;
|
---|
446 |
|
---|
447 | while (length($$varref) >= $blksz) {
|
---|
448 | no strict qw/refs/;
|
---|
449 | syswrite($fh, $$varref, $blksz);
|
---|
450 | substr($$varref, 0, $blksz) = '';
|
---|
451 | ++$blocks{$fh};
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | END
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | if (exists $init{cpio}) {
|
---|
460 | print <<'INTRO', <<"SUB", <<'END';
|
---|
461 |
|
---|
462 | my %cpout = ();
|
---|
463 | my %nc = ();
|
---|
464 |
|
---|
465 | sub cpio {
|
---|
466 | my ($fh, $fname, $nc) = @_;
|
---|
467 | my $text = '';
|
---|
468 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
---|
469 | $atime,$mtime,$ctime,$blksize,$blocks);
|
---|
470 | local (*IN);
|
---|
471 |
|
---|
472 | if ( ! defined $fname ) {
|
---|
473 | $fname = 'TRAILER!!!';
|
---|
474 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
---|
475 | $atime,$mtime,$ctime,$blksize,$blocks) = (0) x 13;
|
---|
476 | } else {
|
---|
477 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
---|
478 | INTRO
|
---|
479 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
|
---|
480 | SUB
|
---|
481 | if (-f _) {
|
---|
482 | open(IN, "./$_\0") || do {
|
---|
483 | warn "Couldn't open $fname: $!\n";
|
---|
484 | return;
|
---|
485 | }
|
---|
486 | } else {
|
---|
487 | $text = readlink($_);
|
---|
488 | $size = 0 unless defined $text;
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | $fname =~ s#^\./##;
|
---|
493 | $nc{$fh} = $nc;
|
---|
494 | if ($nc eq 'n') {
|
---|
495 | $cpout{$fh} .=
|
---|
496 | sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
|
---|
497 | 070707,
|
---|
498 | $dev & 0777777,
|
---|
499 | $ino & 0777777,
|
---|
500 | $mode & 0777777,
|
---|
501 | $uid & 0777777,
|
---|
502 | $gid & 0777777,
|
---|
503 | $nlink & 0777777,
|
---|
504 | $rdev & 0177777,
|
---|
505 | $mtime,
|
---|
506 | length($fname)+1,
|
---|
507 | $size,
|
---|
508 | $fname);
|
---|
509 | } else {
|
---|
510 | $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
|
---|
511 | $cpout{$fh} .= pack("SSSSSSSSLSLa*",
|
---|
512 | 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
|
---|
513 | length($fname)+1, $size,
|
---|
514 | $fname . (length($fname) & 1 ? "\0" : "\0\0"));
|
---|
515 | }
|
---|
516 |
|
---|
517 | if ($text ne '') {
|
---|
518 | $cpout{$fh} .= $text;
|
---|
519 | } elsif ($size) {
|
---|
520 | my $l;
|
---|
521 | flush($fh, \$cpout{$fh}, 5120)
|
---|
522 | while ($l = length($cpout{$fh})) >= 5120;
|
---|
523 | while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
|
---|
524 | flush($fh, \$cpout{$fh}, 5120);
|
---|
525 | $l = length($cpout{$fh});
|
---|
526 | }
|
---|
527 | close IN;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | sub cflushall () {
|
---|
532 | for my $fh (keys %cpout) {
|
---|
533 | cpio($fh, undef, $nc{$fh});
|
---|
534 | $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
|
---|
535 | flush($fh, \$cpout{$fh}, 5120);
|
---|
536 | print $blocks{$fh} * 10, " blocks\n";
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | END
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (exists $init{tar}) {
|
---|
544 | print <<'INTRO', <<"SUB", <<'END';
|
---|
545 |
|
---|
546 | my %tarout = ();
|
---|
547 | my %linkseen = ();
|
---|
548 |
|
---|
549 | sub tar {
|
---|
550 | my ($fh, $fname) = @_;
|
---|
551 | my $prefix = '';
|
---|
552 | my $typeflag = '0';
|
---|
553 | my $linkname;
|
---|
554 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
---|
555 | INTRO
|
---|
556 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
|
---|
557 | SUB
|
---|
558 | local (*IN);
|
---|
559 |
|
---|
560 | if ($nlink > 1) {
|
---|
561 | if ($linkname = $linkseen{$fh, $dev, $ino}) {
|
---|
562 | if (length($linkname) > 100) {
|
---|
563 | warn "$0: omitting file with linkname ",
|
---|
564 | "too long for tar output: $linkname\n";
|
---|
565 | return;
|
---|
566 | }
|
---|
567 | $typeflag = '1';
|
---|
568 | $size = 0;
|
---|
569 | } else {
|
---|
570 | $linkseen{$fh, $dev, $ino} = $fname;
|
---|
571 | }
|
---|
572 | }
|
---|
573 | if ($typeflag eq '0') {
|
---|
574 | if (-f _) {
|
---|
575 | open(IN, "./$_\0") || do {
|
---|
576 | warn "Couldn't open $fname: $!\n";
|
---|
577 | return;
|
---|
578 | }
|
---|
579 | } else {
|
---|
580 | $linkname = readlink($_);
|
---|
581 | if (defined $linkname) { $typeflag = '2' }
|
---|
582 | elsif (-c _) { $typeflag = '3' }
|
---|
583 | elsif (-b _) { $typeflag = '4' }
|
---|
584 | elsif (-d _) { $typeflag = '5' }
|
---|
585 | elsif (-p _) { $typeflag = '6' }
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (length($fname) > 100) {
|
---|
590 | ($prefix, $fname) = ($fname =~ m#\A(.*?)/(.{,100})\Z(?!\n)#);
|
---|
591 | if (!defined($fname) || length($prefix) > 155) {
|
---|
592 | warn "$0: omitting file with name too long for tar output: ",
|
---|
593 | $fname, "\n";
|
---|
594 | return;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | $size = 0 if $typeflag ne '0';
|
---|
599 | my $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155",
|
---|
600 | $fname,
|
---|
601 | sprintf("%7o ", $mode & 0777),
|
---|
602 | sprintf("%7o ", $uid & 0777777),
|
---|
603 | sprintf("%7o ", $gid & 0777777),
|
---|
604 | sprintf("%11o ", $size),
|
---|
605 | sprintf("%11o ", $mtime),
|
---|
606 | ' 'x8,
|
---|
607 | $typeflag,
|
---|
608 | defined $linkname ? $linkname : '',
|
---|
609 | "ustar\0",
|
---|
610 | "00",
|
---|
611 | $user{$uid},
|
---|
612 | $group{$gid},
|
---|
613 | ($rdev >> 8) & 0xff,
|
---|
614 | $rdev & 0xff,
|
---|
615 | $prefix,
|
---|
616 | );
|
---|
617 | substr($header, 148, 8) = sprintf("%7o ", unpack("%16C*", $header));
|
---|
618 | my $l = length($header) % 512;
|
---|
619 | $tarout{$fh} .= $header;
|
---|
620 | $tarout{$fh} .= "\0" x (512 - $l) if $l;
|
---|
621 |
|
---|
622 | if ($size) {
|
---|
623 | flush($fh, \$tarout{$fh}, 10240)
|
---|
624 | while ($l = length($tarout{$fh})) >= 10240;
|
---|
625 | while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
|
---|
626 | my $slop = length($tarout{$fh}) % 512;
|
---|
627 | $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
|
---|
628 | flush($fh, \$tarout{$fh}, 10240);
|
---|
629 | $l = length($tarout{$fh});
|
---|
630 | }
|
---|
631 | close IN;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | sub tflushall () {
|
---|
636 | my $len;
|
---|
637 | for my $fh (keys %tarout) {
|
---|
638 | $len = 10240 - length($tarout{$fh});
|
---|
639 | $len += 10240 if $len < 1024;
|
---|
640 | $tarout{$fh} .= "\0" x $len;
|
---|
641 | flush($fh, \$tarout{$fh}, 10240);
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | END
|
---|
646 | }
|
---|
647 |
|
---|
648 | exit;
|
---|
649 |
|
---|
650 | ############################################################################
|
---|
651 |
|
---|
652 | sub tab () {
|
---|
653 | my $tabstring;
|
---|
654 |
|
---|
655 | $tabstring = "\t" x ($indent_depth/2) . ' ' x ($indent_depth%2 * 4);
|
---|
656 | if (!$statdone) {
|
---|
657 | if ($_ =~ /^(?:name|print|prune|exec|ok|\(|\))/) {
|
---|
658 | $init{delayedstat} = 1;
|
---|
659 | } else {
|
---|
660 | my $statcall = '(($dev,$ino,$mode,$nlink,$uid,$gid) = '
|
---|
661 | . $stat . '($_))';
|
---|
662 | if (exists $init{saw_or}) {
|
---|
663 | $tabstring .= "(\$nlink || $statcall) &&\n" . $tabstring;
|
---|
664 | } else {
|
---|
665 | $tabstring .= "$statcall &&\n" . $tabstring;
|
---|
666 | }
|
---|
667 | $statdone = 1;
|
---|
668 | $init{declarestat} = 1;
|
---|
669 | }
|
---|
670 | }
|
---|
671 | $tabstring =~ s/^\s+/ / if $out =~ /!$/;
|
---|
672 | $tabstring;
|
---|
673 | }
|
---|
674 |
|
---|
675 | sub fileglob_to_re ($) {
|
---|
676 | my $x = shift;
|
---|
677 | $x =~ s#([./^\$()+])#\\$1#g;
|
---|
678 | $x =~ s#([?*])#.$1#g;
|
---|
679 | "^$x\\z";
|
---|
680 | }
|
---|
681 |
|
---|
682 | sub n ($$) {
|
---|
683 | my ($pre, $n) = @_;
|
---|
684 | $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
|
---|
685 | $n =~ s/ 0*(\d)/ $1/;
|
---|
686 | "($pre $n)";
|
---|
687 | }
|
---|
688 |
|
---|
689 | sub quote ($) {
|
---|
690 | my $string = shift;
|
---|
691 | $string =~ s/\\/\\\\/g;
|
---|
692 | $string =~ s/'/\\'/g;
|
---|
693 | "'$string'";
|
---|
694 | }
|
---|
695 |
|
---|
696 | __END__
|
---|
697 |
|
---|
698 | =head1 NAME
|
---|
699 |
|
---|
700 | find2perl - translate find command lines to Perl code
|
---|
701 |
|
---|
702 | =head1 SYNOPSIS
|
---|
703 |
|
---|
704 | find2perl [paths] [predicates] | perl
|
---|
705 |
|
---|
706 | =head1 DESCRIPTION
|
---|
707 |
|
---|
708 | find2perl is a little translator to convert find command lines to
|
---|
709 | equivalent Perl code. The resulting code is typically faster than
|
---|
710 | running find itself.
|
---|
711 |
|
---|
712 | "paths" are a set of paths where find2perl will start its searches and
|
---|
713 | "predicates" are taken from the following list.
|
---|
714 |
|
---|
715 | =over 4
|
---|
716 |
|
---|
717 | =item C<! PREDICATE>
|
---|
718 |
|
---|
719 | Negate the sense of the following predicate. The C<!> must be passed as
|
---|
720 | a distinct argument, so it may need to be surrounded by whitespace and/or
|
---|
721 | quoted from interpretation by the shell using a backslash (just as with
|
---|
722 | using C<find(1)>).
|
---|
723 |
|
---|
724 | =item C<( PREDICATES )>
|
---|
725 |
|
---|
726 | Group the given PREDICATES. The parentheses must be passed as distinct
|
---|
727 | arguments, so they may need to be surrounded by whitespace and/or
|
---|
728 | quoted from interpretation by the shell using a backslash (just as with
|
---|
729 | using C<find(1)>).
|
---|
730 |
|
---|
731 | =item C<PREDICATE1 PREDICATE2>
|
---|
732 |
|
---|
733 | True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not
|
---|
734 | evaluated if PREDICATE1 is false.
|
---|
735 |
|
---|
736 | =item C<PREDICATE1 -o PREDICATE2>
|
---|
737 |
|
---|
738 | True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is
|
---|
739 | not evaluated if PREDICATE1 is true.
|
---|
740 |
|
---|
741 | =item C<-follow>
|
---|
742 |
|
---|
743 | Follow (dereference) symlinks. The checking of file attributes depends
|
---|
744 | on the position of the C<-follow> option. If it precedes the file
|
---|
745 | check option, an C<stat> is done which means the file check applies to the
|
---|
746 | file the symbolic link is pointing to. If C<-follow> option follows the
|
---|
747 | file check option, this now applies to the symbolic link itself, i.e.
|
---|
748 | an C<lstat> is done.
|
---|
749 |
|
---|
750 | =item C<-depth>
|
---|
751 |
|
---|
752 | Change directory traversal algorithm from breadth-first to depth-first.
|
---|
753 |
|
---|
754 | =item C<-prune>
|
---|
755 |
|
---|
756 | Do not descend into the directory currently matched.
|
---|
757 |
|
---|
758 | =item C<-xdev>
|
---|
759 |
|
---|
760 | Do not traverse mount points (prunes search at mount-point directories).
|
---|
761 |
|
---|
762 | =item C<-name GLOB>
|
---|
763 |
|
---|
764 | File name matches specified GLOB wildcard pattern. GLOB may need to be
|
---|
765 | quoted to avoid interpretation by the shell (just as with using
|
---|
766 | C<find(1)>).
|
---|
767 |
|
---|
768 | =item C<-iname GLOB>
|
---|
769 |
|
---|
770 | Like C<-name>, but the match is case insensitive.
|
---|
771 |
|
---|
772 | =item C<-path GLOB>
|
---|
773 |
|
---|
774 | Path name matches specified GLOB wildcard pattern.
|
---|
775 |
|
---|
776 | =item C<-ipath GLOB>
|
---|
777 |
|
---|
778 | Like C<-path>, but the match is case insensitive.
|
---|
779 |
|
---|
780 | =item C<-perm PERM>
|
---|
781 |
|
---|
782 | Low-order 9 bits of permission match octal value PERM.
|
---|
783 |
|
---|
784 | =item C<-perm -PERM>
|
---|
785 |
|
---|
786 | The bits specified in PERM are all set in file's permissions.
|
---|
787 |
|
---|
788 | =item C<-type X>
|
---|
789 |
|
---|
790 | The file's type matches perl's C<-X> operator.
|
---|
791 |
|
---|
792 | =item C<-fstype TYPE>
|
---|
793 |
|
---|
794 | Filesystem of current path is of type TYPE (only NFS/non-NFS distinction
|
---|
795 | is implemented).
|
---|
796 |
|
---|
797 | =item C<-user USER>
|
---|
798 |
|
---|
799 | True if USER is owner of file.
|
---|
800 |
|
---|
801 | =item C<-group GROUP>
|
---|
802 |
|
---|
803 | True if file's group is GROUP.
|
---|
804 |
|
---|
805 | =item C<-nouser>
|
---|
806 |
|
---|
807 | True if file's owner is not in password database.
|
---|
808 |
|
---|
809 | =item C<-nogroup>
|
---|
810 |
|
---|
811 | True if file's group is not in group database.
|
---|
812 |
|
---|
813 | =item C<-inum INUM>
|
---|
814 |
|
---|
815 | True file's inode number is INUM.
|
---|
816 |
|
---|
817 | =item C<-links N>
|
---|
818 |
|
---|
819 | True if (hard) link count of file matches N (see below).
|
---|
820 |
|
---|
821 | =item C<-size N>
|
---|
822 |
|
---|
823 | True if file's size matches N (see below) N is normally counted in
|
---|
824 | 512-byte blocks, but a suffix of "c" specifies that size should be
|
---|
825 | counted in characters (bytes) and a suffix of "k" specifes that
|
---|
826 | size should be counted in 1024-byte blocks.
|
---|
827 |
|
---|
828 | =item C<-atime N>
|
---|
829 |
|
---|
830 | True if last-access time of file matches N (measured in days) (see
|
---|
831 | below).
|
---|
832 |
|
---|
833 | =item C<-ctime N>
|
---|
834 |
|
---|
835 | True if last-changed time of file's inode matches N (measured in days,
|
---|
836 | see below).
|
---|
837 |
|
---|
838 | =item C<-mtime N>
|
---|
839 |
|
---|
840 | True if last-modified time of file matches N (measured in days, see below).
|
---|
841 |
|
---|
842 | =item C<-newer FILE>
|
---|
843 |
|
---|
844 | True if last-modified time of file matches N.
|
---|
845 |
|
---|
846 | =item C<-print>
|
---|
847 |
|
---|
848 | Print out path of file (always true). If none of C<-exec>, C<-ls>,
|
---|
849 | C<-print0>, or C<-ok> is specified, then C<-print> will be added
|
---|
850 | implicitly.
|
---|
851 |
|
---|
852 | =item C<-print0>
|
---|
853 |
|
---|
854 | Like -print, but terminates with \0 instead of \n.
|
---|
855 |
|
---|
856 | =item C<-exec OPTIONS ;>
|
---|
857 |
|
---|
858 | exec() the arguments in OPTIONS in a subprocess; any occurrence of {} in
|
---|
859 | OPTIONS will first be substituted with the path of the current
|
---|
860 | file. Note that the command "rm" has been special-cased to use perl's
|
---|
861 | unlink() function instead (as an optimization). The C<;> must be passed as
|
---|
862 | a distinct argument, so it may need to be surrounded by whitespace and/or
|
---|
863 | quoted from interpretation by the shell using a backslash (just as with
|
---|
864 | using C<find(1)>).
|
---|
865 |
|
---|
866 | =item C<-ok OPTIONS ;>
|
---|
867 |
|
---|
868 | Like -exec, but first prompts user; if user's response does not begin
|
---|
869 | with a y, skip the exec. The C<;> must be passed as
|
---|
870 | a distinct argument, so it may need to be surrounded by whitespace and/or
|
---|
871 | quoted from interpretation by the shell using a backslash (just as with
|
---|
872 | using C<find(1)>).
|
---|
873 |
|
---|
874 | =item C<-eval EXPR>
|
---|
875 |
|
---|
876 | Has the perl script eval() the EXPR.
|
---|
877 |
|
---|
878 | =item C<-ls>
|
---|
879 |
|
---|
880 | Simulates C<-exec ls -dils {} ;>
|
---|
881 |
|
---|
882 | =item C<-tar FILE>
|
---|
883 |
|
---|
884 | Adds current output to tar-format FILE.
|
---|
885 |
|
---|
886 | =item C<-cpio FILE>
|
---|
887 |
|
---|
888 | Adds current output to old-style cpio-format FILE.
|
---|
889 |
|
---|
890 | =item C<-ncpio FILE>
|
---|
891 |
|
---|
892 | Adds current output to "new"-style cpio-format FILE.
|
---|
893 |
|
---|
894 | =back
|
---|
895 |
|
---|
896 | Predicates which take a numeric argument N can come in three forms:
|
---|
897 |
|
---|
898 | * N is prefixed with a +: match values greater than N
|
---|
899 | * N is prefixed with a -: match values less than N
|
---|
900 | * N is not prefixed with either + or -: match only values equal to N
|
---|
901 |
|
---|
902 | =head1 SEE ALSO
|
---|
903 |
|
---|
904 | find
|
---|
905 |
|
---|
906 | =cut
|
---|
907 |
|
---|
908 | __END__
|
---|
909 | :endofperl
|
---|