473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3356

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Smithz | last post by:
Hi there, As a newbie to PHP is it in anyway possible for a casual web surfer to actually get to see the PHP behind my .php file. E.g. if index.php contained PHP code to check if the page...
6
by: Hal Vaughan | last post by:
I've been reading up on file permissions (including FilePermission, Permission, and Permissions). From what I see, these are temporary and forgotten when a program exits (if I'm wrong, tell me,...
13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
10
by: MLH | last post by:
I print to a device that creates a PDF. Knowing the filename, how can I then embed the PDF into the body text of an OutLook Express outbound email & send to a specified address in a table? I want...
2
by: meh | last post by:
I have a very huge database in MS access 2000. I want only few people to make changes to it and rest should be able to display only. Can someone please show me how to do it ? Thanks.
9
by: steph_de_marseille | last post by:
I would like to write a x array in a file. If the array has a small numbers of columns I know I can use a loop like: int i,j; double array; FILE *file1; file1=fopen("data.dat","w");...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
77
by: ume$h | last post by:
/* I wrote the following program to calculate no. of 'a' in the file c:/1.txt but it fails to give appropriate result. What is wrong with it? */ #include"stdio.h" int main(void) { FILE *f;...
30
by: Adam Baker | last post by:
Hello, I'm writing a site where a handful of people will be able to edit the content using PHP scripts (FCKeditor). The content is stored as individual files in a directory. I'd like to validate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.