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