home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / find2perl.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  13KB  |  571 lines

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