From 47b795973613664dc3aa7d532291fd4deeccf36b Mon Sep 17 00:00:00 2001 From: Dan Bolsun Date: Wed, 7 May 2014 16:02:20 +0300 Subject: [PATCH] z plugin: allow usage while in sudo mode --- plugins/z/Makefile | 6 ++---- plugins/z/README | 7 +++++-- plugins/z/z.1 | 3 +++ plugins/z/z.sh | 4 +++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/plugins/z/Makefile b/plugins/z/Makefile index dcf433d40..7591dc9d1 100644 --- a/plugins/z/Makefile +++ b/plugins/z/Makefile @@ -1,4 +1,2 @@ -readme: - @groff -man -Tascii z.1 | col -bx - -.PHONY: readme +README: z.1 + @groff -man -Tascii z.1 | col -bx > README diff --git a/plugins/z/README b/plugins/z/README index ec5abc6f5..9e8d574a0 100644 --- a/plugins/z/README +++ b/plugins/z/README @@ -118,9 +118,12 @@ ENVIRONMENT directories to exclude from tracking. $HOME is always excluded. Direc- tories must be full paths without trailing slashes. + The environment variable $_Z_OWNER can be set if you want to use z + while `sudo -s' and $HOME variable was kept. + FILES - Data is stored in $HOME/.z. This can be overridden by setting the - $_Z_DATA environment variable. When initialized, z will raise an error + Data is stored in $HOME/.z. This can be overridden by setting the + $_Z_DATA environment variable. When initialized, z will raise an error if this path is a directory, and not function correctly. A man page (z.1) is provided. diff --git a/plugins/z/z.1 b/plugins/z/z.1 index 022a4b35d..e6152335e 100644 --- a/plugins/z/z.1 +++ b/plugins/z/z.1 @@ -141,6 +141,9 @@ handle \fBPROMPT_COMMAND\fR or \fBprecmd\fR yourself. The environment variable \fB$_Z_EXCLUDE_DIRS\fR can be set to an array of directories to exclude from tracking. \fB$HOME\fR is always excluded. Directories must be full paths without trailing slashes. +.P +The environment variable \fB$_Z_OWNER\fR can be set if you want to use z +while `sudo -s' and \fB$HOME\fR variable was kept. .SH FILES Data is stored in \fB$HOME/.z\fR. This can be overridden by setting the diff --git a/plugins/z/z.sh b/plugins/z/z.sh index 7e444ef46..6eba45ed8 100644 --- a/plugins/z/z.sh +++ b/plugins/z/z.sh @@ -13,6 +13,7 @@ # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself. # set $_Z_EXCLUDE_DIRS to an array of directories to exclude. +# set $_Z_OWNER to your username if you want use z while sudo with $HOME kept # # USE: # * z foo # cd to most frecent dir matching foo @@ -36,7 +37,7 @@ _z() { local datafile="${_Z_DATA:-$HOME/.z}" # bail out if we don't own ~/.z (we're another user but our ENV is still set) - [ -f "$datafile" -a ! -O "$datafile" ] && return + [ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return # add entries if [ "$1" = "--add" ]; then @@ -54,6 +55,7 @@ _z() { # maintain the file local tempfile tempfile="$(mktemp "$datafile.XXXXXX")" || return + [ -n "$_Z_OWNER" ] && chown $_Z_OWNER:$(id -ng $_Z_OWNER) $tempfile while read line; do [ -d "${line%%\|*}" ] && echo $line done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '