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 | |
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 | |
572330a4 |
59 | sub tab (); |
60 | sub n ($$); |
61 | sub fileglob_to_re ($); |
62 | sub quote ($); |
63 | |
431613dd |
64 | my @roots = (); |
fe14fcc3 |
65 | while ($ARGV[0] =~ /^[^-!(]/) { |
66 | push(@roots, shift); |
67 | } |
2305df61 |
68 | @roots = (curdir()) unless @roots; |
572330a4 |
69 | for (@roots) { $_ = quote($_) } |
431613dd |
70 | my $roots = join(', ', @roots); |
71 | |
72 | my $find = "find"; |
73 | my $indent_depth = 1; |
74 | my $stat = 'lstat'; |
75 | my $decl = ''; |
76 | my $flushall = ''; |
77 | my $initfile = ''; |
78 | my $initnewer = ''; |
79 | my $out = ''; |
572330a4 |
80 | my $declaresubs = "sub wanted;\n"; |
431613dd |
81 | my %init = (); |
3d1e7443 |
82 | my ($follow_in_effect,$Skip_And) = (0,0); |
9c4673c1 |
83 | my $print_needed = 1; |
fe14fcc3 |
84 | |
85 | while (@ARGV) { |
86 | $_ = shift; |
87 | s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n"; |
88 | if ($_ eq '(') { |
572330a4 |
89 | $out .= tab . "(\n"; |
431613dd |
90 | $indent_depth++; |
91 | next; |
92 | } elsif ($_ eq ')') { |
93 | --$indent_depth; |
572330a4 |
94 | $out .= tab . ")"; |
431613dd |
95 | } elsif ($_ eq 'follow') { |
3d1e7443 |
96 | $follow_in_effect= 1; |
431613dd |
97 | $stat = 'stat'; |
3d1e7443 |
98 | $Skip_And= 1; |
431613dd |
99 | } elsif ($_ eq '!') { |
572330a4 |
100 | $out .= tab . "!"; |
431613dd |
101 | next; |
102 | } elsif ($_ eq 'name') { |
572330a4 |
103 | $out .= tab . '/' . fileglob_to_re(shift) . "/s"; |
431613dd |
104 | } elsif ($_ eq 'perm') { |
105 | my $onum = shift; |
106 | $onum =~ /^-?[0-7]+$/ |
107 | || die "Malformed -perm argument: $onum\n"; |
572330a4 |
108 | $out .= tab; |
431613dd |
109 | if ($onum =~ s/^-//) { |
110 | $onum = sprintf("0%o", oct($onum) & 07777); |
111 | $out .= "((\$mode & $onum) == $onum)"; |
112 | } else { |
113 | $onum =~ s/^0*/0/; |
114 | $out .= "((\$mode & 0777) == $onum)"; |
115 | } |
116 | } elsif ($_ eq 'type') { |
117 | (my $filetest = shift) =~ tr/s/S/; |
572330a4 |
118 | $out .= tab . "-$filetest _"; |
431613dd |
119 | } elsif ($_ eq 'print') { |
572330a4 |
120 | $out .= tab . 'print("$name\n")'; |
9c4673c1 |
121 | $print_needed = 0; |
431613dd |
122 | } elsif ($_ eq 'print0') { |
572330a4 |
123 | $out .= tab . 'print("$name\0")'; |
9c4673c1 |
124 | $print_needed = 0; |
431613dd |
125 | } elsif ($_ eq 'fstype') { |
126 | my $type = shift; |
572330a4 |
127 | $out .= tab; |
431613dd |
128 | if ($type eq 'nfs') { |
129 | $out .= '($dev < 0)'; |
130 | } else { |
131 | $out .= '($dev >= 0)'; #XXX |
132 | } |
133 | } elsif ($_ eq 'user') { |
134 | my $uname = shift; |
572330a4 |
135 | $out .= tab . "(\$uid == \$uid{'$uname'})"; |
431613dd |
136 | $init{user} = 1; |
137 | } elsif ($_ eq 'group') { |
138 | my $gname = shift; |
572330a4 |
139 | $out .= tab . "(\$gid == \$gid{'$gname'})"; |
431613dd |
140 | $init{group} = 1; |
141 | } elsif ($_ eq 'nouser') { |
572330a4 |
142 | $out .= tab . '!exists $uid{$uid}'; |
431613dd |
143 | $init{user} = 1; |
144 | } elsif ($_ eq 'nogroup') { |
572330a4 |
145 | $out .= tab . '!exists $gid{$gid}'; |
431613dd |
146 | $init{group} = 1; |
147 | } elsif ($_ eq 'links') { |
572330a4 |
148 | $out .= tab . n('$nlink', shift); |
431613dd |
149 | } elsif ($_ eq 'inum') { |
572330a4 |
150 | $out .= tab . n('$ino', shift); |
431613dd |
151 | } elsif ($_ eq 'size') { |
152 | $_ = shift; |
153 | my $n = 'int(((-s _) + 511) / 512)'; |
c7b9dd21 |
154 | if (s/c\z//) { |
431613dd |
155 | $n = 'int(-s _)'; |
c7b9dd21 |
156 | } elsif (s/k\z//) { |
431613dd |
157 | $n = 'int(((-s _) + 1023) / 1024)'; |
158 | } |
572330a4 |
159 | $out .= tab . n($n, $_); |
431613dd |
160 | } elsif ($_ eq 'atime') { |
572330a4 |
161 | $out .= tab . n('int(-A _)', shift); |
431613dd |
162 | } elsif ($_ eq 'mtime') { |
572330a4 |
163 | $out .= tab . n('int(-M _)', shift); |
431613dd |
164 | } elsif ($_ eq 'ctime') { |
572330a4 |
165 | $out .= tab . n('int(-C _)', shift); |
431613dd |
166 | } elsif ($_ eq 'exec') { |
167 | my @cmd = (); |
168 | while (@ARGV && $ARGV[0] ne ';') |
169 | { push(@cmd, shift) } |
170 | shift; |
572330a4 |
171 | $out .= tab; |
431613dd |
172 | if ($cmd[0] =~m#^(?:(?:/usr)?/bin/)?rm$# |
173 | && $cmd[$#cmd] eq '{}' |
174 | && (@cmd == 2 || (@cmd == 3 && $cmd[1] eq '-f'))) { |
175 | if (@cmd == 2) { |
176 | $out .= '(unlink($_) || warn "$name: $!\n")'; |
177 | } elsif (!@ARGV) { |
178 | $out .= 'unlink($_)'; |
179 | } else { |
180 | $out .= '(unlink($_) || 1)'; |
181 | } |
182 | } else { |
183 | for (@cmd) |
184 | { s/'/\\'/g } |
572330a4 |
185 | { local $" = "','"; $out .= "doexec(0, '@cmd')"; } |
186 | $declaresubs .= "sub doexec (\$\@);\n"; |
431613dd |
187 | $init{doexec} = 1; |
188 | } |
9c4673c1 |
189 | $print_needed = 0; |
431613dd |
190 | } elsif ($_ eq 'ok') { |
191 | my @cmd = (); |
192 | while (@ARGV && $ARGV[0] ne ';') |
193 | { push(@cmd, shift) } |
194 | shift; |
572330a4 |
195 | $out .= tab; |
431613dd |
196 | for (@cmd) |
197 | { s/'/\\'/g } |
b739a87e |
198 | { local $" = "','"; $out .= "doexec(1, '@cmd')"; } |
572330a4 |
199 | $declaresubs .= "sub doexec (\$\@);\n"; |
431613dd |
200 | $init{doexec} = 1; |
9c4673c1 |
201 | $print_needed = 0; |
431613dd |
202 | } elsif ($_ eq 'prune') { |
572330a4 |
203 | $out .= tab . '($File::Find::prune = 1)'; |
431613dd |
204 | } elsif ($_ eq 'xdev') { |
572330a4 |
205 | $out .= tab . '!($File::Find::prune |= ($dev != $File::Find::topdev))' |
431613dd |
206 | ; |
207 | } elsif ($_ eq 'newer') { |
208 | my $file = shift; |
209 | my $newername = 'AGE_OF' . $file; |
210 | $newername =~ s/\W/_/g; |
211 | $newername = '$' . $newername; |
572330a4 |
212 | $out .= tab . "(-M _ < $newername)"; |
213 | $initnewer .= "my $newername = -M " . quote($file) . ";\n"; |
431613dd |
214 | } elsif ($_ eq 'eval') { |
215 | my $prog = shift; |
216 | $prog =~ s/'/\\'/g; |
572330a4 |
217 | $out .= tab . "eval {$prog}"; |
a9a4ee8b |
218 | $print_needed = 0; |
431613dd |
219 | } elsif ($_ eq 'depth') { |
220 | $find = 'finddepth'; |
221 | next; |
222 | } elsif ($_ eq 'ls') { |
572330a4 |
223 | $out .= tab . "ls"; |
224 | $declaresubs .= "sub ls ();\n"; |
431613dd |
225 | $init{ls} = 1; |
9c4673c1 |
226 | $print_needed = 0; |
431613dd |
227 | } elsif ($_ eq 'tar') { |
228 | die "-tar must have a filename argument\n" unless @ARGV; |
229 | my $file = shift; |
230 | my $fh = 'FH' . $file; |
231 | $fh =~ s/\W/_/g; |
572330a4 |
232 | $out .= tab . "tar(*$fh, \$name)"; |
233 | $flushall .= "tflushall;\n"; |
234 | $declaresubs .= "sub tar;\nsub tflushall ();\n"; |
235 | $initfile .= "open($fh, " . quote('> ' . $file) . |
431613dd |
236 | qq{) || die "Can't open $fh: \$!\\n";\n}; |
237 | $init{tar} = 1; |
c7b9dd21 |
238 | } elsif (/^(n?)cpio\z/) { |
431613dd |
239 | die "-$_ must have a filename argument\n" unless @ARGV; |
240 | my $file = shift; |
241 | my $fh = 'FH' . $file; |
242 | $fh =~ s/\W/_/g; |
572330a4 |
243 | $out .= tab . "cpio(*$fh, \$name, '$1')"; |
431613dd |
244 | $find = 'finddepth'; |
572330a4 |
245 | $flushall .= "cflushall;\n"; |
246 | $declaresubs .= "sub cpio;\nsub cflushall ();\n"; |
247 | $initfile .= "open($fh, " . quote('> ' . $file) . |
431613dd |
248 | qq{) || die "Can't open $fh: \$!\\n";\n}; |
249 | $init{cpio} = 1; |
250 | } else { |
251 | die "Unrecognized switch: -$_\n"; |
fe14fcc3 |
252 | } |
431613dd |
253 | |
fe14fcc3 |
254 | if (@ARGV) { |
431613dd |
255 | if ($ARGV[0] eq '-o') { |
572330a4 |
256 | { local($statdone) = 1; $out .= "\n" . tab . "||\n"; } |
431613dd |
257 | $statdone = 0 if $indent_depth == 1 && exists $init{delayedstat}; |
258 | $init{saw_or} = 1; |
259 | shift; |
260 | } else { |
3d1e7443 |
261 | $out .= " &&" unless $Skip_And || $ARGV[0] eq ')'; |
431613dd |
262 | $out .= "\n"; |
263 | shift if $ARGV[0] eq '-a'; |
264 | } |
fe14fcc3 |
265 | } |
266 | } |
267 | |
9c4673c1 |
268 | if ($print_needed) { |
178eb895 |
269 | my $t = tab; |
270 | if ($t !~ /&&\s*$/) { $t .= '&& ' } |
271 | $out .= "\n" . $t . 'print("$name\n")'; |
9c4673c1 |
272 | } |
273 | |
431613dd |
274 | |
fe14fcc3 |
275 | print <<"END"; |
4633a7c4 |
276 | $startperl |
5f05dabc |
277 | eval 'exec $perlpath -S \$0 \${1+"\$@"}' |
431613dd |
278 | if 0; #\$running_under_some_shell |
279 | |
280 | use strict; |
281 | use File::Find (); |
282 | |
283 | # Set the variable \$File::Find::dont_use_nlink if you're using AFS, |
284 | # since AFS cheats. |
285 | |
286 | # for the convenience of &wanted calls, including -eval statements: |
287 | use vars qw/*name *dir *prune/; |
288 | *name = *File::Find::name; |
289 | *dir = *File::Find::dir; |
290 | *prune = *File::Find::prune; |
8adcabd8 |
291 | |
572330a4 |
292 | $declaresubs |
fe14fcc3 |
293 | |
572330a4 |
294 | END |
431613dd |
295 | |
7cc8f688 |
296 | if (exists $init{doexec}) { |
297 | print <<'END'; |
298 | use Cwd (); |
299 | my $cwd = Cwd::cwd(); |
300 | |
301 | END |
302 | } |
303 | |
431613dd |
304 | if (exists $init{ls}) { |
fe14fcc3 |
305 | print <<'END'; |
431613dd |
306 | my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx); |
307 | my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
fe14fcc3 |
308 | |
309 | END |
310 | } |
311 | |
431613dd |
312 | if (exists $init{user} || exists $init{ls} || exists $init{tar}) { |
313 | print "my (%uid, %user);\n"; |
314 | print "while (my (\$name, \$pw, \$uid) = getpwent) {\n"; |
315 | print ' $uid{$name} = $uid{$uid} = $uid;', "\n" |
316 | if exists $init{user}; |
317 | print ' $user{$uid} = $name unless exists $user{$uid};', "\n" |
318 | if exists $init{ls} || exists $init{tar}; |
fe14fcc3 |
319 | print "}\n\n"; |
320 | } |
321 | |
431613dd |
322 | if (exists $init{group} || exists $init{ls} || exists $init{tar}) { |
323 | print "my (%gid, %group);\n"; |
324 | print "while (my (\$name, \$pw, \$gid) = getgrent) {\n"; |
325 | print ' $gid{$name} = $gid{$gid} = $gid;', "\n" |
326 | if exists $init{group}; |
327 | print ' $group{$gid} = $name unless exists $group{$gid};', "\n" |
328 | if exists $init{ls} || exists $init{tar}; |
fe14fcc3 |
329 | print "}\n\n"; |
330 | } |
331 | |
431613dd |
332 | print $initnewer, "\n" if $initnewer ne ''; |
333 | print $initfile, "\n" if $initfile ne ''; |
334 | $flushall .= "exit;\n"; |
335 | if (exists $init{declarestat}) { |
336 | $out = <<'END' . $out; |
337 | my ($dev,$ino,$mode,$nlink,$uid,$gid); |
fe14fcc3 |
338 | |
431613dd |
339 | END |
340 | } |
fe14fcc3 |
341 | |
3d1e7443 |
342 | if ( $follow_in_effect ) { |
343 | $out =~ s/lstat\(\$_\)/lstat(_)/; |
fe14fcc3 |
344 | print <<"END"; |
7b8d334a |
345 | $decl |
431613dd |
346 | # Traverse desired filesystems |
3d1e7443 |
347 | File::Find::$find( {wanted => \\&wanted, follow => 1}, $roots); |
fe14fcc3 |
348 | $flushall |
431613dd |
349 | |
fe14fcc3 |
350 | sub wanted { |
351 | $out; |
352 | } |
353 | |
354 | END |
3d1e7443 |
355 | } else { |
356 | print <<"END"; |
357 | $decl |
358 | # Traverse desired filesystems |
359 | File::Find::$find({wanted => \\&wanted}, $roots); |
360 | $flushall |
fe14fcc3 |
361 | |
3d1e7443 |
362 | sub wanted { |
363 | $out; |
364 | } |
365 | |
366 | END |
367 | } |
431613dd |
368 | |
369 | if (exists $init{doexec}) { |
fe14fcc3 |
370 | print <<'END'; |
431613dd |
371 | |
572330a4 |
372 | sub doexec ($@) { |
431613dd |
373 | my $ok = shift; |
572330a4 |
374 | my @command = @_; # copy so we don't try to s/// aliases to constants |
375 | for my $word (@command) |
431613dd |
376 | { $word =~ s#{}#$name#g } |
fe14fcc3 |
377 | if ($ok) { |
431613dd |
378 | my $old = select(STDOUT); |
379 | $| = 1; |
572330a4 |
380 | print "@command"; |
431613dd |
381 | select($old); |
382 | return 0 unless <STDIN> =~ /^y/; |
383 | } |
384 | chdir $cwd; #sigh |
572330a4 |
385 | system @command; |
431613dd |
386 | chdir $File::Find::dir; |
fe14fcc3 |
387 | return !$?; |
388 | } |
389 | |
390 | END |
391 | } |
392 | |
431613dd |
393 | if (exists $init{ls}) { |
394 | print <<'INTRO', <<"SUB", <<'END'; |
fe14fcc3 |
395 | |
431613dd |
396 | sub sizemm { |
397 | my $rdev = shift; |
398 | sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff); |
399 | } |
fe14fcc3 |
400 | |
572330a4 |
401 | sub ls () { |
431613dd |
402 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
403 | INTRO |
404 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
405 | SUB |
406 | my $pname = $name; |
407 | |
408 | $blocks |
409 | or $blocks = int(($size + 1023) / 1024); |
410 | |
411 | my $perms = $rwx[$mode & 7]; |
412 | $mode >>= 3; |
413 | $perms = $rwx[$mode & 7] . $perms; |
414 | $mode >>= 3; |
415 | $perms = $rwx[$mode & 7] . $perms; |
416 | substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _; |
417 | substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _; |
418 | substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _; |
419 | if (-f _) { $perms = '-' . $perms; } |
420 | elsif (-d _) { $perms = 'd' . $perms; } |
421 | elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($_); } |
422 | elsif (-c _) { $perms = 'c' . $perms; $size = sizemm($rdev); } |
423 | elsif (-b _) { $perms = 'b' . $perms; $size = sizemm($rdev); } |
424 | elsif (-p _) { $perms = 'p' . $perms; } |
425 | elsif (-S _) { $perms = 's' . $perms; } |
426 | else { $perms = '?' . $perms; } |
427 | |
428 | my $user = $user{$uid} || $uid; |
429 | my $group = $group{$gid} || $gid; |
430 | |
431 | my ($sec,$min,$hour,$mday,$mon,$timeyear) = localtime($mtime); |
fe14fcc3 |
432 | if (-M _ > 365.25 / 2) { |
431613dd |
433 | $timeyear += 1900; |
434 | } else { |
435 | $timeyear = sprintf("%02d:%02d", $hour, $min); |
436 | } |
437 | |
438 | printf "%5lu %4ld %-10s %3d %-8s %-8s %8s %s %2d %5s %s\n", |
439 | $ino, |
440 | $blocks, |
441 | $perms, |
442 | $nlink, |
443 | $user, |
444 | $group, |
445 | $size, |
446 | $moname[$mon], |
447 | $mday, |
448 | $timeyear, |
449 | $pname; |
fe14fcc3 |
450 | 1; |
451 | } |
452 | |
431613dd |
453 | END |
454 | } |
455 | |
456 | |
457 | if (exists $init{cpio} || exists $init{tar}) { |
458 | print <<'END'; |
459 | |
460 | my %blocks = (); |
461 | |
462 | sub flush { |
463 | my ($fh, $varref, $blksz) = @_; |
464 | |
465 | while (length($$varref) >= $blksz) { |
466 | no strict qw/refs/; |
467 | syswrite($fh, $$varref, $blksz); |
468 | substr($$varref, 0, $blksz) = ''; |
469 | ++$blocks{$fh}; |
470 | } |
fe14fcc3 |
471 | } |
472 | |
473 | END |
474 | } |
475 | |
fe14fcc3 |
476 | |
431613dd |
477 | if (exists $init{cpio}) { |
478 | print <<'INTRO', <<"SUB", <<'END'; |
479 | |
480 | my %cpout = (); |
481 | my %nc = (); |
fe14fcc3 |
482 | |
431613dd |
483 | sub cpio { |
484 | my ($fh, $fname, $nc) = @_; |
485 | my $text = ''; |
486 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
487 | $atime,$mtime,$ctime,$blksize,$blocks); |
488 | local (*IN); |
489 | |
490 | if ( ! defined $fname ) { |
491 | $fname = 'TRAILER!!!'; |
492 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
493 | $atime,$mtime,$ctime,$blksize,$blocks) = (0) x 13; |
494 | } else { |
495 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
496 | INTRO |
497 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
498 | SUB |
499 | if (-f _) { |
500 | open(IN, "./$_\0") || do { |
501 | warn "Couldn't open $fname: $!\n"; |
502 | return; |
503 | } |
504 | } else { |
505 | $text = readlink($_); |
506 | $size = 0 unless defined $text; |
507 | } |
508 | } |
509 | |
510 | $fname =~ s#^\./##; |
fe14fcc3 |
511 | $nc{$fh} = $nc; |
512 | if ($nc eq 'n') { |
431613dd |
513 | $cpout{$fh} .= |
514 | sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0", |
515 | 070707, |
516 | $dev & 0777777, |
517 | $ino & 0777777, |
518 | $mode & 0777777, |
519 | $uid & 0777777, |
520 | $gid & 0777777, |
521 | $nlink & 0777777, |
522 | $rdev & 0177777, |
523 | $mtime, |
524 | length($fname)+1, |
525 | $size, |
526 | $fname); |
527 | } else { |
528 | $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1; |
529 | $cpout{$fh} .= pack("SSSSSSSSLSLa*", |
530 | 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime, |
531 | length($fname)+1, $size, |
532 | $fname . (length($fname) & 1 ? "\0" : "\0\0")); |
fe14fcc3 |
533 | } |
fe14fcc3 |
534 | |
431613dd |
535 | if ($text ne '') { |
536 | $cpout{$fh} .= $text; |
537 | } elsif ($size) { |
538 | my $l; |
539 | flush($fh, \$cpout{$fh}, 5120) |
540 | while ($l = length($cpout{$fh})) >= 5120; |
541 | while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) { |
542 | flush($fh, \$cpout{$fh}, 5120); |
543 | $l = length($cpout{$fh}); |
544 | } |
545 | close IN; |
fe14fcc3 |
546 | } |
547 | } |
548 | |
572330a4 |
549 | sub cflushall () { |
431613dd |
550 | for my $fh (keys %cpout) { |
572330a4 |
551 | cpio($fh, undef, $nc{$fh}); |
431613dd |
552 | $cpout{$fh} .= "0" x (5120 - length($cpout{$fh})); |
553 | flush($fh, \$cpout{$fh}, 5120); |
554 | print $blocks{$fh} * 10, " blocks\n"; |
fe14fcc3 |
555 | } |
556 | } |
557 | |
558 | END |
559 | } |
560 | |
431613dd |
561 | if (exists $init{tar}) { |
562 | print <<'INTRO', <<"SUB", <<'END'; |
563 | |
564 | my %tarout = (); |
565 | my %linkseen = (); |
566 | |
fe14fcc3 |
567 | sub tar { |
431613dd |
568 | my ($fh, $fname) = @_; |
569 | my $prefix = ''; |
570 | my $typeflag = '0'; |
571 | my $linkname; |
572 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
573 | INTRO |
574 | \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_); |
575 | SUB |
576 | local (*IN); |
fe14fcc3 |
577 | |
431613dd |
578 | if ($nlink > 1) { |
579 | if ($linkname = $linkseen{$fh, $dev, $ino}) { |
580 | if (length($linkname) > 100) { |
581 | warn "$0: omitting file with linkname ", |
582 | "too long for tar output: $linkname\n"; |
583 | return; |
584 | } |
585 | $typeflag = '1'; |
586 | $size = 0; |
587 | } else { |
588 | $linkseen{$fh, $dev, $ino} = $fname; |
589 | } |
590 | } |
591 | if ($typeflag eq '0') { |
592 | if (-f _) { |
593 | open(IN, "./$_\0") || do { |
594 | warn "Couldn't open $fname: $!\n"; |
595 | return; |
596 | } |
597 | } else { |
598 | $linkname = readlink($_); |
599 | if (defined $linkname) { $typeflag = '2' } |
600 | elsif (-c _) { $typeflag = '3' } |
601 | elsif (-b _) { $typeflag = '4' } |
602 | elsif (-d _) { $typeflag = '5' } |
603 | elsif (-p _) { $typeflag = '6' } |
604 | } |
605 | } |
606 | |
607 | if (length($fname) > 100) { |
608 | ($prefix, $fname) = ($fname =~ m#\A(.*?)/(.{,100})\Z(?!\n)#); |
609 | if (!defined($fname) || length($prefix) > 155) { |
610 | warn "$0: omitting file with name too long for tar output: ", |
611 | $fname, "\n"; |
612 | return; |
613 | } |
614 | } |
615 | |
616 | $size = 0 if $typeflag ne '0'; |
617 | my $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155", |
618 | $fname, |
619 | sprintf("%7o ", $mode & 0777), |
620 | sprintf("%7o ", $uid & 0777777), |
621 | sprintf("%7o ", $gid & 0777777), |
622 | sprintf("%11o ", $size), |
623 | sprintf("%11o ", $mtime), |
624 | ' 'x8, |
625 | $typeflag, |
626 | defined $linkname ? $linkname : '', |
627 | "ustar\0", |
628 | "00", |
629 | $user{$uid}, |
630 | $group{$gid}, |
631 | ($rdev >> 8) & 0xff, |
632 | $rdev & 0xff, |
633 | $prefix, |
634 | ); |
635 | substr($header, 148, 8) = sprintf("%7o ", unpack("%16C*", $header)); |
636 | my $l = length($header) % 512; |
fe14fcc3 |
637 | $tarout{$fh} .= $header; |
638 | $tarout{$fh} .= "\0" x (512 - $l) if $l; |
fe14fcc3 |
639 | |
431613dd |
640 | if ($size) { |
641 | flush($fh, \$tarout{$fh}, 10240) |
642 | while ($l = length($tarout{$fh})) >= 10240; |
643 | while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) { |
644 | my $slop = length($tarout{$fh}) % 512; |
645 | $tarout{$fh} .= "\0" x (512 - $slop) if $slop; |
646 | flush($fh, \$tarout{$fh}, 10240); |
647 | $l = length($tarout{$fh}); |
648 | } |
649 | close IN; |
fe14fcc3 |
650 | } |
651 | } |
652 | |
572330a4 |
653 | sub tflushall () { |
431613dd |
654 | my $len; |
655 | for my $fh (keys %tarout) { |
656 | $len = 10240 - length($tarout{$fh}); |
657 | $len += 10240 if $len < 1024; |
658 | $tarout{$fh} .= "\0" x $len; |
659 | flush($fh, \$tarout{$fh}, 10240); |
fe14fcc3 |
660 | } |
661 | } |
662 | |
663 | END |
664 | } |
665 | |
666 | exit; |
667 | |
668 | ############################################################################ |
669 | |
572330a4 |
670 | sub tab () { |
431613dd |
671 | my $tabstring; |
fe14fcc3 |
672 | |
431613dd |
673 | $tabstring = "\t" x ($indent_depth/2) . ' ' x ($indent_depth%2 * 4); |
1c3d792e |
674 | if (!$statdone) { |
431613dd |
675 | if ($_ =~ /^(?:name|print|prune|exec|ok|\(|\))/) { |
676 | $init{delayedstat} = 1; |
677 | } else { |
678 | my $statcall = '(($dev,$ino,$mode,$nlink,$uid,$gid) = ' |
679 | . $stat . '($_))'; |
680 | if (exists $init{saw_or}) { |
681 | $tabstring .= "(\$nlink || $statcall) &&\n" . $tabstring; |
682 | } else { |
683 | $tabstring .= "$statcall &&\n" . $tabstring; |
684 | } |
685 | $statdone = 1; |
686 | $init{declarestat} = 1; |
687 | } |
fe14fcc3 |
688 | } |
689 | $tabstring =~ s/^\s+/ / if $out =~ /!$/; |
690 | $tabstring; |
691 | } |
692 | |
572330a4 |
693 | sub fileglob_to_re ($) { |
431613dd |
694 | my $x = shift; |
50097298 |
695 | $x =~ s#([./^\$()+])#\\$1#g; |
431613dd |
696 | $x =~ s#([?*])#.$1#g; |
c7b9dd21 |
697 | "^$x\\z"; |
fe14fcc3 |
698 | } |
699 | |
572330a4 |
700 | sub n ($$) { |
431613dd |
701 | my ($pre, $n) = @_; |
1c3d792e |
702 | $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /; |
703 | $n =~ s/ 0*(\d)/ $1/; |
431613dd |
704 | "($pre $n)"; |
fe14fcc3 |
705 | } |
706 | |
572330a4 |
707 | sub quote ($) { |
431613dd |
708 | my $string = shift; |
2305df61 |
709 | $string =~ s/\\/\\\\/g; |
431613dd |
710 | $string =~ s/'/\\'/g; |
fe14fcc3 |
711 | "'$string'"; |
712 | } |
431613dd |
713 | |
714 | __END__ |
715 | |
716 | =head1 NAME |
717 | |
718 | find2perl - translate find command lines to Perl code |
719 | |
720 | =head1 SYNOPSIS |
721 | |
722 | find2perl [paths] [predicates] | perl |
723 | |
724 | =head1 DESCRIPTION |
725 | |
726 | find2perl is a little translator to convert find command lines to |
727 | equivalent Perl code. The resulting code is typically faster than |
728 | running find itself. |
729 | |
730 | "paths" are a set of paths where find2perl will start its searches and |
731 | "predicates" are taken from the following list. |
732 | |
733 | =over 4 |
734 | |
735 | =item C<! PREDICATE> |
736 | |
737 | Negate the sense of the following predicate. The C<!> must be passed as |
738 | a distinct argument, so it may need to be surrounded by whitespace and/or |
739 | quoted from interpretation by the shell using a backslash (just as with |
740 | using C<find(1)>). |
741 | |
742 | =item C<( PREDICATES )> |
743 | |
744 | Group the given PREDICATES. The parentheses must be passed as distinct |
745 | arguments, so they may need to be surrounded by whitespace and/or |
746 | quoted from interpretation by the shell using a backslash (just as with |
747 | using C<find(1)>). |
748 | |
749 | =item C<PREDICATE1 PREDICATE2> |
750 | |
751 | True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not |
752 | evaluated if PREDICATE1 is false. |
753 | |
754 | =item C<PREDICATE1 -o PREDICATE2> |
755 | |
756 | True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is |
757 | not evaluated if PREDICATE1 is true. |
758 | |
759 | =item C<-follow> |
760 | |
3d1e7443 |
761 | Follow (dereference) symlinks. The checking of file attributes depends |
762 | on the position of the C<-follow> option. If it precedes the file |
763 | check option, an C<stat> is done which means the file check applies to the |
764 | file the symbolic link is pointing to. If C<-follow> option follows the |
765 | file check option, this now applies to the symbolic link itself, i.e. |
766 | an C<lstat> is done. |
431613dd |
767 | |
768 | =item C<-depth> |
769 | |
770 | Change directory traversal algorithm from breadth-first to depth-first. |
771 | |
772 | =item C<-prune> |
773 | |
774 | Do not descend into the directory currently matched. |
775 | |
776 | =item C<-xdev> |
777 | |
778 | Do not traverse mount points (prunes search at mount-point directories). |
779 | |
780 | =item C<-name GLOB> |
781 | |
782 | File name matches specified GLOB wildcard pattern. GLOB may need to be |
783 | quoted to avoid interpretation by the shell (just as with using |
784 | C<find(1)>). |
785 | |
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 |
831 | counted in characters (bytes) and a suffix of "k" specifes that |
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 | |
910 | find |
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; |