3 if test ! -f config.sh; then
5 ln ../../config.sh . || \
6 ln ../../../config.sh . || \
7 (echo "Can't find config.sh."; exit 1)
12 : This forces SH files to create target in same directory as SH file.
13 : This is so that make depend always knows where to find SH derivatives.
15 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
17 echo "Extracting find2perl (with variable substitutions)"
18 : This section of the file will have variable substitutions done on it.
19 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
20 : Protect any dollar signs and backticks that you do not want interpreted
21 : by putting a backslash in front. You may delete these comments.
23 $spitshell >find2perl <<!GROK!THIS!
26 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
27 if \$running_under_some_shell;
33 : In the following dollars and backticks do not need the extra backslash.
34 $spitshell >>find2perl <<'!NO!SUBS!'
36 while ($ARGV[0] =~ /^[^-!(]/) {
39 @roots = ('.') unless @roots;
40 for (@roots) { $_ = "e($_); }
41 $roots = join(',', @roots);
47 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
61 elsif ($_ eq 'name') {
63 $pat = &fileglob_to_re(shift);
64 $out .= '/' . $pat . "/";
66 elsif ($_ eq 'perm') {
68 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
69 if ($onum =~ s/^-//) {
70 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
71 $out .= &tab . "(\$mode & $onum) == $onum";
74 $onum = '0' . $onum unless $onum =~ /^0/;
75 $out .= &tab . "(\$mode & 0777) == $onum";
78 elsif ($_ eq 'type') {
79 ($filetest = shift) =~ tr/s/S/;
80 $out .= &tab . "-$filetest _";
82 elsif ($_ eq 'print') {
83 $out .= &tab . 'print("$name\n")';
85 elsif ($_ eq 'print0') {
86 $out .= &tab . 'print("$name\0")';
88 elsif ($_ eq 'fstype') {
92 { $out .= '$dev < 0'; }
94 { $out .= '$dev >= 0'; }
96 elsif ($_ eq 'user') {
98 $out .= &tab . "\$uid == \$uid{'$uname'}";
101 elsif ($_ eq 'group') {
103 $out .= &tab . "\$gid == \$gid{'$gname'}";
106 elsif ($_ eq 'nouser') {
107 $out .= &tab . '!defined $uid{$uid}';
110 elsif ($_ eq 'nogroup') {
111 $out .= &tab . '!defined $gid{$gid}';
114 elsif ($_ eq 'links') {
115 $out .= &tab . '$nlink ' . &n(shift);
117 elsif ($_ eq 'inum') {
118 $out .= &tab . '$ino ' . &n(shift);
120 elsif ($_ eq 'size') {
121 $out .= &tab . 'int((-s _ + 511) / 512) ' . &n(shift);
123 elsif ($_ eq 'atime') {
124 $out .= &tab . 'int(-A _) ' . &n(shift);
126 elsif ($_ eq 'mtime') {
127 $out .= &tab . 'int(-M _) ' . &n(shift);
129 elsif ($_ eq 'ctime') {
130 $out .= &tab . 'int(-C _) ' . &n(shift);
132 elsif ($_ eq 'exec') {
133 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
136 if (m#^(/bin/)?rm -f {}$#) {
138 $out .= &tab . 'unlink($_)';
141 $out .= &tab . '(unlink($_) || 1)';
144 elsif (m#^(/bin/)?rm {}$#) {
145 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
148 for (@cmd) { s/'/\\'/g; }
150 $out .= &tab . "&exec(0, '@cmd')";
156 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
158 for (@cmd) { s/'/\\'/g; }
160 $out .= &tab . "&exec(1, '@cmd')";
164 elsif ($_ eq 'prune') {
165 $out .= &tab . '($prune = 1)';
167 elsif ($_ eq 'xdev') {
168 $out .= &tab . '(($prune |= ($dev != $topdev)),1)';
170 elsif ($_ eq 'newer') {
173 $newername = 'AGE_OF' . $file;
174 $newername =~ s/[^\w]/_/g;
175 $newername = '$' . $newername;
176 $out .= "-M _ < $newername";
177 $initnewer .= "$newername = -M " . "e($file) . ";\n";
179 elsif ($_ eq 'eval') {
180 $prog = "e(shift);
181 $out .= &tab . "eval $prog";
183 elsif ($_ eq 'depth') {
188 $out .= &tab . "&ls";
191 elsif ($_ eq 'tar') {
193 die "-tar must have a filename argument\n" unless @ARGV;
199 $initfile .= "open($fh, " . "e($file) .
200 qq{) || die "Can't open $fh: \$!\\n";\n};
202 $flushall = "\n&tflushall;\n";
207 die "-$_ must have a filename argument\n" unless @ARGV;
211 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
213 $initfile .= "open($fh, " . "e($file) .
214 qq{) || die "Can't open $fh: \$!\\n";\n};
216 $flushall = "\n&flushall;\n";
219 die "Unrecognized switch: -$_\n";
222 if ($ARGV[0] eq '-o') {
223 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
224 $statdone = 0 if $indent == 1 && $delayedstat;
229 $out .= " &&" unless $ARGV[0] eq ')';
231 shift if $ARGV[0] eq '-a';
239 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
240 if \$running_under_some_shell;
246 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
247 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
252 if ($inituser || $initls) {
253 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
254 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
255 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
259 if ($initgroup || $initls) {
260 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
261 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
262 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
266 print $initnewer, "\n" if $initnewer;
268 print $initfile, "\n" if $initfile;
270 $find = $depth ? "finddepth" : "find";
274 # Traverse desired filesystems
289 local($ok, @cmd) = @_;
290 foreach $word (@cmd) {
291 $word =~ s#{}#$name#g;
294 local($old) = select(STDOUT);
298 return 0 unless <STDIN> =~ /^y/;
312 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
313 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
317 if (defined $blocks) {
318 $blocks = int(($blocks + 1) / 2);
321 $blocks = int(($size + 1023) / 1024);
324 if (-f _) { $perms = '-'; }
325 elsif (-d _) { $perms = 'd'; }
326 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
327 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
328 elsif (-p _) { $perms = 'p'; }
329 elsif (-S _) { $perms = 's'; }
330 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
333 $tmp = $rwx[$tmpmode & 7];
335 $tmp = $rwx[$tmpmode & 7] . $tmp;
337 $tmp = $rwx[$tmpmode & 7] . $tmp;
338 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
339 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
340 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
343 $user = $user{$uid} || $uid;
344 $group = $group{$gid} || $gid;
346 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
347 $moname = $moname[$mon];
348 if (-M _ > 365.25 / 2) {
349 $timeyear = '19' . $year;
352 $timeyear = sprintf("%02d:%02d", $hour, $min);
355 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
371 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
383 if ($name eq 'TRAILER!!!') {
388 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
389 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
391 open(IN, "./$_\0") || do {
392 warn "Couldn't open $name: $!\n";
397 $text = readlink($_);
398 $size = 0 unless defined $text;
402 ($nm = $name) =~ s#^\./##;
406 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
421 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
422 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
423 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
424 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
427 $cpout{$fh} .= $text;
430 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
431 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
433 $l = length($cpout{$fh});
442 while (length($cpout{$fh}) >= 5120) {
443 syswrite($fh,$cpout{$fh},5120);
445 substr($cpout{$fh}, 0, 5120) = '';
450 $name = 'TRAILER!!!';
451 foreach $fh (keys %cpout) {
453 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
455 print $blocks{$fh} * 10, " blocks\n";
466 local($linkname,$header,$l,$slop);
467 local($linkflag) = "\0";
469 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
470 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
473 if ($linkname = $linkseen{$fh,$dev,$ino}) {
477 $linkseen{$fh,$dev,$ino} = $nm;
481 open(IN, "./$_\0") || do {
482 warn "Couldn't open $name: $!\n";
485 $size = 0 if $linkflag ne "\0";
488 $linkname = readlink($_);
489 $linkflag = 2 if defined $linkname;
494 $header = pack("a100a8a8a8a12a12a8a1a100",
496 sprintf("%6o ", $mode & 0777),
497 sprintf("%6o ", $uid & 0777777),
498 sprintf("%6o ", $gid & 0777777),
499 sprintf("%11o ", $size),
500 sprintf("%11o ", $mtime),
504 $l = length($header) % 512;
505 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
506 substr($header, 154, 1) = "\0"; # blech
507 $tarout{$fh} .= $header;
508 $tarout{$fh} .= "\0" x (512 - $l) if $l;
510 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
511 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
512 $slop = length($tarout{$fh}) % 512;
513 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
515 $l = length($tarout{$fh});
524 while (length($tarout{$fh}) >= 10240) {
525 syswrite($fh,$tarout{$fh},10240);
527 substr($tarout{$fh}, 0, 10240) = '';
534 foreach $fh (keys %tarout) {
535 $len = 10240 - length($tarout{$fh});
536 $len += 10240 if $len < 1024;
537 $tarout{$fh} .= "\0" x $len;
547 ############################################################################
552 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
554 if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
559 $tabstring .= <<'ENDOFSTAT' . $tabstring;
560 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
564 $tabstring .= <<'ENDOFSTAT' . $tabstring;
565 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
571 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
578 $tmp =~ s/([.^\$()])/\\$1/g;
579 $tmp =~ s/([?*])/.$1/g;
586 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
587 $n =~ s/ 0*(\d)/ $1/;