2006-02-08 09:20:05 +00:00
|
|
|
|
2006-02-15 17:59:25 +00:00
|
|
|
function __fish_umask_parse -d "Internal umask function"
|
2006-02-08 09:20:05 +00:00
|
|
|
# Test if already a valid octal mask, and pad it with zeros
|
2006-11-29 14:00:04 +00:00
|
|
|
if echo $argv | sgrep -E '^(0|)[0-7]{1,3}$' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
for i in (seq (echo 5-(echo $argv|wc -c)|bc)); set argv 0$argv; end
|
|
|
|
echo $argv
|
|
|
|
else
|
|
|
|
# Test if argument really is a valid symbolic mask
|
2006-11-29 14:00:04 +00:00
|
|
|
if not echo $argv | sgrep -E '^(((u|g|o|a|)(=|\+|-)|)(r|w|x)*)(,(((u|g|o|a|)(=|\+|-)|)(r|w|x)*))*$' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l implicit_all
|
|
|
|
|
|
|
|
# Insert inverted umask into res variable
|
|
|
|
|
|
|
|
set -l mode
|
|
|
|
set -l val
|
|
|
|
set -l tmp $umask
|
|
|
|
set -l res
|
|
|
|
|
|
|
|
for i in 1 2 3
|
|
|
|
set tmp (echo $tmp|cut -c 2-)
|
|
|
|
set res[$i] (echo 7-(echo $tmp|cut -c 1)|bc)
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l el (echo $argv|tr , \n)
|
|
|
|
for i in $el
|
|
|
|
switch $i
|
|
|
|
case 'u*'
|
|
|
|
set idx 1
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case 'g*'
|
|
|
|
set idx 2
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case 'o*'
|
|
|
|
set idx 3
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case 'a*'
|
|
|
|
set idx 1 2 3
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case '*'
|
|
|
|
set implicit_all 1
|
|
|
|
set idx 1 2 3
|
|
|
|
end
|
|
|
|
|
|
|
|
switch $i
|
|
|
|
case '=*'
|
|
|
|
set mode set
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case '+*'
|
|
|
|
set mode add
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case '-*'
|
|
|
|
set mode remove
|
|
|
|
set i (echo $i| cut -c 2-)
|
|
|
|
|
|
|
|
case '*'
|
|
|
|
if not count $implicit_all >/dev/null
|
|
|
|
printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
|
|
|
|
return
|
|
|
|
end
|
|
|
|
set mode set
|
|
|
|
end
|
|
|
|
|
2006-11-29 14:00:04 +00:00
|
|
|
if not echo $perm|sgrep -E '^(r|w|x)*$' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
printf (_ "%s: Invalid mask '%s'\n") umask $argv >&2
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
set val 0
|
2006-11-29 14:00:04 +00:00
|
|
|
if echo $i |sgrep 'r' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
set val 4
|
|
|
|
end
|
2006-11-29 14:00:04 +00:00
|
|
|
if echo $i |sgrep 'w' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
set val (echo $val + 2|bc)
|
|
|
|
end
|
2006-11-29 14:00:04 +00:00
|
|
|
if echo $i |sgrep 'x' >/dev/null
|
2006-02-08 09:20:05 +00:00
|
|
|
set val (echo $val + 1|bc)
|
|
|
|
end
|
|
|
|
|
|
|
|
for j in $idx
|
|
|
|
switch $mode
|
|
|
|
case set
|
|
|
|
set res[$j] $val
|
|
|
|
|
|
|
|
case add
|
|
|
|
set res[$j] (perl -e 'print( ( '$res[$j]'|'$val[$j]' )."\n" )')
|
|
|
|
|
|
|
|
case remove
|
|
|
|
set res[$j] (perl -e 'print( ( (7-'$res[$j]')&'$val[$j]' )."\n" )')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i in 1 2 3
|
|
|
|
set res[$i] (echo 7-$res[$i]|bc)
|
|
|
|
end
|
|
|
|
echo 0$res[1]$res[2]$res[3]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_umask_print_symbolic
|
|
|
|
set -l res ""
|
|
|
|
set -l letter a u g o
|
|
|
|
|
|
|
|
for i in 2 3 4
|
|
|
|
set res $res,$letter[$i]=
|
|
|
|
set val (echo $umask|cut -c $i)
|
|
|
|
|
|
|
|
if contains $val 0 1 2 3
|
|
|
|
set res {$res}r
|
|
|
|
end
|
|
|
|
|
|
|
|
if contains $val 0 1 4 5
|
|
|
|
set res {$res}w
|
|
|
|
end
|
|
|
|
|
|
|
|
if contains $val 0 2 4 6
|
|
|
|
set res {$res}x
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
echo $res|cut -c 2-
|
|
|
|
end
|
|
|
|
|
2006-03-01 16:53:47 +00:00
|
|
|
function umask -d (N_ "Set default file permission mask")
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
set -l as_command 0
|
|
|
|
set -l symbolic 0
|
|
|
|
|
|
|
|
set -l shortopt -o pSh
|
|
|
|
set -l longopt
|
|
|
|
if not getopt -T >/dev/null
|
|
|
|
set longopt -l as-command,symbolic,help
|
|
|
|
end
|
|
|
|
|
|
|
|
if not getopt -n umask -Q $shortopt $longopt -- $argv
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l tmp (getopt $shortopt $longopt -- $argv)
|
|
|
|
|
|
|
|
eval set opt $tmp
|
|
|
|
|
|
|
|
while count $opt >/dev/null
|
|
|
|
|
|
|
|
switch $opt[1]
|
|
|
|
case -h --help
|
2006-11-17 16:24:38 +00:00
|
|
|
__fish_print_help umask
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
case -p --as-command
|
|
|
|
set as_command 1
|
|
|
|
|
|
|
|
case -S --symbolic
|
|
|
|
set symbolic 1
|
|
|
|
|
|
|
|
case --
|
|
|
|
set -e opt[1]
|
|
|
|
break
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
set -e opt[1]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
switch (count $opt)
|
|
|
|
|
|
|
|
case 0
|
|
|
|
if not set -q umask
|
|
|
|
set -g umask 113
|
|
|
|
end
|
|
|
|
if test $as_command -eq 1
|
|
|
|
echo umask $umask
|
|
|
|
else
|
|
|
|
if test $symbolic -eq 1
|
|
|
|
__fish_umask_print_symbolic $umask
|
|
|
|
else
|
|
|
|
echo $umask
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case 1
|
|
|
|
set -l parsed (__fish_umask_parse $opt)
|
|
|
|
if test (count $parsed) -eq 1
|
|
|
|
set -g umask $parsed
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
|
|
|
|
case '*'
|
|
|
|
printf (_ '%s: Too many arguments\n') umask >&2
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|