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; |
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 | |
39 | print OUT <<'!NO!SUBS!'; |
431613dd |
40 | use strict; |
41 | use vars qw/$statdone/; |
2305df61 |
42 | use File::Spec::Functions 'curdir'; |
431613dd |
43 | my $startperl = "#! $perlpath -w"; |
7b8d334a |
44 | |
572330a4 |
45 | sub tab (); |
46 | sub n ($$); |
47 | sub fileglob_to_re ($); |
48 | sub quote ($); |
49 | |
431613dd |
50 | my @roots = (); |
fe14fcc3 |
51 | while ($ARGV[0] =~ /^[^-!(]/) { |
52 | push(@roots, shift); |
53 | } |
2305df61 |
54 | @roots = (curdir()) unless @roots; |
572330a4 |
55 | for (@roots) { $_ = quote($_) } |
431613dd |
56 | my $roots = join(', ', @roots); |
57 | |
58 | my $find = "find"; |
59 | my $indent_depth = 1; |
60 | my $stat = 'lstat'; |
61 | my $decl = ''; |
62 | my $flushall = ''; |
63 | my $initfile = ''; |
64 | my $initnewer = ''; |
65 | my $out = ''; |
572330a4 |
66 | my $declaresubs = "sub wanted;\n"; |
431613dd |
67 | my %init = (); |
3d1e7443 |
68 | my ($follow_in_effect,$Skip_And) = (0,0); |
9c4673c1 |
69 | my $print_needed = 1; |
fe14fcc3 |
70 | |
71 | while (@ARGV) { |
72 | $_ = shift; |
73 | s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n"; |
74 | if ($_ eq '(') { |
572330a4 |
75 | $out .= tab . "(\n"; |
431613dd |
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 |
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 |
92 | } elsif ($_ eq 'perm') { |
93 | my $onum = shift; |
94 | $onum =~ /^-?[0-7]+$/ |
95 | || die "Malformed -perm argument: $onum\n"; |
572330a4 |
96 | $out .= tab; |
431613dd |
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 |
113 | } elsif ($_ eq 'fstype') { |
114 | my $type = shift; |
572330a4 |
115 | $out .= tab; |
431613dd |
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 |
124 | $init{user} = 1; |
125 | } elsif ($_ eq 'group') { |
126 | my $gname = shift; |
572330a4 |
127 | $out .= tab . "(\$gid == \$gid{'$gname'})"; |
431613dd |
128 | $init{group} = 1; |
129 | } elsif ($_ eq 'nouser') { |
572330a4 |
130 | $out .= tab . '!exists $uid{$uid}'; |
431613dd |
131 | $init{user} = 1; |
132 | } elsif ($_ eq 'nogroup') { |
572330a4 |
133 | $out .= tab . '!exists $gid{$gid}'; |
431613dd |
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 |
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 |
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 |
154 | } elsif ($_ eq 'exec') { |
155 | my @cmd = (); |
156 | while (@ARGV && $ARGV[0] ne ';') |
157 | { push(@cmd, shift) } |
158 | shift; |
572330a4 |
159 | $out .= tab; |
431613dd |
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 |
173 | { local $" = "','"; $out .= "doexec(0, '@cmd')"; } |
174 | $declaresubs .= "sub doexec (\$\@);\n"; |
431613dd |
175 | $init{doexec} = 1; |
176 | } |
9c4673c1 |
177 | $print_needed = 0; |
431613dd |
178 | } elsif ($_ eq 'ok') { |
179 | my @cmd = (); |
180 | while (@ARGV && $ARGV[0] ne ';') |
181 | { push(@cmd, shift) } |
182 | shift; |
572330a4 |
183 | $out .= tab; |
431613dd |
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 |
194 | ; |
195 | } elsif ($_ eq 'newer') { |
196 | my $file = shift; |
197 | my $newername = 'AGE_OF' . $file; |
198 | $newername =~ s/\W/_/g; |
199 | $newername = '$' . $newername; |
572330a4 |
200 | $out .= tab . "(-M _ < $newername)"; |
201 | $initnewer .= "my $newername = -M " . quote($file) . ";\n"; |
431613dd |
202 | } elsif ($_ eq 'eval') { |
203 | my $prog = shift; |
204 | $prog =~ s/'/\\'/g; |
572330a4 |
205 | $out .= tab . "eval {$prog}"; |
a9a4ee8b |
206 | $print_needed = 0; |
431613dd |
207 | } elsif ($_ eq 'depth') { |
208 | $find = 'finddepth'; |
209 | next; |
210 | } elsif ($_ eq 'ls') { |
572330a4 |
211 | $out .= tab . "ls"; |
212 | $declaresubs .= "sub ls ();\n"; |
431613dd |
213 | $init{ls} = 1; |
9c4673c1 |
214 | $print_needed = 0; |
431613dd |
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 |
220 | $out .= tab . "tar(*$fh, \$name)"; |
221 | $flushall .= "tflushall;\n"; |
222 | $declaresubs .= "sub tar;\nsub tflushall ();\n"; |
223 | $initfile .= "open($fh, " . quote('> ' . $file) . |
431613dd |
224 | qq{) || die "Can't open $fh: \$!\\n";\n}; |
225 | $init{tar} = 1; |
c7b9dd21 |
226 | } elsif (/^(n?)cpio\z/) { |
431613dd |
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 |
233 | $flushall .= "cflushall;\n"; |
234 | $declaresubs .= "sub cpio;\nsub cflushall ();\n"; |
235 | $initfile .= "open($fh, " . quote('> ' . $file) . |
431613dd |
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 |
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 |
250 | $out .= "\n"; |
251 | shift if $ARGV[0] eq '-a'; |
252 | } |
fe14fcc3 |
253 | } |
254 | } |
255 | |
9c4673c1 |
256 | if ($print_needed) { |
178eb895 |
257 | my $t = tab; |
258 | if ($t !~ /&&\s*$/) { $t .= '&& ' } |
259 | $out .= "\n" . $t . 'print("$name\n")'; |
9c4673c1 |
260 | } |
261 | |
431613dd |
262 | |
fe14fcc3 |
263 | print <<"END"; |
4633a7c4 |
264 | $startperl |
5f05dabc |
265 | eval 'exec $perlpath -S \$0 \${1+"\$@"}' |
431613dd |
266 | if 0; #\$running_under_some_shell |
267 | |
268 | use strict; |
269 | use 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: |
275 | use 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 |
282 | END |
431613dd |
283 | |
7cc8f688 |
284 | if (exists $init{doexec}) { |
285 | print <<'END'; |
286 | use Cwd (); |
287 | my $cwd = Cwd::cwd(); |
288 | |
289 | END |
290 | } |
291 | |
431613dd |
292 | if (exists $init{ls}) { |
fe14fcc3 |
293 | print <<'END'; |
431613dd |
294 | my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx); |
295 | my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
fe14fcc3 |
296 | |
297 | END |
298 | } |
299 | |
431613dd |
300 | if (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 |
307 | print "}\n\n"; |
308 | } |
309 | |
431613dd |
310 | if (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 |
317 | print "}\n\n"; |
318 | } |
319 | |
431613dd |
320 | print $initnewer, "\n" if $initnewer ne ''; |
321 | print $initfile, "\n" if $initfile ne ''; |
322 | $flushall .= "exit;\n"; |
323 | if (exists $init{declarestat}) { |
324 | $out = <<'END' . $out; |
325 | my ($dev,$ino,$mode,$nlink,$uid,$gid); |
fe14fcc3 |
326 | |
431613dd |
327 | END |
328 | } |
fe14fcc3 |
329 | |
3d1e7443 |
330 | if ( $follow_in_effect ) { |
331 | $out =~ s/lstat\(\$_\)/lstat(_)/; |
fe14fcc3 |
332 | print <<"END"; |
7b8d334a |
333 | $decl |
431613dd |
334 | # Traverse desired filesystems |
3d1e7443 |
335 | File::Find::$find( {wanted => \\&wanted, follow => 1}, $roots); |
fe14fcc3 |
336 | $flushall |
431613dd |
337 | |
fe14fcc3 |
338 | sub wanted { |
339 | $out; |
340 | } |
341 | |
342 | END |
3d1e7443 |
343 | } else { |
344 | print <<"END"; |
345 | $decl |
346 | # Traverse desired filesystems |
347 | File::Find::$find({wanted => \\&wanted}, $roots); |
348 | $flushall |
fe14fcc3 |
349 | |
3d1e7443 |
350 | sub wanted { |
351 | $out; |
352 | } |
353 | |
354 | END |
355 | } |
431613dd |
356 | |
357 | if (exists $init{doexec}) { |
fe14fcc3 |
358 | print <<'END'; |
431613dd |
359 | |
572330a4 |
360 | sub doexec ($@) { |
431613dd |
361 | my $ok = shift; |
572330a4 |
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 |
366 | my $old = select(STDOUT); |
367 | $| = 1; |
572330a4 |
368 | print "@command"; |
431613dd |
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 |
375 | return !$?; |
376 | } |
377 | |
378 | END |
379 | } |
380 | |
431613dd |
381 | if (exists $init{ls}) { |
382 | print <<'INTRO', <<"SUB", <<'END'; |
fe14fcc3 |
383 | |
431613dd |
384 | sub sizemm { |
385 | my $rdev = shift; |
386 | sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff); |
387 | } |
fe14fcc3 |
388 | |
572330a4 |
389 | sub ls () { |
431613dd |
390 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
391 | INTRO |
392 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
393 | SUB |
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 |
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 |
438 | 1; |
439 | } |
440 | |
431613dd |
441 | END |
442 | } |
443 | |
444 | |
445 | if (exists $init{cpio} || exists $init{tar}) { |
446 | print <<'END'; |
447 | |
448 | my %blocks = (); |
449 | |
450 | sub 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 |
459 | } |
460 | |
461 | END |
462 | } |
463 | |
fe14fcc3 |
464 | |
431613dd |
465 | if (exists $init{cpio}) { |
466 | print <<'INTRO', <<"SUB", <<'END'; |
467 | |
468 | my %cpout = (); |
469 | my %nc = (); |
fe14fcc3 |
470 | |
431613dd |
471 | sub 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, |
484 | INTRO |
485 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
486 | SUB |
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 |
499 | $nc{$fh} = $nc; |
500 | if ($nc eq 'n') { |
431613dd |
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 |
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 |
534 | } |
535 | } |
536 | |
572330a4 |
537 | sub cflushall () { |
431613dd |
538 | for my $fh (keys %cpout) { |
572330a4 |
539 | cpio($fh, undef, $nc{$fh}); |
431613dd |
540 | $cpout{$fh} .= "0" x (5120 - length($cpout{$fh})); |
541 | flush($fh, \$cpout{$fh}, 5120); |
542 | print $blocks{$fh} * 10, " blocks\n"; |
fe14fcc3 |
543 | } |
544 | } |
545 | |
546 | END |
547 | } |
548 | |
431613dd |
549 | if (exists $init{tar}) { |
550 | print <<'INTRO', <<"SUB", <<'END'; |
551 | |
552 | my %tarout = (); |
553 | my %linkseen = (); |
554 | |
fe14fcc3 |
555 | sub tar { |
431613dd |
556 | my ($fh, $fname) = @_; |
557 | my $prefix = ''; |
558 | my $typeflag = '0'; |
559 | my $linkname; |
560 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
561 | INTRO |
562 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
563 | SUB |
564 | local (*IN); |
fe14fcc3 |
565 | |
431613dd |
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 |
625 | $tarout{$fh} .= $header; |
626 | $tarout{$fh} .= "\0" x (512 - $l) if $l; |
fe14fcc3 |
627 | |
431613dd |
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 |
638 | } |
639 | } |
640 | |
572330a4 |
641 | sub tflushall () { |
431613dd |
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 |
648 | } |
649 | } |
650 | |
651 | END |
652 | } |
653 | |
654 | exit; |
655 | |
656 | ############################################################################ |
657 | |
572330a4 |
658 | sub 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 |
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 |
676 | } |
677 | $tabstring =~ s/^\s+/ / if $out =~ /!$/; |
678 | $tabstring; |
679 | } |
680 | |
572330a4 |
681 | sub 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 |
686 | } |
687 | |
572330a4 |
688 | sub n ($$) { |
431613dd |
689 | my ($pre, $n) = @_; |
1c3d792e |
690 | $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /; |
691 | $n =~ s/ 0*(\d)/ $1/; |
431613dd |
692 | "($pre $n)"; |
fe14fcc3 |
693 | } |
694 | |
572330a4 |
695 | sub quote ($) { |
431613dd |
696 | my $string = shift; |
2305df61 |
697 | $string =~ s/\\/\\\\/g; |
431613dd |
698 | $string =~ s/'/\\'/g; |
fe14fcc3 |
699 | "'$string'"; |
700 | } |
431613dd |
701 | |
702 | __END__ |
703 | |
704 | =head1 NAME |
705 | |
706 | find2perl - translate find command lines to Perl code |
707 | |
708 | =head1 SYNOPSIS |
709 | |
710 | find2perl [paths] [predicates] | perl |
711 | |
712 | =head1 DESCRIPTION |
713 | |
714 | find2perl is a little translator to convert find command lines to |
715 | equivalent Perl code. The resulting code is typically faster than |
716 | running 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 | |
725 | Negate the sense of the following predicate. The C<!> must be passed as |
726 | a distinct argument, so it may need to be surrounded by whitespace and/or |
727 | quoted from interpretation by the shell using a backslash (just as with |
728 | using C<find(1)>). |
729 | |
730 | =item C<( PREDICATES )> |
731 | |
732 | Group the given PREDICATES. The parentheses must be passed as distinct |
733 | arguments, so they may need to be surrounded by whitespace and/or |
734 | quoted from interpretation by the shell using a backslash (just as with |
735 | using C<find(1)>). |
736 | |
737 | =item C<PREDICATE1 PREDICATE2> |
738 | |
739 | True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not |
740 | evaluated if PREDICATE1 is false. |
741 | |
742 | =item C<PREDICATE1 -o PREDICATE2> |
743 | |
744 | True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is |
745 | not evaluated if PREDICATE1 is true. |
746 | |
747 | =item C<-follow> |
748 | |
3d1e7443 |
749 | Follow (dereference) symlinks. The checking of file attributes depends |
750 | on the position of the C<-follow> option. If it precedes the file |
751 | check option, an C<stat> is done which means the file check applies to the |
752 | file the symbolic link is pointing to. If C<-follow> option follows the |
753 | file check option, this now applies to the symbolic link itself, i.e. |
754 | an C<lstat> is done. |
431613dd |
755 | |
756 | =item C<-depth> |
757 | |
758 | Change directory traversal algorithm from breadth-first to depth-first. |
759 | |
760 | =item C<-prune> |
761 | |
762 | Do not descend into the directory currently matched. |
763 | |
764 | =item C<-xdev> |
765 | |
766 | Do not traverse mount points (prunes search at mount-point directories). |
767 | |
768 | =item C<-name GLOB> |
769 | |
770 | File name matches specified GLOB wildcard pattern. GLOB may need to be |
771 | quoted to avoid interpretation by the shell (just as with using |
772 | C<find(1)>). |
773 | |
6f028b01 |
774 | =item C<-iname GLOB> |
775 | |
776 | Like C<-name>, but the match is case insensitive. |
777 | |
778 | =item C<-path GLOB> |
779 | |
780 | Path name matches specified GLOB wildcard pattern. |
781 | |
782 | =item C<-ipath GLOB> |
783 | |
784 | Like C<-path>, but the match is case insensitive. |
785 | |
431613dd |
786 | =item C<-perm PERM> |
787 | |
788 | Low-order 9 bits of permission match octal value PERM. |
789 | |
790 | =item C<-perm -PERM> |
791 | |
792 | The bits specified in PERM are all set in file's permissions. |
793 | |
794 | =item C<-type X> |
795 | |
796 | The file's type matches perl's C<-X> operator. |
797 | |
798 | =item C<-fstype TYPE> |
799 | |
800 | Filesystem of current path is of type TYPE (only NFS/non-NFS distinction |
801 | is implemented). |
802 | |
803 | =item C<-user USER> |
804 | |
805 | True if USER is owner of file. |
806 | |
807 | =item C<-group GROUP> |
808 | |
809 | True if file's group is GROUP. |
810 | |
811 | =item C<-nouser> |
812 | |
813 | True if file's owner is not in password database. |
814 | |
815 | =item C<-nogroup> |
816 | |
817 | True if file's group is not in group database. |
818 | |
819 | =item C<-inum INUM> |
820 | |
821 | True file's inode number is INUM. |
822 | |
823 | =item C<-links N> |
824 | |
825 | True if (hard) link count of file matches N (see below). |
826 | |
827 | =item C<-size N> |
828 | |
829 | True if file's size matches N (see below) N is normally counted in |
830 | 512-byte blocks, but a suffix of "c" specifies that size should be |
863b2ca0 |
831 | counted in characters (bytes) and a suffix of "k" specifies that |
431613dd |
832 | size should be counted in 1024-byte blocks. |
833 | |
834 | =item C<-atime N> |
835 | |
836 | True if last-access time of file matches N (measured in days) (see |
837 | below). |
838 | |
839 | =item C<-ctime N> |
840 | |
841 | True if last-changed time of file's inode matches N (measured in days, |
842 | see below). |
843 | |
844 | =item C<-mtime N> |
845 | |
846 | True if last-modified time of file matches N (measured in days, see below). |
847 | |
848 | =item C<-newer FILE> |
849 | |
850 | True if last-modified time of file matches N. |
851 | |
852 | =item C<-print> |
853 | |
9c4673c1 |
854 | Print out path of file (always true). If none of C<-exec>, C<-ls>, |
855 | C<-print0>, or C<-ok> is specified, then C<-print> will be added |
856 | implicitly. |
431613dd |
857 | |
858 | =item C<-print0> |
859 | |
860 | Like -print, but terminates with \0 instead of \n. |
861 | |
862 | =item C<-exec OPTIONS ;> |
863 | |
fb8eeed8 |
864 | exec() the arguments in OPTIONS in a subprocess; any occurrence of {} in |
431613dd |
865 | OPTIONS will first be substituted with the path of the current |
866 | file. Note that the command "rm" has been special-cased to use perl's |
867 | unlink() function instead (as an optimization). The C<;> must be passed as |
868 | a distinct argument, so it may need to be surrounded by whitespace and/or |
869 | quoted from interpretation by the shell using a backslash (just as with |
870 | using C<find(1)>). |
871 | |
872 | =item C<-ok OPTIONS ;> |
873 | |
874 | Like -exec, but first prompts user; if user's response does not begin |
875 | with a y, skip the exec. The C<;> must be passed as |
876 | a distinct argument, so it may need to be surrounded by whitespace and/or |
877 | quoted from interpretation by the shell using a backslash (just as with |
878 | using C<find(1)>). |
879 | |
928e06a6 |
880 | =item C<-eval EXPR> |
431613dd |
881 | |
928e06a6 |
882 | Has the perl script eval() the EXPR. |
431613dd |
883 | |
884 | =item C<-ls> |
885 | |
886 | Simulates C<-exec ls -dils {} ;> |
887 | |
888 | =item C<-tar FILE> |
889 | |
890 | Adds current output to tar-format FILE. |
891 | |
892 | =item C<-cpio FILE> |
893 | |
894 | Adds current output to old-style cpio-format FILE. |
895 | |
896 | =item C<-ncpio FILE> |
897 | |
898 | Adds current output to "new"-style cpio-format FILE. |
899 | |
900 | =back |
901 | |
902 | Predicates 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 |
908 | =head1 SEE ALSO |
909 | |
9b33fb8e |
910 | find, File::Find. |
431613dd |
911 | |
912 | =cut |
fe14fcc3 |
913 | !NO!SUBS! |
4633a7c4 |
914 | |
915 | close OUT or die "Can't close $file: $!"; |
916 | chmod 0755, $file or die "Can't reset permissions for $file: $!\n"; |
917 | exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':'; |
8a5546a1 |
918 | chdir $origdir; |