about summary refs log tree commit diff
path: root/Completion/Unix/Command/_chown
blob: 71c21da32f6b9840c90a6def6e0e2d1028e7d89d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#compdef chown chgrp zf_chown=chown zf_chgrp=chgrp

local curcontext="$curcontext" state line expl ret=1
local suf usr grp req deref args

if _pick_variant gnu=Free\ Soft unix --version; then
  args=(
    '(-c --changes -v --verbose)'{-c,--changes}'[report each change made]'
    '(-c --changes -v --verbose)'{-v,--verbose}'[output info for every file processed]'
    '(-h --no-dereference)--dereference[dereference symlinks]'
    '(-h --no-dereference --dereference)'{-h,--no-dereference}'[operate on symlinks them self]'
    '(-f --silent --quiet)'{-f,--silent,--quiet}"[don't report errors]"
    '--reference=[copy ownership of specified file]:file:_files'
    '(-R --recursive)'{-R,--recursive}'[change files and directories recursively]'
    '(- : *)--help[display help information]'
    '(- : *)--version[display version information]'
  )    
  [[ $service = chown ]] &&
      args+=( '--from=[restrict changes to files by current ownership]: :->owner' )
else
  # based on $OSTYPE = solaris2.8
  args=(
    "-f[don't report errors]"
    "-h[operate on symlinks them self]"
    '-R[change files and directories recursively]'
  )
fi

(( $+words[(r)--reference*] )) || args+=( '(--reference)1: :->owner' )
_arguments -C -s "$args[@]" '*:files:->files' && ret=0

case $state in
  owner)
    if [[ $service = chgrp ]] || compset -P '*[:.]'; then
      if (( EGID && $+commands[groups] && ! $+funcstack[(r)_sudo] )); then  # except for root
       _wanted groups expl 'group' compadd $(groups) && return 0
      fi
      _groups && ret=0
    else
      if compset -S '[.:]*'; then
        suf=()
      elif [[ $OSTYPE = irix* ]]; then
	suf=( -qS '.' )
      else
	suf=( -qS ':' )
      fi
      _users "$suf[@]" && ret=0
    fi
  ;;
  files)
    (( $+opt_args[-h] || $+opt_args[--no-dereference] )) || deref="-"
    if (( $+opt_args[--reference] )); then
      zmodload -F zsh/stat b:zstat 2>/dev/null
      usr=$(zstat +uid $opt_args[--reference])
      grp=$(zstat +gid $opt_args[--reference])
      _wanted files expl file _files -g "*($deref^u$usr,$deref^g$grp)" && ret=0
      return ret
    fi
    if [[ $service = chgrp ]]; then
      grp=${line[1]}
    else
      usr=${line[1]%%[.:]*}
      usr=${${(M)usr:#[0-9]#}:-${userdirs[$usr]:+.$usr.}}
      grp=${${(M)line[1]%%[.:]*}#?}
    fi
    [[ -n $grp ]] && grp="${${(M)grp:#[0-9]#}:-.$grp.}"
    req=( ${usr:+\^u$usr} ${grp:+\^g$grp} )
    (( EUID )) && req=( u$EUID$^req )
    req=( $deref$^req )
    req="*(${(j:,:)req})"

    ( : $~req ) 2> /dev/null || req='*'

    _wanted files expl file _files -g "$req" && ret=0
  ;;
esac

return ret