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.
22 $spitshell >find2perl <<!GROK!THIS!
29 : In the following dollars and backticks do not need the extra backslash.
30 $spitshell >>find2perl <<'!NO!SUBS!'
32 while ($ARGV[0] =~ /^[^-!(]/) {
35 @roots = ('.') unless @roots;
36 for (@roots) { $_ = "e($_); }
37 $roots = join(',', @roots);
43 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
57 elsif ($_ eq 'name') {
59 $pat = &fileglob_to_re(shift);
60 $out .= '/' . $pat . "/";
62 elsif ($_ eq 'perm') {
64 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
65 if ($onum =~ s/^-//) {
66 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
67 $out .= &tab . "(\$mode & $onum) == $onum";
70 $onum = '0' . $onum unless $onum =~ /^0/;
71 $out .= &tab . "(\$mode & 0777) == $onum";
74 elsif ($_ eq 'type') {
75 ($filetest = shift) =~ tr/s/S/;
76 $out .= &tab . "-$filetest _";
78 elsif ($_ eq 'print') {
79 $out .= &tab . 'print("$name\n")';
81 elsif ($_ eq 'print0') {
82 $out .= &tab . 'print("$name\0")';
84 elsif ($_ eq 'fstype') {
88 { $out .= '$dev < 0'; }
90 { $out .= '$dev >= 0'; }
92 elsif ($_ eq 'user') {
94 $out .= &tab . "\$uid == \$uid{'$uname'}";
97 elsif ($_ eq 'group') {
99 $out .= &tab . "\$gid == \$gid('$gname')";
102 elsif ($_ eq 'nouser') {
103 $out .= &tab . '!defined $uid{$uid}';
106 elsif ($_ eq 'nogroup') {
107 $out .= &tab . '!defined $gid{$gid}';
110 elsif ($_ eq 'links') {
111 $out .= &tab . '$nlink ' . &n(shift);
113 elsif ($_ eq 'inum') {
114 $out .= &tab . '$ino ' . &n(shift);
116 elsif ($_ eq 'size') {
117 $out .= &tab . 'int((-s _ + 511) / 512) ' . &n(shift);
119 elsif ($_ eq 'atime') {
120 $out .= &tab . 'int(-A _) ' . &n(shift);
122 elsif ($_ eq 'mtime') {
123 $out .= &tab . 'int(-M _) ' . &n(shift);
125 elsif ($_ eq 'ctime') {
126 $out .= &tab . 'int(-C _) ' . &n(shift);
128 elsif ($_ eq 'exec') {
129 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
131 for (@cmd) { s/'/\\'/g; }
133 $out .= &tab . "&exec(0, '@cmd')";
138 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
140 for (@cmd) { s/'/\\'/g; }
142 $out .= &tab . "&exec(1, '@cmd')";
146 elsif ($_ eq 'prune') {
147 $out .= &tab . '($prune = 1)';
149 elsif ($_ eq 'xdev') {
150 $out .= &tab . '(($prune |= ($dev != $topdev)),1)';
152 elsif ($_ eq 'newer') {
155 $newername = 'AGE_OF' . $file;
156 $newername =~ s/[^\w]/_/g;
157 $newername = '$' . $newername;
158 $out .= "-M _ < $newername";
159 $initnewer .= "$newername = -M " . "e($file) . ";\n";
161 elsif ($_ eq 'eval') {
162 $prog = "e(shift);
163 $out .= &tab . "eval $prog";
165 elsif ($_ eq 'depth') {
170 $out .= &tab . "&ls";
173 elsif ($_ eq 'tar') {
175 die "-tar must have a filename argument\n" unless @ARGV;
181 $initfile .= "open($fh, " . "e($file) .
182 qq{) || die "Can't open $fh: \$!\\n";\n};
184 $flushall = "\n&tflushall;\n";
189 die "-$_ must have a filename argument\n" unless @ARGV;
193 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
195 $initfile .= "open($fh, " . "e($file) .
196 qq{) || die "Can't open $fh: \$!\\n";\n};
198 $flushall = "\n&flushall;\n";
201 die "Unrecognized switch: -$_\n";
204 if ($ARGV[0] eq '-o') {
205 local($indent) = $indent - 4;
206 $out .= "\n" . &tab . "||\n";
210 $out .= " &&" unless $ARGV[0] eq ')';
212 shift if $ARGV[0] eq '-a';
224 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
225 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
230 if ($inituser || $initls) {
231 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
232 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
233 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
237 if ($initgroup || $initls) {
238 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
239 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
240 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
244 print $initnewer, "\n" if $initnewer;
246 print $initfile, "\n" if $initfile;
249 # Traverse desired filesystems
264 foreach $topdir (@_) {
265 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
266 || (warn("Can't stat $topdir: $!\n"), next);
268 if (chdir($topdir)) {
272 $topdir = '' if $topdir eq '/';
273 &dodir($topdir,$topnlink);
274 ($dir,$_) = ($topdir,'.');
281 ($dir,$_) = ($topdir,'.');
284 $topdir = '' if $topdir eq '/';
285 &dodir($topdir,$topnlink);
291 warn "Can't cd to $topdir: $!\n";
295 unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
296 ($dir,$_) = ('.', $topdir);
298 chdir $dir && &wanted;
305 local($dir,$nlink) = @_;
306 local($dev,$ino,$mode,$subcount);
309 # Get the list of files in the current directory.
311 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
312 local(@filenames) = readdir(DIR);
315 if ($nlink == 2) { # This dir has no subdirectories.
323 else { # This dir has subdirectories.
324 $subcount = $nlink - 2;
331 print <<'END' unless $depth;
335 if ($subcount > 0) { # Seen all the subdirs?
337 # Get link count and check for directoriness.
339 ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
343 # It really is a directory, so do it recursively.
345 if (!$prune && chdir $_) {
346 &dodir($name,$nlink);
353 print <<'END' if $depth;
366 local($ok, @cmd) = @_;
367 foreach $word (@cmd) {
368 $word =~ s#{}#$name#g;
371 local($old) = select(STDOUT);
375 return 0 unless <STDIN> =~ /^y/;
389 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
390 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
394 if (defined $blocks) {
395 $blocks = int(($blocks + 1) / 2);
398 $blocks = int(($size + 1023) / 1024);
401 if (-f _) { $perms = '-'; }
402 elsif (-d _) { $perms = 'd'; }
403 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
404 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
405 elsif (-p _) { $perms = 'p'; }
406 elsif (-S _) { $perms = 's'; }
407 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
410 $tmp = $rwx[$tmpmode & 7];
412 $tmp = $rwx[$tmpmode & 7] . $tmp;
414 $tmp = $rwx[$tmpmode & 7] . $tmp;
415 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
416 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
417 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
420 $user = $user{$uid} || $uid;
421 $group = $group{$gid} || $gid;
423 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
424 $moname = $moname[$mon];
425 if (-M _ > 365.25 / 2) {
426 $timeyear = '19' . $year;
429 $timeyear = sprintf("%02d:%02d", $hour, $min);
432 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
448 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
460 if ($name eq 'TRAILER!!!') {
465 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
466 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
469 warn "Couldn't open $name: $!\n";
474 $text = readlink($_);
475 $size = 0 unless defined $text;
479 ($nm = $name) =~ s#^\./##;
483 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
498 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
499 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
500 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
501 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
504 $cpout{$fh} .= $text;
507 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
508 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
510 $l = length($cpout{$fh});
519 while (length($cpout{$fh}) >= 5120) {
520 syswrite($fh,$cpout{$fh},5120);
522 substr($cpout{$fh}, 0, 5120) = '';
527 $name = 'TRAILER!!!';
528 foreach $fh (keys %cpout) {
530 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
532 print $blocks{$fh} * 10, " blocks\n";
543 local($linkname,$header,$l,$slop);
544 local($linkflag) = "\0";
546 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
547 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
550 if ($linkname = $linkseen{$fh,$dev,$ino}) {
554 $linkseen{$fh,$dev,$ino} = $nm;
559 warn "Couldn't open $name: $!\n";
562 $size = 0 if $linkflag ne "\0";
565 $linkname = readlink($_);
566 $linkflag = 2 if defined $linkname;
571 $header = pack("a100a8a8a8a12a12a8a1a100",
573 sprintf("%6o ", $mode & 0777),
574 sprintf("%6o ", $uid & 0777777),
575 sprintf("%6o ", $gid & 0777777),
576 sprintf("%11o ", $size),
577 sprintf("%11o ", $mtime),
581 $l = length($header) % 512;
582 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
583 substr($header, 154, 1) = "\0"; # blech
584 $tarout{$fh} .= $header;
585 $tarout{$fh} .= "\0" x (512 - $l) if $l;
587 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
588 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
589 $slop = length($tarout{$fh}) % 512;
590 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
592 $l = length($tarout{$fh});
601 while (length($tarout{$fh}) >= 10240) {
602 syswrite($fh,$tarout{$fh},10240);
604 substr($tarout{$fh}, 0, 10240) = '';
611 foreach $fh (keys %tarout) {
612 $len = 10240 - length($tarout{$fh});
613 $len += 10240 if $len < 1024;
614 $tarout{$fh} .= "\0" x $len;
624 ############################################################################
629 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
630 if ($_ !~ /^(name|print)/) {
632 $tabstring .= <<'ENDOFSTAT' . $tabstring;
633 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
638 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
645 $tmp =~ s/([.^\$()])/\\$1/g;
646 $tmp =~ s/([?*])/.$1/g;
653 $n =~ s/^-0*/< / || $n =~ s/^\+0*/> / || $n =~ s/^0*/== /;