Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

chown completion

Below is a patch against _chown which I've not committed. It adds a
certain amount of intelligence to reduce the number of matches. It
restricts the files matched to just those for which the chown command
would make sense, i.e. excluding those for which the ownership would be
unchanged by the chown command. I've got a few questions though before
I either finish or abandon this.

How could I avoid the two consecutive _files calls to specify files
where either the user is not $usr or the group is not $grp?
*(^u.$usr.g.$grp.) matches files where user is not $usr AND group is
not $grp.

For non-root users, only the groups listed by the groups command are
completed first. I think that it is only some configurations which have
this restriction on chown. Does anyone have any ideas on how to detect
this? Does the groups command work the same on other unices?

It could also restrict files to those owned by the current user (unless
it is root) as you can't chown other people's files.

Any ideas on how it should deal with invalid users or groups? As it is,
it displays `_path_files:330: unknown user' which isn't too bad really.
This also doesn't currently handle users and groups being specified
numerically but that should be fairly easy to do.

Incidentally, if I separate the user/groups with a colon, not a dot in
the glob, I get this message:
    _path_files:330: bad pattern: *(^g:users


--- _chown      Mon Apr  2 12:46:20 2001
+++ _chown      Fri May  4 17:42:47 2001
@@ -1,10 +1,13 @@
 #compdef chown chgrp
-local suf
+local suf usr grp expl ret=1
 if [[ CURRENT -eq 2 || CURRENT -eq 3 && $words[CURRENT-1] = -* ]]; then
   if [[ $service = chgrp ]] || compset -P '*[:.]'; then
-    _groups
+    if (( GID && $+commands[groups] )); then
+      _wanted groups expl 'group' compadd $(groups) && return 0
+    fi
+    _groups && return 0
     if [[ $OSTYPE = (solaris*|hpux*) ]]; then
@@ -12,8 +15,21 @@
     compset -S '.*' && unset suf
-    _users -S "$suf" -q
+    _users -S "$suf" -q && return 0
-  _files
+  usr=${words[CURRENT-1]%%[.:]*}
+  grp=${words[CURRENT-1]#*[.:]}
+  if [[ $service = chgrp ]]; then
+    _files -g "*(^g.$grp.)" && return 0
+  elif [[ $usr = $grp ]]; then
+    _files -g "*(^u.$usr.)" && return 0
+  else
+    _files -g "*(^u.$usr.)" && ret=0
+    _files -g "*(^g.$grp.)" && ret=0
+  fi
+  _files && ret=0
+return ret

This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit

Messages sorted by: Reverse Date, Date, Thread, Author