Commit | Line | Data |
---|---|---|
4633a7c4 LW |
1 | #!/usr/local/bin/perl |
2 | ||
3 | use Config; | |
4 | use File::Basename qw(&basename &dirname); | |
8a5546a1 | 5 | use 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 | 17 | chdir dirname($0); |
18 | $file = basename($0, '.PL'); | |
774d564b | 19 | $file .= '.com' if $^O eq 'VMS'; |
4633a7c4 LW |
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 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 | ||
39 | print OUT <<'!NO!SUBS!'; | |
431613dd GS |
40 | use strict; |
41 | use vars qw/$statdone/; | |
2305df61 | 42 | use File::Spec::Functions 'curdir'; |
431613dd | 43 | my $startperl = "#! $perlpath -w"; |
7b8d334a | 44 | |
572330a4 PN |
45 | sub tab (); |
46 | sub n ($$); | |
47 | sub fileglob_to_re ($); | |
48 | sub quote ($); | |
49 | ||
431613dd | 50 | my @roots = (); |
fe14fcc3 LW |
51 | while ($ARGV[0] =~ /^[^-!(]/) { |
52 | push(@roots, shift); | |
53 | } | |
2305df61 | 54 | @roots = (curdir()) unless @roots; |
572330a4 | 55 | for (@roots) { $_ = quote($_) } |
431613dd GS |
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 LW |
70 | |
71 | while (@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 | 256 | if ($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 | 263 | print <<"END"; |
4633a7c4 | 264 | $startperl |
5f05dabc | 265 | eval 'exec $perlpath -S \$0 \${1+"\$@"}' |
431613dd GS |
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 JH |
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 GS |
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 LW |
296 | |
297 | END | |
298 | } | |
299 | ||
431613dd GS |
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 LW |
307 | print "}\n\n"; |
308 | } | |
309 | ||
431613dd GS |
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 LW |
317 | print "}\n\n"; |
318 | } | |
319 | ||
431613dd GS |
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 GS |
327 | END |
328 | } | |
fe14fcc3 | 329 | |
3d1e7443 GS |
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 LW |
338 | sub wanted { |
339 | $out; | |
340 | } | |
341 | ||
342 | END | |
3d1e7443 GS |
343 | } else { |
344 | print <<"END"; | |
345 | $decl | |
346 | # Traverse desired filesystems | |
347 | File::Find::$find({wanted => \\&wanted}, $roots); | |
348 | $flushall | |
fe14fcc3 | 349 | |
3d1e7443 GS |
350 | sub wanted { |
351 | $out; | |
352 | } | |
353 | ||
354 | END | |
355 | } | |
431613dd GS |
356 | |
357 | if (exists $init{doexec}) { | |
fe14fcc3 | 358 | print <<'END'; |
431613dd | 359 | |
572330a4 | 360 | sub 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 | ||
378 | END | |
379 | } | |
380 | ||
431613dd GS |
381 | if (exists $init{ls}) { |
382 | print <<'INTRO', <<"SUB", <<'END'; | |
fe14fcc3 | 383 | |
431613dd GS |
384 | sub sizemm { |
385 | my $rdev = shift; | |
386 | sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff); | |
387 | } | |
fe14fcc3 | 388 | |
572330a4 | 389 | sub ls () { |
431613dd GS |
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 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 |
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 LW |
459 | } |
460 | ||
461 | END | |
462 | } | |
463 | ||
fe14fcc3 | 464 | |
431613dd GS |
465 | if (exists $init{cpio}) { |
466 | print <<'INTRO', <<"SUB", <<'END'; | |
467 | ||
468 | my %cpout = (); | |
469 | my %nc = (); | |
fe14fcc3 | 470 | |
431613dd GS |
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 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 | 537 | sub 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 | ||
546 | END | |
547 | } | |
548 | ||
431613dd GS |
549 | if (exists $init{tar}) { |
550 | print <<'INTRO', <<"SUB", <<'END'; | |
551 | ||
552 | my %tarout = (); | |
553 | my %linkseen = (); | |
554 | ||
fe14fcc3 | 555 | sub 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, | |
561 | INTRO | |
562 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); | |
563 | SUB | |
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 | 641 | sub 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 | ||
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 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 | 681 | sub fileglob_to_re ($) { |
431613dd | 682 | my $x = shift; |
50097298 | 683 | $x =~ s#([./^\$()+])#\\$1#g; |
bea97888 TC |
684 | $x =~ s#\*#.*#g; |
685 | $x =~ s#\?#.#g; | |
c7b9dd21 | 686 | "^$x\\z"; |
fe14fcc3 LW |
687 | } |
688 | ||
572330a4 | 689 | sub n ($$) { |
431613dd | 690 | my ($pre, $n) = @_; |
1c3d792e LW |
691 | $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /; |
692 | $n =~ s/ 0*(\d)/ $1/; | |
431613dd | 693 | "($pre $n)"; |
fe14fcc3 LW |
694 | } |
695 | ||
572330a4 | 696 | sub quote ($) { |
431613dd | 697 | my $string = shift; |
2305df61 | 698 | $string =~ s/\\/\\\\/g; |
431613dd | 699 | $string =~ s/'/\\'/g; |
fe14fcc3 LW |
700 | "'$string'"; |
701 | } | |
431613dd GS |
702 | |
703 | __END__ | |
704 | ||
705 | =head1 NAME | |
706 | ||
707 | find2perl - translate find command lines to Perl code | |
708 | ||
709 | =head1 SYNOPSIS | |
710 | ||
711 | find2perl [paths] [predicates] | perl | |
712 | ||
713 | =head1 DESCRIPTION | |
714 | ||
715 | find2perl is a little translator to convert find command lines to | |
716 | equivalent Perl code. The resulting code is typically faster than | |
717 | running find itself. | |
718 | ||
719 | "paths" are a set of paths where find2perl will start its searches and | |
720 | "predicates" are taken from the following list. | |
721 | ||
722 | =over 4 | |
723 | ||
724 | =item C<! PREDICATE> | |
725 | ||
726 | Negate the sense of the following predicate. The C<!> must be passed as | |
727 | a distinct argument, so it may need to be surrounded by whitespace and/or | |
728 | quoted from interpretation by the shell using a backslash (just as with | |
729 | using C<find(1)>). | |
730 | ||
731 | =item C<( PREDICATES )> | |
732 | ||
733 | Group the given PREDICATES. The parentheses must be passed as distinct | |
734 | arguments, so they may need to be surrounded by whitespace and/or | |
735 | quoted from interpretation by the shell using a backslash (just as with | |
736 | using C<find(1)>). | |
737 | ||
738 | =item C<PREDICATE1 PREDICATE2> | |
739 | ||
740 | True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not | |
741 | evaluated if PREDICATE1 is false. | |
742 | ||
743 | =item C<PREDICATE1 -o PREDICATE2> | |
744 | ||
745 | True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is | |
746 | not evaluated if PREDICATE1 is true. | |
747 | ||
748 | =item C<-follow> | |
749 | ||
3d1e7443 GS |
750 | Follow (dereference) symlinks. The checking of file attributes depends |
751 | on the position of the C<-follow> option. If it precedes the file | |
752 | check option, an C<stat> is done which means the file check applies to the | |
753 | file the symbolic link is pointing to. If C<-follow> option follows the | |
754 | file check option, this now applies to the symbolic link itself, i.e. | |
755 | an C<lstat> is done. | |
431613dd GS |
756 | |
757 | =item C<-depth> | |
758 | ||
759 | Change directory traversal algorithm from breadth-first to depth-first. | |
760 | ||
761 | =item C<-prune> | |
762 | ||
763 | Do not descend into the directory currently matched. | |
764 | ||
765 | =item C<-xdev> | |
766 | ||
767 | Do not traverse mount points (prunes search at mount-point directories). | |
768 | ||
769 | =item C<-name GLOB> | |
770 | ||
771 | File name matches specified GLOB wildcard pattern. GLOB may need to be | |
772 | quoted to avoid interpretation by the shell (just as with using | |
773 | C<find(1)>). | |
774 | ||
6f028b01 SR |
775 | =item C<-iname GLOB> |
776 | ||
777 | Like C<-name>, but the match is case insensitive. | |
778 | ||
779 | =item C<-path GLOB> | |
780 | ||
781 | Path name matches specified GLOB wildcard pattern. | |
782 | ||
783 | =item C<-ipath GLOB> | |
784 | ||
785 | Like C<-path>, but the match is case insensitive. | |
786 | ||
431613dd GS |
787 | =item C<-perm PERM> |
788 | ||
789 | Low-order 9 bits of permission match octal value PERM. | |
790 | ||
791 | =item C<-perm -PERM> | |
792 | ||
793 | The bits specified in PERM are all set in file's permissions. | |
794 | ||
795 | =item C<-type X> | |
796 | ||
797 | The file's type matches perl's C<-X> operator. | |
798 | ||
799 | =item C<-fstype TYPE> | |
800 | ||
801 | Filesystem of current path is of type TYPE (only NFS/non-NFS distinction | |
802 | is implemented). | |
803 | ||
804 | =item C<-user USER> | |
805 | ||
806 | True if USER is owner of file. | |
807 | ||
808 | =item C<-group GROUP> | |
809 | ||
810 | True if file's group is GROUP. | |
811 | ||
812 | =item C<-nouser> | |
813 | ||
814 | True if file's owner is not in password database. | |
815 | ||
816 | =item C<-nogroup> | |
817 | ||
818 | True if file's group is not in group database. | |
819 | ||
820 | =item C<-inum INUM> | |
821 | ||
822 | True file's inode number is INUM. | |
823 | ||
824 | =item C<-links N> | |
825 | ||
826 | True if (hard) link count of file matches N (see below). | |
827 | ||
828 | =item C<-size N> | |
829 | ||
830 | True if file's size matches N (see below) N is normally counted in | |
831 | 512-byte blocks, but a suffix of "c" specifies that size should be | |
863b2ca0 | 832 | counted in characters (bytes) and a suffix of "k" specifies that |
431613dd GS |
833 | size should be counted in 1024-byte blocks. |
834 | ||
835 | =item C<-atime N> | |
836 | ||
837 | True if last-access time of file matches N (measured in days) (see | |
838 | below). | |
839 | ||
840 | =item C<-ctime N> | |
841 | ||
842 | True if last-changed time of file's inode matches N (measured in days, | |
843 | see below). | |
844 | ||
845 | =item C<-mtime N> | |
846 | ||
847 | True if last-modified time of file matches N (measured in days, see below). | |
848 | ||
849 | =item C<-newer FILE> | |
850 | ||
851 | True if last-modified time of file matches N. | |
852 | ||
853 | =item C<-print> | |
854 | ||
9c4673c1 SR |
855 | Print out path of file (always true). If none of C<-exec>, C<-ls>, |
856 | C<-print0>, or C<-ok> is specified, then C<-print> will be added | |
857 | implicitly. | |
431613dd GS |
858 | |
859 | =item C<-print0> | |
860 | ||
861 | Like -print, but terminates with \0 instead of \n. | |
862 | ||
863 | =item C<-exec OPTIONS ;> | |
864 | ||
fb8eeed8 | 865 | exec() the arguments in OPTIONS in a subprocess; any occurrence of {} in |
431613dd GS |
866 | OPTIONS will first be substituted with the path of the current |
867 | file. Note that the command "rm" has been special-cased to use perl's | |
868 | unlink() function instead (as an optimization). The C<;> must be passed as | |
869 | a distinct argument, so it may need to be surrounded by whitespace and/or | |
870 | quoted from interpretation by the shell using a backslash (just as with | |
871 | using C<find(1)>). | |
872 | ||
873 | =item C<-ok OPTIONS ;> | |
874 | ||
875 | Like -exec, but first prompts user; if user's response does not begin | |
876 | with a y, skip the exec. The C<;> must be passed as | |
877 | a distinct argument, so it may need to be surrounded by whitespace and/or | |
878 | quoted from interpretation by the shell using a backslash (just as with | |
879 | using C<find(1)>). | |
880 | ||
928e06a6 | 881 | =item C<-eval EXPR> |
431613dd | 882 | |
928e06a6 | 883 | Has the perl script eval() the EXPR. |
431613dd GS |
884 | |
885 | =item C<-ls> | |
886 | ||
887 | Simulates C<-exec ls -dils {} ;> | |
888 | ||
889 | =item C<-tar FILE> | |
890 | ||
891 | Adds current output to tar-format FILE. | |
892 | ||
893 | =item C<-cpio FILE> | |
894 | ||
895 | Adds current output to old-style cpio-format FILE. | |
896 | ||
897 | =item C<-ncpio FILE> | |
898 | ||
899 | Adds current output to "new"-style cpio-format FILE. | |
900 | ||
901 | =back | |
902 | ||
903 | Predicates which take a numeric argument N can come in three forms: | |
904 | ||
905 | * N is prefixed with a +: match values greater than N | |
906 | * N is prefixed with a -: match values less than N | |
907 | * N is not prefixed with either + or -: match only values equal to N | |
908 | ||
431613dd GS |
909 | =head1 SEE ALSO |
910 | ||
9b33fb8e | 911 | find, File::Find. |
431613dd GS |
912 | |
913 | =cut | |
fe14fcc3 | 914 | !NO!SUBS! |
4633a7c4 LW |
915 | |
916 | close OUT or die "Can't close $file: $!"; | |
917 | chmod 0755, $file or die "Can't reset permissions for $file: $!\n"; | |
918 | exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':'; | |
8a5546a1 | 919 | chdir $origdir; |