470,619 Members | 1,841 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Q: Is file readable by all users?

Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers
Jul 19 '05 #1
4 3240

"Troll" <ab***@microsoft.com> wrote in message
news:MD********************@news-server.bigpond.net.au...
Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers

I had a look at stat and can read the permissions. But what I need to do
then is change the file permissions ONLY if the file is not readable by all
users.
Should I be trying to capture the output of:

$mode = (stat "test.txt") [2];
printf "%0\n", $mode & 07777; # this gives me the original permissions

and then sed-ing it so as to read the last character? If the last char > 3
then the file is readable by all users.
How can I assign the above printf output to a scalar?

Any pointers welcome :)

Jul 19 '05 #2
*snip*
I had a look at stat and can read the permissions. But what I need to do
then is change the file permissions ONLY if the file is not readable by all users.
Should I be trying to capture the output of:

$mode = (stat "test.txt") [2];
printf "%0\n", $mode & 07777; # this gives me the original permissions

and then sed-ing it so as to read the last character? If the last char > 3
then the file is readable by all users.
How can I assign the above printf output to a scalar?

Any pointers welcome :)


Ok, got it.
$permissions = sprintf "%0\n", $mode & 07777;

Silly Q: How do I check if the last digit is > 3? My mind is blank atm...

Jul 19 '05 #3
*snip*
then the file is readable by all users.
How can I assign the above printf output to a scalar?

Any pointers welcome :)


Ok, got it.
$permissions = sprintf "%0\n", $mode & 07777;

Silly Q: How do I check if the last digit is > 3? My mind is blank atm...

Is there something simpler than:
if ($permissions =~ m/^..[4-7]/) {

Thanks.


Jul 19 '05 #4
"Troll" <ab***@microsoft.com> wrote in message news:<MD********************@news-server.bigpond.net.au>...
Hi,

Would something like:

if (-r $name) {
: # do nothing
} else {
print "File is not readable...\n";
}

be enough to test if a file is readable by all users?
I'm a little unsure which bit does the -r relate to.

What about if I wanted to test whether the file is readable by the owner's
group but not everyone else?

Cheers


The -r test only checks if the user running the program can read that
file.
To check if all users can read a file use the 'mode' returned by the
stat() function (3rd returned element).

use Fcntl ':mode';

$mode = (stat($filename))[2];
$other_read = $mode & S_IROTH;

Check 'perldoc perlfunc' for a full example on how to interpret
'mode'.

Hope that helps.

Irene
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Hal Vaughan | last post: by
13 posts views Thread by raykyoto | last post: by
9 posts views Thread by steph_de_marseille | last post: by
9 posts views Thread by JimmyKoolPantz | last post: by
77 posts views Thread by ume$h | last post: by
30 posts views Thread by Adam Baker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.