|
|
Subscribe / Log in / New account

Octal mode translations

Octal mode translations

Posted Aug 2, 2018 20:40 UTC (Thu) by rweikusat2 (subscriber, #117920)
In reply to: The Grumpy Editor's Python 3 experience by rweikusat2
Parent article: The Grumpy Editor's Python 3 experience

BTW,

#!/usr/bin/perl
#

sub usage
{
    print STDERR ("Usage: pchmod <mode arg> <path>+\n");
    exit(1);
}

sub add { $_[0] | $_[1] }
sub clear { $_[0] & ~$_[1] } 
sub set { $_[1] }

sub valid_mode
{
    $_[0] =~ /^([+-])?0?[0-7]{1,3}$/;
}

sub parse_mode
{
    my (@ops, $op, $v);

    for (split(/,/, $_[0])) {
        die("invalid mode $_") unless valid_mode($_);
        
        if (/^([+-])(.*)/) {
            $op = $1 eq '+' ? \&add : \&clear;
            $v = $2;
        } else {
            $op = \&set;
            $v = $_;
        }

        push(@ops, [$op, oct($v)]);
    }

    return @ops;
}

my (@ops, @stat, $m, $rc);

@ARGV > 1 || usage();
@ops = parse_mode(shift);

for (@ARGV) {
    @stat = stat;
    @stat or warn("stat '$_': $!"), next;

    $m = $stat[2];
    $m = $_->[0]($m, $_->[1]) for @ops;
    $rc = chmod($m, $_);
    $rc or warn("chmod '$_': $!");
}

Assuming that's called pchmod, it becomes something like pchmod +511,-2.


to post comments

Octal mode translations

Posted Aug 2, 2018 21:57 UTC (Thu) by marcH (subscriber, #57642) [Link] (1 responses)

Wow, I didn't think I would ever see that much (coding!!) effort put into trolling...

Octal mode translations

Posted Aug 2, 2018 22:10 UTC (Thu) by rweikusat2 (subscriber, #117920) [Link]

You shouldn't post peiorative assumptions about other people just because they happen to disagree with your opinion and argue about that.

The features (or lack of features) of chmod are not relevant to the discussion. Apparently, nobody ever needed relative octal mode specifications so badly that this got implemented. As demonstrated above, this is trivial (I wrote this while waiting for a 'git gc' to finish).

Octal mode specifications are convenient in code, especially, C code, because the replacement macronames are lengthy sequences of unpronouncible gibberish. They're easy enough to remember that they're also convenient for specifying absolute modes for the chmod command. I found it useful to overcome my original "numbers ... "-prejudice and would thus encourage others to try the same.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds