Abid Khwaja wrote:
> I've been trying to figure out how to use File::stat to check file
> modes but haven't had much luck understanding how it works from the
> documentation. My goal is to check if a file is owned by a specific
> user, group owned by a specific group and has mode 660. I have the
> uid and gid checks down but need help with with the mode check.
The documentation for the stat function explains how to do that:
perldoc -f stat
[snip]
You can import symbolic mode constants ("S_IF*") and functions
("S_IS*") from the Fcntl module:
use Fcntl ’:mode’;
$mode = (stat($filename))[2];
$user_rwx = ($mode & S_IRWXU) >> 6;
$group_read = ($mode & S_IRGRP) >> 3;
$other_execute = $mode & S_IXOTH;
printf "Permissions are %04o\n", S_IMODE($mode), "\n";
$is_setuid = $mode & S_ISUID;
$is_setgid = S_ISDIR($mode);
Also the STAT(2) man page may help.
> So I'm doing the following test:
>
> use File::stat;
> my $conffile = "/etc/slist.conf";
> my $perms = stat($conffile)
> || die "can't find $conffile: $!\n";
> my $mode = $perms->mode;
> print "$mode\n";
>
> against the following file:
>
> ---------- 1 joe uucp 311 Nov 14 15:20 slist.conf
>
> When I run the code, here's what I get:
>
> 32768
>
> The output varies as I change the file mode but I don't see the relation
> between the code output and the mode. If someone can explain to me how
> this works, it would be greatly appreciated. I'm running this on a MacOS
> X box but the code needs to run cross-unix-platform.
That is because most discussions about the mode assume an octal representation
where the three least significant bits are the world permissions and the next
three bits are the group permissions and the next three bits are the owner
permissions. Of those three bits the least significant bit is execute
permission and the next bit is write permission and the next bit is read
permission. For example:
$ touch TEST
$ chmod 0752 TEST
^^^
ogw
$ ls -l TEST
-rwxr-x-w- 1 john users 0 2005-11-24 18:20 TEST
^^^^^^^^^
ooogggwww
$ perl -le'
use File::stat;
my $st = stat "TEST" or die "stat: $!";
printf "%o\n", $st->[2];
'
100752
^^^
ogw
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>