I wrote a script which will scan local, mounted file systems for world-writable files excluding any which are in a hash generated from a list in a file. It utilizes Fcntl in order to make use of the file mode constants.

The current version runs stat three times against a file:

return unless (((stat)[2] & S_IWUSR) && ((stat)[2] & S_IWGRP) && ((s +tat)[2] & S_IWOTH));

This is, of course, less than ideal since it is running one operation three times against every file. That said, it works and the output contains only the files I'm looking for minus those I've explicitly excluded.

It was suggested that I instead set a variable which contains these constants and run a bitwise AND with the $mode field from stat(). I tried this. The result was that the script returned every file, even those not world-writable.

Additionally, I get a "Possible precedence problem" error. I'm inexperienced with bitwise operations so I don't know what I'm encountering with that particular error.

# Some code my $mask = S_IWUSR | S_IWGRP | S_IWOTH; # Some more code return unless $dirStats[2] & $mask == $mask; # Even more code
Possible precedence problem on bitwise & operator at ./ww_files-v4-2 +.pl line 106.

Undestandably, the context is extremely vague here. I'm not sure if etiquette allows for the posting of entire scripts so I've pastebin'ed it: http://pastebin.com/A8d3K2mR

EDIT: All answers pointed to using parens around the bitwise AND before comparing to $mask. This solved both the "Possible precedence" error as well as the results being unexpected.


In reply to bitwise AND against a variable containing Fnctl constants not returning desired results by theillien1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.