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