home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / find2perl.bat < prev    next >
Encoding:
DOS Batch File  |  1999-10-16  |  13.6 KB  |  603 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S "%0" %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. goto endofperl
  11. @rem ';
  12. #!perl
  13. #line 14
  14.     eval 'exec P:\Apps\ActivePerl\temp\bin\MSWin32-x86-object\perl.exe -S $0 ${1+"$@"}'
  15.       if $running_under_some_shell;
  16. $startperl = "#!perl";
  17. $perlpath = "P:\Apps\ActivePerl\temp\bin\MSWin32-x86-object\perl.exe";
  18.  
  19. # Modified September 26, 1993 to provide proper handling of years after 1999
  20. #   Tom Link <tml+@pitt.edu>
  21. #   University of Pittsburgh
  22. # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
  23. #  Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
  24. #  University of Adelaide, Adelaide, South Australia
  25.  
  26. while ($ARGV[0] =~ /^[^-!(]/) {
  27.     push(@roots, shift);
  28. }
  29. @roots = ('.') unless @roots;
  30. for (@roots) { $_ = "e($_); }
  31. $roots = join(',', @roots);
  32.  
  33. $indent = 1;
  34. $stat = 'lstat';
  35. $decl = '';
  36.  
  37. while (@ARGV) {
  38.     $_ = shift;
  39.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  40.     if ($_ eq '(') {
  41.     $out .= &tab . "(\n";
  42.     $indent++;
  43.     next;
  44.     }
  45.     elsif ($_ eq ')') {
  46.     $indent--;
  47.     $out .= &tab . ")";
  48.     }
  49.     elsif ($_ eq 'follow') {
  50.     $stat = 'stat';
  51.     $decl = '%already_seen = ();';
  52.     $out .= &tab . '(not $already_seen{"$dev,$ino"}) &&';
  53.     $out .= "\n" . &tab . '(($already_seen{"$dev,$ino"} = !(-d _)) || 1)';
  54.     }
  55.     elsif ($_ eq '!') {
  56.     $out .= &tab . "!";
  57.     next;
  58.     }
  59.     elsif ($_ eq 'name') {
  60.     $out .= &tab;
  61.     $pat = &fileglob_to_re(shift);
  62.     $out .= '/' . $pat . "/";
  63.     }
  64.     elsif ($_ eq 'perm') {
  65.     $onum = shift;
  66.     die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
  67.     if ($onum =~ s/^-//) {
  68.         $onum = '0' . sprintf("%o", oct($onum) & 017777);    # s/b 07777 ?
  69.         $out .= &tab . "((\$mode & $onum) == $onum)";
  70.     }
  71.     else {
  72.         $onum = '0' . $onum unless $onum =~ /^0/;
  73.         $out .= &tab . "((\$mode & 0777) == $onum)";
  74.     }
  75.     }
  76.     elsif ($_ eq 'type') {
  77.     ($filetest = shift) =~ tr/s/S/;
  78.     $out .= &tab . "-$filetest _";
  79.     }
  80.     elsif ($_ eq 'print') {
  81.     $out .= &tab . 'print("$name\n")';
  82.     }
  83.     elsif ($_ eq 'print0') {
  84.     $out .= &tab . 'print("$name\0")';
  85.     }
  86.     elsif ($_ eq 'fstype') {
  87.     $out .= &tab;
  88.     $type = shift;
  89.     if ($type eq 'nfs')
  90.         { $out .= '($dev < 0)'; }
  91.     else
  92.         { $out .= '($dev >= 0)'; }
  93.     }
  94.     elsif ($_ eq 'user') {
  95.     $uname = shift;
  96.     $out .= &tab . "(\$uid == \$uid{'$uname'})";
  97.     $inituser++;
  98.     }
  99.     elsif ($_ eq 'group') {
  100.     $gname = shift;
  101.     $out .= &tab . "(\$gid == \$gid{'$gname'})";
  102.     $initgroup++;
  103.     }
  104.     elsif ($_ eq 'nouser') {
  105.     $out .= &tab . '!defined $uid{$uid}';
  106.     $inituser++;
  107.     }
  108.     elsif ($_ eq 'nogroup') {
  109.     $out .= &tab . '!defined $gid{$gid}';
  110.     $initgroup++;
  111.     }
  112.     elsif ($_ eq 'links') {
  113.     $out .= &tab . '($nlink ' . &n(shift);
  114.     }
  115.     elsif ($_ eq 'inum') {
  116.     $out .= &tab . '($ino ' . &n(shift);
  117.     }
  118.     elsif ($_ eq 'size') {
  119.     $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
  120.     }
  121.     elsif ($_ eq 'atime') {
  122.     $out .= &tab . '(int(-A _) ' . &n(shift);
  123.     }
  124.     elsif ($_ eq 'mtime') {
  125.     $out .= &tab . '(int(-M _) ' . &n(shift);
  126.     }
  127.     elsif ($_ eq 'ctime') {
  128.     $out .= &tab . '(int(-C _) ' . &n(shift);
  129.     }
  130.     elsif ($_ eq 'exec') {
  131.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  132.     shift;
  133.     $_ = "@cmd";
  134.     if (m#^(/bin/)?rm -f {}$#) {
  135.         if (!@ARGV) {
  136.         $out .= &tab . 'unlink($_)';
  137.         }
  138.         else {
  139.         $out .= &tab . '(unlink($_) || 1)';
  140.         }
  141.     }
  142.     elsif (m#^(/bin/)?rm {}$#) {
  143.         $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
  144.     }
  145.     else {
  146.         for (@cmd) { s/'/\\'/g; }
  147.         $" = "','";
  148.         $out .= &tab . "&exec(0, '@cmd')";
  149.         $" = ' ';
  150.         $initexec++;
  151.     }
  152.     }
  153.     elsif ($_ eq 'ok') {
  154.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  155.     shift;
  156.     for (@cmd) { s/'/\\'/g; }
  157.     $" = "','";
  158.     $out .= &tab . "&exec(1, '@cmd')";
  159.     $" = ' ';
  160.     $initexec++;
  161.     }
  162.     elsif ($_ eq 'prune') {
  163.     $out .= &tab . '($prune = 1)';
  164.     }
  165.     elsif ($_ eq 'xdev') {
  166.     $out .= &tab . '!($prune |= ($dev != $topdev))';
  167.     }
  168.     elsif ($_ eq 'newer') {
  169.     $out .= &tab;
  170.     $file = shift;
  171.     $newername = 'AGE_OF' . $file;
  172.     $newername =~ s/[^\w]/_/g;
  173.     $newername = "\$$newername";
  174.     $out .= "(-M _ < $newername)";
  175.     $initnewer .= "$newername = -M " . "e($file) . ";\n";
  176.     }
  177.     elsif ($_ eq 'eval') {
  178.     $prog = "e(shift);
  179.     $out .= &tab . "eval $prog";
  180.     }
  181.     elsif ($_ eq 'depth') {
  182.     $depth++;
  183.     next;
  184.     }
  185.     elsif ($_ eq 'ls') {
  186.     $out .= &tab . "&ls";
  187.     $initls++;
  188.     }
  189.     elsif ($_ eq 'tar') {
  190.     $out .= &tab;
  191.     die "-tar must have a filename argument\n" unless @ARGV;
  192.     $file = shift;
  193.     $fh = 'FH' . $file;
  194.     $fh =~ s/[^\w]/_/g;
  195.     $out .= "&tar($fh)";
  196.     $file = '>' . $file;
  197.     $initfile .= "open($fh, " . "e($file) .
  198.       qq{) || die "Can't open $fh: \$!\\n";\n};
  199.     $inittar++;
  200.     $flushall = "\n&tflushall;\n";
  201.     }
  202.     elsif (/^n?cpio$/) {
  203.     $depth++;
  204.     $out .= &tab;
  205.     die "-$_ must have a filename argument\n" unless @ARGV;
  206.     $file = shift;
  207.     $fh = 'FH' . $file;
  208.     $fh =~ s/[^\w]/_/g;
  209.     $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
  210.     $file = '>' . $file;
  211.     $initfile .= "open($fh, " . "e($file) .
  212.       qq{) || die "Can't open $fh: \$!\\n";\n};
  213.     $initcpio++;
  214.     $flushall = "\n&flushall;\n";
  215.     }
  216.     else {
  217.     die "Unrecognized switch: -$_\n";
  218.     }
  219.     if (@ARGV) {
  220.     if ($ARGV[0] eq '-o') {
  221.         { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
  222.         $statdone = 0 if $indent == 1 && $delayedstat;
  223.         $saw_or++;
  224.         shift;
  225.     }
  226.     else {
  227.         $out .= " &&" unless $ARGV[0] eq ')';
  228.         $out .= "\n";
  229.         shift if $ARGV[0] eq '-a';
  230.     }
  231.     }
  232. }
  233.  
  234. print <<"END";
  235. $startperl
  236.     eval 'exec $perlpath -S \$0 \${1+"\$@"}'
  237.     if \$running_under_some_shell;
  238.  
  239. END
  240.  
  241. if ($initls) {
  242.     print <<'END';
  243. @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  244. @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  245.  
  246. END
  247. }
  248.  
  249. if ($inituser || $initls) {
  250.     print 'while (($name, $pw, $uid) = getpwent) {', "\n";
  251.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
  252.     print '    $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
  253.     print "}\n\n";
  254. }
  255.  
  256. if ($initgroup || $initls) {
  257.     print 'while (($name, $pw, $gid) = getgrent) {', "\n";
  258.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
  259.     print '    $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
  260.     print "}\n\n";
  261. }
  262.  
  263. print $initnewer, "\n" if $initnewer;
  264.  
  265. print $initfile, "\n" if $initfile;
  266.  
  267. $find = $depth ? "finddepth" : "find";
  268. print <<"END";
  269. require "$find.pl";
  270.  
  271. # Traverse desired filesystems
  272.  
  273. $decl
  274. &$find($roots);
  275. $flushall
  276. exit;
  277. sub wanted {
  278. $out;
  279. }
  280.  
  281. END
  282.  
  283. if ($initexec) {
  284.     print <<'END';
  285. sub exec {
  286.     local($ok, @cmd) = @_;
  287.     foreach $word (@cmd) {
  288.     $word =~ s#{}#$name#g;
  289.     }
  290.     if ($ok) {
  291.     local($old) = select(STDOUT);
  292.     $| = 1;
  293.     print "@cmd";
  294.     select($old);
  295.     return 0 unless <STDIN> =~ /^y/;
  296.     }
  297.     chdir $cwd;        # sigh
  298.     system @cmd;
  299.     chdir $dir;
  300.     return !$?;
  301. }
  302.  
  303. END
  304. }
  305.  
  306. if ($initls) {
  307.     print <<"INTERP", <<'END';
  308. sub ls {
  309.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$sizemm,
  310.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  311. INTERP
  312.  
  313.     $pname = $name;
  314.  
  315.     if (defined $blocks) {
  316.     $blocks = int(($blocks + 1) / 2);
  317.     }
  318.     else {
  319.     $blocks = int(($size + 1023) / 1024);
  320.     }
  321.  
  322.     if    (-f _) { $perms = '-'; }
  323.     elsif (-d _) { $perms = 'd'; }
  324.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  325.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  326.     elsif (-p _) { $perms = 'p'; }
  327.     elsif (-S _) { $perms = 's'; }
  328.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  329.  
  330.     $tmpmode = $mode;
  331.     $tmp = $rwx[$tmpmode & 7];
  332.     $tmpmode >>= 3;
  333.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  334.     $tmpmode >>= 3;
  335.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  336.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  337.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  338.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  339.     $perms .= $tmp;
  340.  
  341.     $user = $user{$uid} || $uid;
  342.     $group = $group{$gid} || $gid;
  343.  
  344.     ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  345.     $moname = $moname[$mon];
  346.     if (-M _ > 365.25 / 2) {
  347.     $timeyear = $year + 1900;
  348.     }
  349.     else {
  350.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  351.     }
  352.  
  353.     printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  354.         $ino,
  355.          $blocks,
  356.               $perms,
  357.                 $nlink,
  358.                 $user,
  359.                      $group,
  360.                       $sizemm,
  361.                           $moname,
  362.                          $mday,
  363.                              $timeyear,
  364.                              $pname;
  365.     1;
  366. }
  367.  
  368. sub sizemm {
  369.     sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
  370. }
  371.  
  372. END
  373. }
  374.  
  375. if ($initcpio) {
  376. print <<'START', <<"INTERP", <<'END';
  377. sub cpio {
  378.     local($nc,$fh) = @_;
  379.     local($text);
  380.  
  381.     if ($name eq 'TRAILER!!!') {
  382.     $text = '';
  383.     $size = 0;
  384.     }
  385.     else {
  386. START
  387.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  388.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  389. INTERP
  390.     if (-f _) {
  391.         open(IN, "./$_\0") || do {
  392.         warn "Couldn't open $name: $!\n";
  393.         return;
  394.         };
  395.     }
  396.     else {
  397.         $text = readlink($_);
  398.         $size = 0 unless defined $text;
  399.     }
  400.     }
  401.  
  402.     ($nm = $name) =~ s#^\./##;
  403.     $nc{$fh} = $nc;
  404.     if ($nc eq 'n') {
  405.     $cpout{$fh} .=
  406.       sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  407.         070707,
  408.         $dev & 0777777,
  409.         $ino & 0777777,
  410.         $mode & 0777777,
  411.         $uid & 0777777,
  412.         $gid & 0777777,
  413.         $nlink & 0777777,
  414.         $rdev & 0177777,
  415.         $mtime,
  416.         length($nm)+1,
  417.         $size,
  418.         $nm);
  419.     }
  420.     else {
  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"));
  425.     }
  426.     if ($text ne '') {
  427.     $cpout{$fh} .= $text;
  428.     }
  429.     elsif ($size) {
  430.     &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
  431.     while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  432.         &flush($fh);
  433.         $l = length($cpout{$fh});
  434.     }
  435.     }
  436.     close IN;
  437. }
  438.  
  439. sub flush {
  440.     local($fh) = @_;
  441.  
  442.     while (length($cpout{$fh}) >= 5120) {
  443.     syswrite($fh,$cpout{$fh},5120);
  444.     ++$blocks{$fh};
  445.     substr($cpout{$fh}, 0, 5120) = '';
  446.     }
  447. }
  448.  
  449. sub flushall {
  450.     $name = 'TRAILER!!!';
  451.     foreach $fh (keys %cpout) {
  452.     &cpio($nc{$fh},$fh);
  453.     $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  454.     &flush($fh);
  455.     print $blocks{$fh} * 10, " blocks\n";
  456.     }
  457. }
  458.  
  459. END
  460. }
  461.  
  462. if ($inittar) {
  463. print <<'START', <<"INTERP", <<'END';
  464. sub tar {
  465.     local($fh) = @_;
  466.     local($linkname,$header,$l,$slop);
  467.     local($linkflag) = "\0";
  468.  
  469. START
  470.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  471.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  472. INTERP
  473.     $nm = $name;
  474.     if ($nlink > 1) {
  475.     if ($linkname = $linkseen{$fh,$dev,$ino}) {
  476.         $linkflag = 1;
  477.     }
  478.     else {
  479.         $linkseen{$fh,$dev,$ino} = $nm;
  480.     }
  481.     }
  482.     if (-f _) {
  483.     open(IN, "./$_\0") || do {
  484.         warn "Couldn't open $name: $!\n";
  485.         return;
  486.     };
  487.     $size = 0 if $linkflag ne "\0";
  488.     }
  489.     else {
  490.     $linkname = readlink($_);
  491.     $linkflag = 2 if defined $linkname;
  492.     $nm .= '/' if -d _;
  493.     $size = 0;
  494.     }
  495.  
  496.     $header = pack("a100a8a8a8a12a12a8a1a100",
  497.     $nm,
  498.     sprintf("%6o ", $mode & 0777),
  499.     sprintf("%6o ", $uid & 0777777),
  500.     sprintf("%6o ", $gid & 0777777),
  501.     sprintf("%11o ", $size),
  502.     sprintf("%11o ", $mtime),
  503.     "        ",
  504.     $linkflag,
  505.     $linkname);
  506.     $l = length($header) % 512;
  507.     substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
  508.     substr($header, 154, 1) = "\0";  # blech
  509.     $tarout{$fh} .= $header;
  510.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  511.     if ($size) {
  512.     &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
  513.     while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  514.         $slop = length($tarout{$fh}) % 512;
  515.         $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  516.         &tflush($fh);
  517.         $l = length($tarout{$fh});
  518.     }
  519.     }
  520.     close IN;
  521. }
  522.  
  523. sub tflush {
  524.     local($fh) = @_;
  525.  
  526.     while (length($tarout{$fh}) >= 10240) {
  527.     syswrite($fh,$tarout{$fh},10240);
  528.     ++$blocks{$fh};
  529.     substr($tarout{$fh}, 0, 10240) = '';
  530.     }
  531. }
  532.  
  533. sub tflushall {
  534.     local($len);
  535.  
  536.     foreach $fh (keys %tarout) {
  537.     $len = 10240 - length($tarout{$fh});
  538.     $len += 10240 if $len < 1024;
  539.     $tarout{$fh} .= "\0" x $len;
  540.     &tflush($fh);
  541.     }
  542. }
  543.  
  544. END
  545. }
  546.  
  547. exit;
  548.  
  549. ############################################################################
  550.  
  551. sub tab {
  552.     local($tabstring);
  553.  
  554.     $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
  555.     if (!$statdone) {
  556.     if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
  557.         $delayedstat++;
  558.     }
  559.     else {
  560.         if ($saw_or) {
  561.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  562. (\$nlink || ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\))) &&
  563. ENDOFSTAT
  564.         }
  565.         else {
  566.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  567. ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\)) &&
  568. ENDOFSTAT
  569.         }
  570.         $statdone = 1;
  571.     }
  572.     }
  573.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  574.     $tabstring;
  575. }
  576.  
  577. sub fileglob_to_re {
  578.     local($tmp) = @_;
  579.  
  580.     $tmp =~ s#([./^\$()])#\\$1#g;
  581.     $tmp =~ s/([?*])/.$1/g;
  582.     "^$tmp\$";
  583. }
  584.  
  585. sub n {
  586.     local($n) = @_;
  587.  
  588.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  589.     $n =~ s/ 0*(\d)/ $1/;
  590.     $n . ')';
  591. }
  592.  
  593. sub quote {
  594.     local($string) = @_;
  595.     $string =~ s/'/\\'/;
  596.     "'$string'";
  597. }
  598. __END__
  599. :endofperl
  600.