sub setpermbybitnum { my $vec=""; vec($vec,shift,1) = 1; $vec } # check if any perms are set sub no_perm { "$_[0]" =~ tr/\0//c == 0} # check if all specified perm(s) are set sub has_perms { no_perm( ("$_[0]" & "$_[1]") ^ "$_[1]") } # check if any specified perms are set sub one_or_more_of_perms { !no_perm("$_[0]" & "$_[1]") } use constant READ_PERM => setpermbybitnum(3); use constant WRITE_PERM => setpermbybitnum(2); use constant EXECUTE_PERM => setpermbybitnum(1); my $allowed = READ_PERM | WRITE_PERM; # Check for permissions: if ( no_perm($allowed) ) { print "PERMISSION DENIED\n"; exit -1; } if ( has_perms($allowed, READ_PERM) ) { print "READ Allowed\n"; } if ( has_perms($allowed, WRITE_PERM) ) { print "WRITE Allowed\n"; } if ( has_perms($allowed, EXECUTE_PERM) ) { print "EXECUTE Allowd\n"; } if ( has_perms($allowed, READ_PERM|WRITE_PERM ) ) { print "READ and WRITE Allowed\n"; } if ( has_perms($allowed, READ_PERM|EXECUTE_PERM) ) { print "READ and EXECUTE Allowed\n"; } if ( one_or_more_of_perms($allowed, READ_PERM|EXECUTE_PERM) ) { print "READ or EXECUTE Allowed\n"; }
In reply to Re: Bitmask or Named permissions
by Anonymous Monk
in thread Bitmask or Named permissions
by linux454
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |