zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Improve _su
@ 2020-02-12 21:26 dana
  2020-02-12 23:28 ` dana
  0 siblings, 1 reply; 2+ messages in thread
From: dana @ 2020-02-12 21:26 UTC (permalink / raw)
  To: Zsh hackers list

I was looking at _su for workers/45410 and noticed a few things:

* Fetching the user's shell often doesn't work on macOS, because normal users
  don't appear in passwd; we can use Directory Service for this

* We try to use -s to set the shell even for implementations that don't
  support that; we should skip those

* For the getent passwd case, we weren't escaping the user name before passing
  it to eval

* For the non-getent passwd case, we were doing a prefix match on the user
  name (i don't think that was intended?)

dana


diff --git a/Completion/Unix/Command/_su b/Completion/Unix/Command/_su
index 900905632..ea0beab94 100644
--- a/Completion/Unix/Command/_su
+++ b/Completion/Unix/Command/_su
@@ -58,12 +58,22 @@ fi
 _arguments $args ${(e)first} "*:shell arguments:= ->rest" && return
 
 usr=${line[norm]/--/root}
-if (( $#opt_args[(i)-(s|-shell)] )); then
+
+# Normal users generally don't appear in passwd on macOS; try the Directory
+# Service first
+if [[ $OSTYPE == darwin* ]] && (( $+commands[dscl] )); then
+  shell=${"$(
+    _call_program shells dscl . -read /Users/${(q)usr} UserShell
+  )"#UserShell: }
+fi
+
+[[ -z $shell ]] &&
+if (( ${#${(@M)args:#*-s\[*\]:*}} && $#opt_args[(i)-(s|-shell)] )); then
   shell=${(v)opt_args[(i)-(s|-shell)]}
 elif (( ${+commands[getent]} )); then
-  shell="${$(_call_program shells getent passwd $usr)##*:}"
+  shell="${$(_call_program shells getent passwd ${(q)usr})##*:}"
 else
-  shell="${${(M@)${(@f)$(</etc/passwd)}:#$usr*}##*:}"
+  shell="${${(M@)${(@f)$(</etc/passwd)}:#${usr}:*}##*:}"
 fi
 
 case $state in


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-12 23:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-12 21:26 [PATCH] Improve _su dana
2020-02-12 23:28 ` dana

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).