3 eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
4 if $running_under_some_shell;
6 $bin = "/usr/local/bin";
9 while ($ARGV[0] =~ /^[^-!(]/) {
12 @roots = ('.') unless @roots;
13 for (@roots) { $_ = "e($_); }
14 $roots = join(',', @roots);
20 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
34 elsif ($_ eq 'name') {
36 $pat = &fileglob_to_re(shift);
37 $out .= '/' . $pat . "/";
39 elsif ($_ eq 'perm') {
41 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
42 if ($onum =~ s/^-//) {
43 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
44 $out .= &tab . "((\$mode & $onum) == $onum)";
47 $onum = '0' . $onum unless $onum =~ /^0/;
48 $out .= &tab . "((\$mode & 0777) == $onum)";
51 elsif ($_ eq 'type') {
52 ($filetest = shift) =~ tr/s/S/;
53 $out .= &tab . "-$filetest _";
55 elsif ($_ eq 'print') {
56 $out .= &tab . 'print("$name\n")';
58 elsif ($_ eq 'print0') {
59 $out .= &tab . 'print("$name\0")';
61 elsif ($_ eq 'fstype') {
65 { $out .= '$dev < 0'; }
67 { $out .= '$dev >= 0'; }
69 elsif ($_ eq 'user') {
71 $out .= &tab . "\$uid == \$uid{'$uname'}";
74 elsif ($_ eq 'group') {
76 $out .= &tab . "\$gid == \$gid{'$gname'}";
79 elsif ($_ eq 'nouser') {
80 $out .= &tab . '!defined $uid{$uid}';
83 elsif ($_ eq 'nogroup') {
84 $out .= &tab . '!defined $gid{$gid}';
87 elsif ($_ eq 'links') {
88 $out .= &tab . '$nlink ' . &n(shift);
90 elsif ($_ eq 'inum') {
91 $out .= &tab . '$ino ' . &n(shift);
93 elsif ($_ eq 'size') {
94 $out .= &tab . 'int((-s _ + 511) / 512) ' . &n(shift);
96 elsif ($_ eq 'atime') {
97 $out .= &tab . 'int(-A _) ' . &n(shift);
99 elsif ($_ eq 'mtime') {
100 $out .= &tab . 'int(-M _) ' . &n(shift);
102 elsif ($_ eq 'ctime') {
103 $out .= &tab . 'int(-C _) ' . &n(shift);
105 elsif ($_ eq 'exec') {
106 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
109 if (m#^(/bin/)?rm -f {}$#) {
111 $out .= &tab . 'unlink($_)';
114 $out .= &tab . '(unlink($_) || 1)';
117 elsif (m#^(/bin/)?rm {}$#) {
118 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
121 for (@cmd) { s/'/\\'/g; }
123 $out .= &tab . "&exec(0, '@cmd')";
129 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
131 for (@cmd) { s/'/\\'/g; }
133 $out .= &tab . "&exec(1, '@cmd')";
137 elsif ($_ eq 'prune') {
138 $out .= &tab . '($prune = 1)';
140 elsif ($_ eq 'xdev') {
141 $out .= &tab . '(($prune |= ($dev != $topdev)),1)';
143 elsif ($_ eq 'newer') {
146 $newername = 'AGE_OF' . $file;
147 $newername =~ s/[^\w]/_/g;
148 $newername = '$' . $newername;
149 $out .= "-M _ < $newername";
150 $initnewer .= "$newername = -M " . "e($file) . ";\n";
152 elsif ($_ eq 'eval') {
153 $prog = "e(shift);
154 $out .= &tab . "eval $prog";
156 elsif ($_ eq 'depth') {
161 $out .= &tab . "&ls";
164 elsif ($_ eq 'tar') {
166 die "-tar must have a filename argument\n" unless @ARGV;
172 $initfile .= "open($fh, " . "e($file) .
173 qq{) || die "Can't open $fh: \$!\\n";\n};
175 $flushall = "\n&tflushall;\n";
180 die "-$_ must have a filename argument\n" unless @ARGV;
184 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
186 $initfile .= "open($fh, " . "e($file) .
187 qq{) || die "Can't open $fh: \$!\\n";\n};
189 $flushall = "\n&flushall;\n";
192 die "Unrecognized switch: -$_\n";
195 if ($ARGV[0] eq '-o') {
196 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
197 $statdone = 0 if $indent == 1 && $delayedstat;
202 $out .= " &&" unless $ARGV[0] eq ')';
204 shift if $ARGV[0] eq '-a';
212 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
213 if \$running_under_some_shell;
219 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
220 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
225 if ($inituser || $initls) {
226 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
227 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
228 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
232 if ($initgroup || $initls) {
233 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
234 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
235 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
239 print $initnewer, "\n" if $initnewer;
241 print $initfile, "\n" if $initfile;
243 $find = $depth ? "finddepth" : "find";
247 # Traverse desired filesystems
262 local($ok, @cmd) = @_;
263 foreach $word (@cmd) {
264 $word =~ s#{}#$name#g;
267 local($old) = select(STDOUT);
271 return 0 unless <STDIN> =~ /^y/;
285 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
286 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
290 if (defined $blocks) {
291 $blocks = int(($blocks + 1) / 2);
294 $blocks = int(($size + 1023) / 1024);
297 if (-f _) { $perms = '-'; }
298 elsif (-d _) { $perms = 'd'; }
299 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
300 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
301 elsif (-p _) { $perms = 'p'; }
302 elsif (-S _) { $perms = 's'; }
303 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
306 $tmp = $rwx[$tmpmode & 7];
308 $tmp = $rwx[$tmpmode & 7] . $tmp;
310 $tmp = $rwx[$tmpmode & 7] . $tmp;
311 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
312 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
313 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
316 $user = $user{$uid} || $uid;
317 $group = $group{$gid} || $gid;
319 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
320 $moname = $moname[$mon];
321 if (-M _ > 365.25 / 2) {
322 $timeyear = '19' . $year;
325 $timeyear = sprintf("%02d:%02d", $hour, $min);
328 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
344 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
356 if ($name eq 'TRAILER!!!') {
361 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
362 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
364 open(IN, "./$_\0") || do {
365 warn "Couldn't open $name: $!\n";
370 $text = readlink($_);
371 $size = 0 unless defined $text;
375 ($nm = $name) =~ s#^\./##;
379 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
394 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
395 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
396 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
397 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
400 $cpout{$fh} .= $text;
403 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
404 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
406 $l = length($cpout{$fh});
415 while (length($cpout{$fh}) >= 5120) {
416 syswrite($fh,$cpout{$fh},5120);
418 substr($cpout{$fh}, 0, 5120) = '';
423 $name = 'TRAILER!!!';
424 foreach $fh (keys %cpout) {
426 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
428 print $blocks{$fh} * 10, " blocks\n";
439 local($linkname,$header,$l,$slop);
440 local($linkflag) = "\0";
442 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
443 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
446 if ($linkname = $linkseen{$fh,$dev,$ino}) {
450 $linkseen{$fh,$dev,$ino} = $nm;
454 open(IN, "./$_\0") || do {
455 warn "Couldn't open $name: $!\n";
458 $size = 0 if $linkflag ne "\0";
461 $linkname = readlink($_);
462 $linkflag = 2 if defined $linkname;
467 $header = pack("a100a8a8a8a12a12a8a1a100",
469 sprintf("%6o ", $mode & 0777),
470 sprintf("%6o ", $uid & 0777777),
471 sprintf("%6o ", $gid & 0777777),
472 sprintf("%11o ", $size),
473 sprintf("%11o ", $mtime),
477 $l = length($header) % 512;
478 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
479 substr($header, 154, 1) = "\0"; # blech
480 $tarout{$fh} .= $header;
481 $tarout{$fh} .= "\0" x (512 - $l) if $l;
483 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
484 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
485 $slop = length($tarout{$fh}) % 512;
486 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
488 $l = length($tarout{$fh});
497 while (length($tarout{$fh}) >= 10240) {
498 syswrite($fh,$tarout{$fh},10240);
500 substr($tarout{$fh}, 0, 10240) = '';
507 foreach $fh (keys %tarout) {
508 $len = 10240 - length($tarout{$fh});
509 $len += 10240 if $len < 1024;
510 $tarout{$fh} .= "\0" x $len;
520 ############################################################################
525 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
527 if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
532 $tabstring .= <<'ENDOFSTAT' . $tabstring;
533 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
537 $tabstring .= <<'ENDOFSTAT' . $tabstring;
538 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
544 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
551 $tmp =~ s/([.^\$()])/\\$1/g;
552 $tmp =~ s/([?*])/.$1/g;
559 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
560 $n =~ s/ 0*(\d)/ $1/;