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