3 if test -f config.sh; then TOP=.;
4 elif test -f ../config.sh; then TOP=..;
5 elif test -f ../../config.sh; then TOP=../..;
6 elif test -f ../../../config.sh; then TOP=../../..;
7 elif test -f ../../../../config.sh; then TOP=../../../..;
9 echo "Can't find config.sh."; exit 1
14 : This forces SH files to create target in same directory as SH file.
15 : This is so that make depend always knows where to find SH derivatives.
17 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
19 echo "Extracting x2p/find2perl (with variable substitutions)"
20 : This section of the file will have variable substitutions done on it.
21 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
22 : Protect any dollar signs and backticks that you do not want interpreted
23 : by putting a backslash in front. You may delete these comments.
25 $spitshell >find2perl <<!GROK!THIS!
28 # Modified September 26, 1993 to provide proper handling of years after 1999
29 # Tom Link <tml+@pitt.edu>
30 # University of Pittsburgh
32 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
33 if \$running_under_some_shell;
39 : In the following dollars and backticks do not need the extra backslash.
40 $spitshell >>find2perl <<'!NO!SUBS!'
42 while ($ARGV[0] =~ /^[^-!(]/) {
45 @roots = ('.') unless @roots;
46 for (@roots) { $_ = "e($_); }
47 $roots = join(',', @roots);
53 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
67 elsif ($_ eq 'name') {
69 $pat = &fileglob_to_re(shift);
70 $out .= '/' . $pat . "/";
72 elsif ($_ eq 'perm') {
74 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
75 if ($onum =~ s/^-//) {
76 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
77 $out .= &tab . "((\$mode & $onum) == $onum)";
80 $onum = '0' . $onum unless $onum =~ /^0/;
81 $out .= &tab . "((\$mode & 0777) == $onum)";
84 elsif ($_ eq 'type') {
85 ($filetest = shift) =~ tr/s/S/;
86 $out .= &tab . "-$filetest _";
88 elsif ($_ eq 'print') {
89 $out .= &tab . 'print("$name\n")';
91 elsif ($_ eq 'print0') {
92 $out .= &tab . 'print("$name\0")';
94 elsif ($_ eq 'fstype') {
98 { $out .= '($dev < 0)'; }
100 { $out .= '($dev >= 0)'; }
102 elsif ($_ eq 'user') {
104 $out .= &tab . "(\$uid == \$uid{'$uname'})";
107 elsif ($_ eq 'group') {
109 $out .= &tab . "(\$gid == \$gid{'$gname'})";
112 elsif ($_ eq 'nouser') {
113 $out .= &tab . '!defined $uid{$uid}';
116 elsif ($_ eq 'nogroup') {
117 $out .= &tab . '!defined $gid{$gid}';
120 elsif ($_ eq 'links') {
121 $out .= &tab . '($nlink ' . &n(shift);
123 elsif ($_ eq 'inum') {
124 $out .= &tab . '($ino ' . &n(shift);
126 elsif ($_ eq 'size') {
127 $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
129 elsif ($_ eq 'atime') {
130 $out .= &tab . '(int(-A _) ' . &n(shift);
132 elsif ($_ eq 'mtime') {
133 $out .= &tab . '(int(-M _) ' . &n(shift);
135 elsif ($_ eq 'ctime') {
136 $out .= &tab . '(int(-C _) ' . &n(shift);
138 elsif ($_ eq 'exec') {
139 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
142 if (m#^(/bin/)?rm -f {}$#) {
144 $out .= &tab . 'unlink($_)';
147 $out .= &tab . '(unlink($_) || 1)';
150 elsif (m#^(/bin/)?rm {}$#) {
151 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
154 for (@cmd) { s/'/\\'/g; }
156 $out .= &tab . "&exec(0, '@cmd')";
162 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
164 for (@cmd) { s/'/\\'/g; }
166 $out .= &tab . "&exec(1, '@cmd')";
170 elsif ($_ eq 'prune') {
171 $out .= &tab . '($prune = 1)';
173 elsif ($_ eq 'xdev') {
174 $out .= &tab . '!($prune |= ($dev != $topdev))';
176 elsif ($_ eq 'newer') {
179 $newername = 'AGE_OF' . $file;
180 $newername =~ s/[^\w]/_/g;
181 $newername = '$' . $newername;
182 $out .= "(-M _ < $newername)";
183 $initnewer .= "$newername = -M " . "e($file) . ";\n";
185 elsif ($_ eq 'eval') {
186 $prog = "e(shift);
187 $out .= &tab . "eval $prog";
189 elsif ($_ eq 'depth') {
194 $out .= &tab . "&ls";
197 elsif ($_ eq 'tar') {
199 die "-tar must have a filename argument\n" unless @ARGV;
205 $initfile .= "open($fh, " . "e($file) .
206 qq{) || die "Can't open $fh: \$!\\n";\n};
208 $flushall = "\n&tflushall;\n";
213 die "-$_ must have a filename argument\n" unless @ARGV;
217 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
219 $initfile .= "open($fh, " . "e($file) .
220 qq{) || die "Can't open $fh: \$!\\n";\n};
222 $flushall = "\n&flushall;\n";
225 die "Unrecognized switch: -$_\n";
228 if ($ARGV[0] eq '-o') {
229 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
230 $statdone = 0 if $indent == 1 && $delayedstat;
235 $out .= " &&" unless $ARGV[0] eq ')';
237 shift if $ARGV[0] eq '-a';
245 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
246 if \$running_under_some_shell;
252 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
253 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
258 if ($inituser || $initls) {
259 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
260 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
261 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
265 if ($initgroup || $initls) {
266 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
267 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
268 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
272 print $initnewer, "\n" if $initnewer;
274 print $initfile, "\n" if $initfile;
276 $find = $depth ? "finddepth" : "find";
280 # Traverse desired filesystems
295 local($ok, @cmd) = @_;
296 foreach $word (@cmd) {
297 $word =~ s#{}#$name#g;
300 local($old) = select(STDOUT);
304 return 0 unless <STDIN> =~ /^y/;
318 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
319 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
323 if (defined $blocks) {
324 $blocks = int(($blocks + 1) / 2);
327 $blocks = int(($size + 1023) / 1024);
330 if (-f _) { $perms = '-'; }
331 elsif (-d _) { $perms = 'd'; }
332 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
333 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
334 elsif (-p _) { $perms = 'p'; }
335 elsif (-S _) { $perms = 's'; }
336 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
339 $tmp = $rwx[$tmpmode & 7];
341 $tmp = $rwx[$tmpmode & 7] . $tmp;
343 $tmp = $rwx[$tmpmode & 7] . $tmp;
344 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
345 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
346 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
349 $user = $user{$uid} || $uid;
350 $group = $group{$gid} || $gid;
352 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
353 $moname = $moname[$mon];
354 if (-M _ > 365.25 / 2) {
355 $timeyear = $year + 1900;
358 $timeyear = sprintf("%02d:%02d", $hour, $min);
361 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
377 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
389 if ($name eq 'TRAILER!!!') {
394 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
395 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
397 open(IN, "./$_\0") || do {
398 warn "Couldn't open $name: $!\n";
403 $text = readlink($_);
404 $size = 0 unless defined $text;
408 ($nm = $name) =~ s#^\./##;
412 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
427 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
428 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
429 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
430 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
433 $cpout{$fh} .= $text;
436 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
437 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
439 $l = length($cpout{$fh});
448 while (length($cpout{$fh}) >= 5120) {
449 syswrite($fh,$cpout{$fh},5120);
451 substr($cpout{$fh}, 0, 5120) = '';
456 $name = 'TRAILER!!!';
457 foreach $fh (keys %cpout) {
459 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
461 print $blocks{$fh} * 10, " blocks\n";
472 local($linkname,$header,$l,$slop);
473 local($linkflag) = "\0";
475 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
476 $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
479 if ($linkname = $linkseen{$fh,$dev,$ino}) {
483 $linkseen{$fh,$dev,$ino} = $nm;
487 open(IN, "./$_\0") || do {
488 warn "Couldn't open $name: $!\n";
491 $size = 0 if $linkflag ne "\0";
494 $linkname = readlink($_);
495 $linkflag = 2 if defined $linkname;
500 $header = pack("a100a8a8a8a12a12a8a1a100",
502 sprintf("%6o ", $mode & 0777),
503 sprintf("%6o ", $uid & 0777777),
504 sprintf("%6o ", $gid & 0777777),
505 sprintf("%11o ", $size),
506 sprintf("%11o ", $mtime),
510 $l = length($header) % 512;
511 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
512 substr($header, 154, 1) = "\0"; # blech
513 $tarout{$fh} .= $header;
514 $tarout{$fh} .= "\0" x (512 - $l) if $l;
516 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
517 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
518 $slop = length($tarout{$fh}) % 512;
519 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
521 $l = length($tarout{$fh});
530 while (length($tarout{$fh}) >= 10240) {
531 syswrite($fh,$tarout{$fh},10240);
533 substr($tarout{$fh}, 0, 10240) = '';
540 foreach $fh (keys %tarout) {
541 $len = 10240 - length($tarout{$fh});
542 $len += 10240 if $len < 1024;
543 $tarout{$fh} .= "\0" x $len;
553 ############################################################################
558 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
560 if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
565 $tabstring .= <<'ENDOFSTAT' . $tabstring;
566 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
570 $tabstring .= <<'ENDOFSTAT' . $tabstring;
571 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
577 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
584 $tmp =~ s#([./^\$()])#\\$1#g;
585 $tmp =~ s/([?*])/.$1/g;
592 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
593 $n =~ s/ 0*(\d)/ $1/;