Friendly UMASK wanted

Larry Wall lwall at jato.Jpl.Nasa.Gov
Sat Feb 3 06:55:17 AEST 1990


In article <5265 at star.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
: In article <2069 at syma.sussex.ac.uk>,
: 	andy at syma.sussex.ac.uk (Andy Clews) writes:
: \...
: \		umask go-rw
: 
: Csh:
: 	alias	mask    'set tmp="`/full/path/of/mask \!*`"; eval $tmp'
: 
: Sh (supporting shell functions):
: 
: 	mask()
: 	{
: 		tmp="`/full/path/of/mask $*`" 
: 		eval $tmp 
: 	}
: 
: ...mask being the following shell script hack (Perl, Randal?).
: 
[123 line shell script from purgatory deleted]

Well, I'm not Randal, but here's the same thing in perl, 40 lines:

#!/usr/bin/perl

($name = $0) =~ s!.*/!!;
$umask = umask;

$USAGE = <<EOM;
Usage: $name mode
   or: $name [-s]
mode: a symbolic string like `g-wx,o='
  or: the equivalent octal number
see chmod(1)
EOM

($arg,$extra) = @ARGV;
die $USAGE if $extra;

if ($arg eq '') {		# report numerically
    printf "echo %o\n",umask;
}
elsif ($arg eq '-s') {		# report symbolically
    for (',o', ',g', 'u') {
	for ('x', 'w', 'r') {
	    $sym = $_ . $sym unless $umask & 1;  $umask >>= 1;
	}
	$sym = "$_=" . $sym;
    }
    print "echo $sym\n";
}
elsif ($arg =~ /^\d+$/) {	# change numerically
    print "umask $arg\n";
}
else {				# change symbolically (cheat: let chmod do it)
    $tmp="/tmp/umask$$";
    open(TMP,">$tmp") || die "$name: Can't create $tmp: $!\n";
    chmod ~$umask & 0777, $tmp;
    ($err = `chmod $ARGV[0] $tmp 2>&1`) =~ s/chmod/$name/; die $err if $err;
    ($dev,$ino,$mode) = stat TMP;
    unlink $tmp;
    printf "umask %o\n", ~$mode & 0777;
}

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.questions mailing list