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)) { }
132 if (m#^(/bin/)?rm -f {}$#) {
134 $out .= &tab . 'unlink($_)';
137 $out .= &tab . '(unlink($_) || 1)';
140 elsif (m#^(/bin/)?rm {}$#) {
141 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
144 for (@cmd) { s/'/\\'/g; }
146 $out .= &tab . "&exec(0, '@cmd')";
152 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
154 for (@cmd) { s/'/\\'/g; }
156 $out .= &tab . "&exec(1, '@cmd')";
160 elsif ($_ eq 'prune') {
161 $out .= &tab . '($prune = 1)';
163 elsif ($_ eq 'xdev') {
164 $out .= &tab . '(($prune |= ($dev != $topdev)),1)';
166 elsif ($_ eq 'newer') {
169 $newername = 'AGE_OF' . $file;
170 $newername =~ s/[^\w]/_/g;
171 $newername = '$' . $newername;
172 $out .= "-M _ < $newername";
173 $initnewer .= "$newername = -M " . "e($file) . ";\n";
175 elsif ($_ eq 'eval') {
176 $prog = "e(shift);
177 $out .= &tab . "eval $prog";
179 elsif ($_ eq 'depth') {
184 $out .= &tab . "&ls";
187 elsif ($_ eq 'tar') {
189 die "-tar must have a filename argument\n" unless @ARGV;
195 $initfile .= "open($fh, " . "e($file) .
196 qq{) || die "Can't open $fh: \$!\\n";\n};
198 $flushall = "\n&tflushall;\n";
203 die "-$_ must have a filename argument\n" unless @ARGV;
207 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
209 $initfile .= "open($fh, " . "e($file) .
210 qq{) || die "Can't open $fh: \$!\\n";\n};
212 $flushall = "\n&flushall;\n";
215 die "Unrecognized switch: -$_\n";
218 if ($ARGV[0] eq '-o') {
219 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
220 $statdone = 0 if $indent == 1 && $delayedstat;
225 $out .= " &&" unless $ARGV[0] eq ')';
227 shift if $ARGV[0] eq '-a';
239 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
240 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
245 if ($inituser || $initls) {
246 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
247 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
248 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
252 if ($initgroup || $initls) {
253 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
254 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
255 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
259 print $initnewer, "\n" if $initnewer;
261 print $initfile, "\n" if $initfile;
263 $find = $depth ? "finddepth" : "find";
267 # Traverse desired filesystems
282 local($ok, @cmd) = @_;
283 foreach $word (@cmd) {
284 $word =~ s#{}#$name#g;
287 local($old) = select(STDOUT);
291 return 0 unless <STDIN> =~ /^y/;
305 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
306 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
310 if (defined $blocks) {
311 $blocks = int(($blocks + 1) / 2);
314 $blocks = int(($size + 1023) / 1024);
317 if (-f _) { $perms = '-'; }
318 elsif (-d _) { $perms = 'd'; }
319 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
320 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
321 elsif (-p _) { $perms = 'p'; }
322 elsif (-S _) { $perms = 's'; }
323 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
326 $tmp = $rwx[$tmpmode & 7];
328 $tmp = $rwx[$tmpmode & 7] . $tmp;
330 $tmp = $rwx[$tmpmode & 7] . $tmp;
331 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
332 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
333 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
336 $user = $user{$uid} || $uid;
337 $group = $group{$gid} || $gid;
339 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
340 $moname = $moname[$mon];
341 if (-M _ > 365.25 / 2) {
342 $timeyear = '19' . $year;
345 $timeyear = sprintf("%02d:%02d", $hour, $min);
348 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
364 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
376 if ($name eq 'TRAILER!!!') {
381 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
382 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
384 open(IN, "./$_\0") || do {
385 warn "Couldn't open $name: $!\n";
390 $text = readlink($_);
391 $size = 0 unless defined $text;
395 ($nm = $name) =~ s#^\./##;
399 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
414 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
415 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
416 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
417 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
420 $cpout{$fh} .= $text;
423 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
424 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
426 $l = length($cpout{$fh});
435 while (length($cpout{$fh}) >= 5120) {
436 syswrite($fh,$cpout{$fh},5120);
438 substr($cpout{$fh}, 0, 5120) = '';
443 $name = 'TRAILER!!!';
444 foreach $fh (keys %cpout) {
446 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
448 print $blocks{$fh} * 10, " blocks\n";
459 local($linkname,$header,$l,$slop);
460 local($linkflag) = "\0";
462 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
463 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
466 if ($linkname = $linkseen{$fh,$dev,$ino}) {
470 $linkseen{$fh,$dev,$ino} = $nm;
474 open(IN, "./$_\0") || do {
475 warn "Couldn't open $name: $!\n";
478 $size = 0 if $linkflag ne "\0";
481 $linkname = readlink($_);
482 $linkflag = 2 if defined $linkname;
487 $header = pack("a100a8a8a8a12a12a8a1a100",
489 sprintf("%6o ", $mode & 0777),
490 sprintf("%6o ", $uid & 0777777),
491 sprintf("%6o ", $gid & 0777777),
492 sprintf("%11o ", $size),
493 sprintf("%11o ", $mtime),
497 $l = length($header) % 512;
498 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
499 substr($header, 154, 1) = "\0"; # blech
500 $tarout{$fh} .= $header;
501 $tarout{$fh} .= "\0" x (512 - $l) if $l;
503 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
504 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
505 $slop = length($tarout{$fh}) % 512;
506 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
508 $l = length($tarout{$fh});
517 while (length($tarout{$fh}) >= 10240) {
518 syswrite($fh,$tarout{$fh},10240);
520 substr($tarout{$fh}, 0, 10240) = '';
527 foreach $fh (keys %tarout) {
528 $len = 10240 - length($tarout{$fh});
529 $len += 10240 if $len < 1024;
530 $tarout{$fh} .= "\0" x $len;
540 ############################################################################
545 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
547 if ($_ =~ /^(name|print)/) {
552 $tabstring .= <<'ENDOFSTAT' . $tabstring;
553 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
557 $tabstring .= <<'ENDOFSTAT' . $tabstring;
558 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
564 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
571 $tmp =~ s/([.^\$()])/\\$1/g;
572 $tmp =~ s/([?*])/.$1/g;
579 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
580 $n =~ s/ 0*(\d)/ $1/;