-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe
executable file
·67 lines (55 loc) · 1.95 KB
/
e
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
#!/usr/bin/env sh
# compat: +ash +bash +dash -hush -ksh +mksh +oksh +osh +posh +yash +zsh
e() { ### @-
### wrap around `$EDITOR` to run it as root if necessary.
### this still needs some work to detect root-owned directories.
###
### ```
### $ e /etc/sudoers
### [sudo] password for notwa:
### ```
###
### **NOTE:** there also exists an e(1) program provided by
### the *e-wrapper* package that i don't use.
[ -z "$ZSH_OPTIONS" ] || setopt local_options sh_word_split
local d= f= temp= running= editor= needroot=0
editor="${EDITOR%% *}"
(running pid cmd | while read -r pid cmd; do
if [ "$cmd" = "$editor" ]; then
printf "%s (%s)\n" "$editor is already running" "$pid" >&2
confirm
exit
fi
done) || return
for f; do
temp="$(readlink -f "$f")" && f="$temp"
[ -z "$MSYSTEM" ] || f="$(cygpath -u "$f")" || continue
# easy: file exists, we have write permissions
[ -w "$f" ] && continue
# easy: file exists, but no write permissions
[ -e "$f" ] && { needroot=1; break; }
# hard: file may be in a directory that we can't inspect
d="$f"
while [ "${d%/*}" != "$d" ]; do
d="${d%/*}"
[ -w "$d/" ] && break
[ -e "$d/" ] && { needroot=1; break; }
done
[ $needroot = 1 ] && break
# easy: file just doesn't exist
done
if [ $needroot = 1 ] && [ -n "$MSYSTEM" ]; then
# this pretty much never happens, because permissions are so busted, but...
printf "NOTE: you need root permissions, but this is Windows." >&2
printf " this probably isn't going to work." >&2
pause
$EDITOR "$@"
elif [ $needroot = 0 ]; then
$EDITOR "$@"
else
sudo -e "$@"
fi
}
[ -n "${preload+-}" ] || . ~/sh/preload || exit 2
eval ${preload:-preload} running confirm pause
[ -n "${preload+-}" ] || e "$@"