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
|
2014-07-29 05:43:05 +00:00
|
|
|
if echo $argv | sgrep -E '^0?[0-7]{1,3}$' >/dev/null
|
2006-12-12 17:11:18 +00:00
|
|
|
set -l char_count (echo $argv| wc -c)
|
|
|
|
for i in (seq (math 5 - $char_count)); set argv 0$argv; end
|
2010-09-18 02:18:26 +00:00
|
|
|
echo $argv
|
2006-02-08 09:20:05 +00:00
|
|
|
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-)
|
2006-12-12 17:11:18 +00:00
|
|
|
set -l char_count (echo $tmp|cut -c 1)
|
|
|
|
set res[$i] (math 7 - $char_count)
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
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
|
2010-09-18 02:18:26 +00:00
|
|
|
set i (echo $i| cut -c 2-)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
case '+*'
|
|
|
|
set mode add
|
2010-09-18 02:18:26 +00:00
|
|
|
set i (echo $i| cut -c 2-)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
case '-*'
|
|
|
|
set mode remove
|
2010-09-18 02:18:26 +00:00
|
|
|
set i (echo $i| cut -c 2-)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
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-12-12 17:11:18 +00:00
|
|
|
set val (math $val + 2)
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
2006-11-29 14:00:04 +00:00
|
|
|
if echo $i |sgrep 'x' >/dev/null
|
2006-12-12 17:11:18 +00:00
|
|
|
set val (math $val + 1)
|
2006-02-08 09:20:05 +00:00
|
|
|
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
|
2006-12-12 17:11:18 +00:00
|
|
|
set res[$i] (math 7 - $res[$i])
|
2006-02-08 09:20:05 +00:00
|
|
|
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
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
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
|
|
|
|
|
2007-01-16 01:29:18 +00:00
|
|
|
function umask --description "Set default file permission mask"
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
set -l as_command 0
|
|
|
|
set -l symbolic 0
|
2013-01-12 23:35:40 +00:00
|
|
|
|
|
|
|
set -l options
|
|
|
|
set -l shortopt pSh
|
2006-02-08 09:20:05 +00:00
|
|
|
if not getopt -T >/dev/null
|
2013-01-12 23:35:40 +00:00
|
|
|
# GNU getopt
|
2006-02-08 09:20:05 +00:00
|
|
|
set longopt -l as-command,symbolic,help
|
2013-01-26 20:49:37 +00:00
|
|
|
set options -o $shortopt $longopt --
|
2013-01-12 23:35:40 +00:00
|
|
|
# Verify options
|
|
|
|
if not getopt -n umask $options $argv >/dev/null
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Old getopt, used on OS X
|
|
|
|
set options $shortopt
|
|
|
|
# Verify options
|
|
|
|
if not getopt $options $argv >/dev/null
|
|
|
|
return 1
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
2013-01-12 23:35:40 +00:00
|
|
|
set -l tmp (getopt $options $argv)
|
2006-02-08 09:20:05 +00:00
|
|
|
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
|
2010-09-18 02:18:26 +00:00
|
|
|
set as_command 1
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
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
|