home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / perl / chk_strings.pl < prev    next >
Perl Script  |  1992-03-10  |  4KB  |  139 lines

  1. #
  2. #  This is a big one.  Support routines to check for strings 
  3. # that look like pathnames and make sure they're not writable.
  4. # Will recurse if $recurse is set.  the shell version can't do
  5. # this (yet).  call &ignore with list of regexps that you don't
  6. # care about.  (or set @ignores)
  7. #
  8. # originally by Tom Christiansen <tchrist@convex.com>
  9. # since hacked on by parties various and sundry.
  10.  
  11. require 'is_able.pl';
  12. require 'file_mode.pl';
  13. require 'pathconf.pl';
  14.  
  15. package chk_strings;
  16.  
  17.  
  18. $'STRINGS = $'STRINGS || '/usr/ucb/strings';
  19.  
  20. for ( '/dev/null', '/dev/tty' ) {
  21.     $seen{$_}++;
  22.  
  23. sub main'chk_strings {
  24.     local($ARGV) = @_;
  25.     local($_);
  26.     local($word);
  27.     local(*STRINGS);  # XXX: might run out of fd's on deep recursion!  -tchrist
  28.     local(%paths, $text); 
  29.     local($STRINGS) = "$'STRINGS $ARGV |";
  30.  
  31.     &ignore(@ignores) if defined @ignores && !$already_ignored;
  32.  
  33.     $STRINGS="< $ARGV", $text=1 if -T $ARGV;
  34.     print "Opening via: $STRINGS\n" if $debug;
  35.  
  36.     open (STRINGS, $STRINGS); 
  37.     while (<STRINGS>) { 
  38.     next unless m#/#;   # was m#/.*/#;
  39. #---------------------------------------------------------------------------
  40. # Comments and modifications by Martin Foord (maf%dbsm.oz.au@munnari.oz.au).
  41.     #s/#.*$// if $text;  # strip out comments if -T file
  42.     # Comments start in the shell at the beginning of a word or at the
  43.     # beggining of a line
  44.     if ($text) {
  45.         s/\s+#.*$//;
  46.         s/^#.*$//;
  47.     }
  48.  
  49.     # Get rid of semicolons, they can hang around on filenames ...
  50.     s/;//g;
  51. #---------------------------------------------------------------------------
  52.  
  53.     s/"([^"]*)"/ $1 /g;
  54.     s/'([^']*)'/ $1 /g;
  55.     # See my comments below on how to deal with this stuff ... (line 64).
  56.     #s/`([^`]*)`/ $1 /g;
  57.  
  58.  
  59.     s!([<>])\s+/!$1/!g;  # "> /foo" goes to ">/foo";
  60.  
  61.     s/=/ /g;  # warning -- mangled files with = in them
  62.     for $word (split) {
  63.         if ($word =~ m#:/#) {
  64.         print "push $word (split on the colons)\n" if $debug;
  65.         @paths{split(/:/, $word)} = ();
  66.         } elsif ($word =~ m#^[<>]?/#) {
  67.         print "push $word\n" if $debug;
  68.         $paths{$word}++;
  69.         }
  70.     }
  71.     }
  72.     close (STRINGS);
  73.     push(@files, $ARGV);
  74.  
  75.     for (keys %paths) {
  76.     s/\)$//;
  77.     s/^\(//;
  78.     s#^/+#/#;
  79.     s#^(/.*)/$#$1#;        # get rid of trailing slash
  80.  
  81. #---------------------------------------------------------------------------
  82. # Comments and modifications by Martin Foord (maf%dbsm.oz.au@munnari.oz.au).
  83.     # It's best to evaluate what's in backquotes rather than remove them
  84.     # as in the substitution above, due to files which
  85.     # look like this /var/yp/`domainname` (eg in my /etc/rc.local).
  86.     s`\`(.+)\``$1`; # eval what's in backquotes.
  87.     chop if /\n$/;    # fang off \n if there ...
  88. #---------------------------------------------------------------------------
  89.     next if &ignored($_);
  90.     s/^[<>]//;
  91.     next if $_ eq '';
  92.     next unless !$seen{$_}++ && -e && !-S _;
  93.     print "checking $_\n" if $debug;
  94.     if ($how = &'is_writable($_)) {
  95.         print "Warning!  File $_ (inside ",
  96.             join(' inside ', reverse @files), ") is _World_ $how!\n";
  97.     } elsif ($recurse && (&'Mode($_) & 0111) && -f _) {
  98.          print "recursing $_\n" if $debug;
  99.          &'chk_strings($_);   
  100.     } 
  101.     }
  102.      pop(@files);
  103.  
  104. sub ignore {
  105.     local($_);
  106.     local($prog);
  107.  
  108.     $already_ignored = 1;
  109.  
  110.     $prog = <<'EOCODE';
  111.  
  112. sub ignored {
  113.     local($return) = 1;
  114.     local($prog);
  115.     local($_) = @_;
  116.     {
  117. EOCODE
  118.     for (@_) {
  119.     $prog .= "\tlast if m\201${_}\201;\n";
  120.     } 
  121.     $prog .= <<'EOCODE';
  122.     $return = 0;
  123.     }
  124.     print "$_ IGNORED\n" if $debug && $return;
  125.     $return;
  126. }
  127. EOCODE
  128.     
  129.     print $prog if $debug;
  130.     eval $prog;
  131.     die $@ if $@;
  132.  
  133. sub ignored {}; # in case they never ignore anything
  134.  
  135. 1;
  136.