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