1: ReturnFileMask returns a filemask obtained by 'stat' 
   2: to the -rwxrwxrwx format.... please tell me if there 
   3: is a better way to do this.... We couldn't find one... 
   4: and it works great in our case
   5: 
   6: sub ReturnFileMask($)
   7: {
   8:  my $Numeric=sprintf "%3o",(shift)&07777;
   9:  my $Mask='-';
  10:  foreach(split //,$Numeric)
  11:  {
  12:   my $Tmp=$_;
  13:   my @Pat=qw(r w x);
  14:   foreach(4,2,1)
  15:   {
  16:    (($Tmp-=$_) ge 0) ? ($Mask.=shift @Pat) : ($Mask.="-") && ($Tmp+=$_) && (shift @Pat);
  17:   }
  18:  }
  19:  return $Mask;
  20: }

Replies are listed 'Best First'.
RE: FileMask Conversion
by btrott (Parson) on May 10, 2000 at 19:58 UTC
    You could check out Stat::lsMode, which does the same thing and also a bit more (doesn't just do the rwx bits).

    It also has a slightly different and cleaner algorithm (no offense meant to yours :).

RE: FileMask Conversion
by nuance (Hermit) on May 12, 2000 at 23:41 UTC
    Well to be honest I don't really know if this is a "better" solution than yours, but it reads more like the way I think. The y/ /0/ statement is only there because the interpreter was giving me a warning about a bitwise_and with a " " (there may be a better way to do this with options to sprintf).

    So, in the spitrit of TIMTOWTDI.

    sub ReturnFileMask($) { my $Numeric=sprintf "%3o",(shift)&07777; my $Mask='-xw@r'; my $Tmp="-"; foreach(split //,$Numeric) { y/ /0/; $Tmp .= substr($Mask, $_ & 4, 1) . substr($Mask, $_ & 2, 1) . substr($Mask, $_ & 1, 1); } return $Tmp; }
    or alternitively, my second attempt, which doesn't ponce about with the bit wise ands. I think I much prefer this:

    sub ReturnFileMask($) { my $Numeric=sprintf "%3o",(shift)&07777; my $Tmp="-"; foreach(split //,$Numeric) { y/ /0/; $Tmp .= substr('----rrrr', $_, 1) . substr('--ww--ww', $_, 1) . substr('-x-x-x-x', $_, 1); } return $Tmp; }
    Or, how about the really simple
    sub ReturnFileMask($) { my $Tmp="-"; foreach(split //,sprintf "%3o",(shift)&07777) { y/ /0/; $Tmp .= ("---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx")[$ +_]; } return $Tmp; }

    Nuance

    Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

RE: FileMask Conversion
by gnat (Beadle) on May 17, 2000 at 23:21 UTC
    Here's mine:

    $permish = 0751; # as returned by stat $b = 01000; ($p = "rwxrwxrwx") =~ s/(.)/($b>>=1)&$permish ? $1 : '-'/ge; print "$p\n";

    This doesn't cope with setuid bits, mind you.

RE: FileMask Conversion
by gnat (Beadle) on May 17, 2000 at 23:24 UTC
    I guess to make it aware of setuid/setgid you'd just add tests for those bits externally. If anyone can incorporate setguid awareness into the s//e I'd be impressed.
RE: FileMask Conversion
by BBQ (Curate) on May 10, 2000 at 20:41 UTC
    This looks to me as a strong candidate for the Obfuscated Code category. Seriously, maybe it's just 'cause I don't know what you're doing, or maybe because your coding is extremely C'ish. I think it looks cool although I don't have the 1st clue as where it would be useful (or what it actually does).

    #!/home/bbq/bin/perl
    # Trust no1!
RE: FileMask Conversion
by Anonymous Monk on May 14, 2000 at 22:38 UTC
    yes