| 1 | 1 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Stat::lsMode; | ||||||||||
| 4 | 1 | our $VERSION = '0.51'; | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | BEGIN { require Exporter; *import = \&Exporter::import } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | our @EXPORT = qw(format_mode file_mode format_perms); | |||||||||
| 9 | |||||||||||
| 10 | 1 | our (@perms, @ftype); | |||||||||
| 11 | 1 | @perms = qw(--- --x -w- -wx r-- r-x rw- rwx); | |||||||||
| 12 | 1 | @ftype = qw(. p c ? d ? b ? - ? l ? s D ? ?); | |||||||||
| 13 | 1 | $ftype[0] = ''; | |||||||||
| 14 | |||||||||||
| 15 | 1 | our $NOVICE_MODE = 1; # Default on? | |||||||||
| 16 | 0 | ▶ | sub novice { | ||||||||
|
Subroutine
novice: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 17 | 0 | my $pack = shift; | |||||||||
| 18 | 0 | ▶ | croak "novice_mode requires one boolean argument" unless @_ == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 0 | my $old = $NOVICE_MODE; # Should this be localized t $pack? | |||||||||
| 20 | 0 | $NOVICE_MODE = $_[0]; | |||||||||
| 21 | 0 | $old; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 14 | ▶ | sub format_mode { | ||||||||
|
Subroutine
format_mode: called CC=14 SCAR=41.5
Pod
documented
| |||||||||||
| 25 | 14 | ▶ | croak "format_mode requires a mode as an argument" unless @_ >= 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 14 | my $mode = shift; | |||||||||
| 27 | 14 | my %opts = @_; | |||||||||
| 28 | |||||||||||
| 29 | 14 | ▶ | unless (defined $mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | ▶ | return wantarray() ? () : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 14 | ▶ | _novice_warning($mode) if $NOVICE_MODE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 14 | my $setids = ($mode & 07000)>>9; | |||||||||
| 36 | 14 | my @permstrs = @perms[($mode&0700)>>6, ($mode&0070)>>3, $mode&0007]; | |||||||||
| 37 | 14 | my $ftype = $ftype[($mode & 0170000)>>12]; | |||||||||
| 38 | 14 | ▶ | my @ftype = $opts{no_ftype} ? () : ($ftype); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 14 | ▶ | if ($setids) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | ▶ | if ($setids & 01) { # Sticky bit | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | ▶ | $permstrs[2] =~ s/([-x])$/$1 eq 'x' ? 't' : 'T'/e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | 0 | ▶ | if ($setids & 04) { # Setuid bit | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | ▶ | $permstrs[0] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | 0 | ▶ | if ($setids & 02) { # Setgid bit | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | ▶ | $permstrs[1] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 14 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | (@ftype, @permstrs); | |||||||||
| 54 | } else { | ||||||||||
| 55 | 14 | join '', @ftype, @permstrs; | |||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 6 | ▶ | sub file_mode { | ||||||||
|
Subroutine
file_mode: called CC=5 SCAR=23.1
Pod
documented
| |||||||||||
| 60 | 6 | ▶ | croak "file_mode requires one filename as an argument" unless @_ == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 6 | my $file = shift; | |||||||||
| 62 | 6 | my $mode = (lstat $file)[2]; | |||||||||
| 63 | |||||||||||
| 64 | 6 | ▶ | unless (defined $mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | return (); | |||||||||
| 67 | } else { | ||||||||||
| 68 | 0 | ▶ | carp "Couldn't get mode for file `$file': $!" if $NOVICE_MODE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | return undef; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 6 | format_mode($mode, @_); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub format_perms { | ||||||||
|
Subroutine
format_perms: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 77 | 0 | ▶ | croak "format_perms requires a permission mode as an argument" unless @_ == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | format_mode($_[0], no_ftype => 1); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | # None of these are really plausible modes. | ||||||||||
| 82 | # They are all almost certain to have occurred | ||||||||||
| 83 | # when someone used decimal instead of octal to specify a mode. | ||||||||||
| 84 | |||||||||||
| 85 | 1 | my %badmode = map +($_ => 1), ( | |||||||||
| 86 | 777, 775, 755, 770, 700, 750, | ||||||||||
| 87 | 751, | ||||||||||
| 88 | 666, 664, 644, 660, 600, 640, | ||||||||||
| 89 | 444, 440, | ||||||||||
| 90 | 400, # 400 = rw--w---- which is just barely plausible. | ||||||||||
| 91 | # 000 *is* OK. It means just what you think. | ||||||||||
| 92 | 711, 771, 751, 551, 111, | ||||||||||
| 93 | ); | ||||||||||
| 94 | |||||||||||
| 95 | # Novices like to ask for the bits for mode `666' instead of `0666'. | ||||||||||
| 96 | # Try to detect and diagnose that. | ||||||||||
| 97 | 14 | ▶ | sub _novice_warning { | ||||||||
|
Subroutine
_novice_warning: called CC=2 SCAR=8.1
| |||||||||||
| 98 | 14 | my $mode = shift; | |||||||||
| 99 | 14 | ▶ | if ($badmode{$mode}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | carp "mode $mode is very surprising. Perhaps you meant 0$mode"; | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 1 | 1; | |||||||||
| 105 | |||||||||||
| 106 | __END__ | ||||||||||