For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > getting file permission









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getting file permission
Canned

2007-12-08, 7:00 pm

Hi,
I'm a perl noob and I try to get a list of file inside a directory along
with their permission (like ls -l). So far I can only get the filenames.
Anyone knows how can I get the file permissions?

Thanks in advance,
C
Canned

2007-12-09, 8:00 am

Canned schreef:
> Hi,
> I'm a perl noob and I try to get a list of file inside a directory along
> with their permission (like ls -l). So far I can only get the filenames.
> Anyone knows how can I get the file permissions?
>
> Thanks in advance,
> C


I found out that by using stat(), I could get the file permission but
there still are some problem with that. The problem is that I can only
get the real permission if they formatted with "%04o", but I want to get
the real permission assigned to a variable. Anybody know how to do it?

use strict;
use warnings;
use Fcntl ':mode';

my $dir = $ARGV[0];
my $filetype = $ARGV[1];

#
# Main
#
if (@ARGV != 2) {
print "Usage: perm_check.pl directory file-extension\n";
} else {
my @list = glob("$dir/*.$filetype");
foreach (@list) {
&permission_check($_);
}
}

#
# Subroutines
#
sub permission_check {
my $mode = (stat($_))[2];
my $user_perm = $mode & S_IRWXU;
my $group_perm = $mode & S_IRWXG;
my $world_perm = $mode & S_IRWXO;

print "$_ = $user_perm $group_perm $world_perm\n"; # This would
gives me some strange numbers
printf "permissions = %04o\n", S_IMODE($mode); # This give me
the real permissions
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com