-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions_os.CYGWIN_NT
120 lines (98 loc) · 3.25 KB
/
.functions_os.CYGWIN_NT
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#----- header -----
[ "${0##*/}" != "${BASH_SOURCE##*/}" ] || { >&2 echo -e "ERROR\tfile must be sourced ($0)"; return 2; }
#------------------
#NOTE
# Unix-like environments use ':' in PATH, CLASSPATH etc. but
# windows-native binaries like Go, Python require ';' so use addPath()
#
PATHSEP=';'
readonly PATHSEP
function __WHICH() { \which --skip-alias --skip-functions "$@"; }
readonly -f __WHICH
# override CYGWIN default which uses non-portable Junctions
function ln() {
is_exec mklink.exe || { /bin/ln "$@"; return; }
local flag destdir=
local -i OPTIND; local opt OPTARG
while getopts ':hHJst:v' opt; do
case "$opt" in
H|J) flag="/$OPTARG" ;;
s) unset flag ;;
t) is_dir "${destdir:=$OPTARG}" || return ;;
v) local VERBOSE=1 ;;
:) log.error "missing argument (-$OPTARG)" ;;&
\?) log.error "unsupported option (-${OPTARG})" ;&
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME ...
_EOF
return 2
esac
done
shift $((OPTIND - 1))
# allows trailing directory like real 'ln' but $# >2 else dangling link
[ $# -gt 2 ] &&
if [ -n "$destdir" ]; then
:
elif [ -d "${!#}" ]; then
destdir=${!#}; set -- "${@:1:$#-1}"
else
log.error "ARGC > 2 requires '-t' or directory as last parameter"
return 2
fi
local -r _flag=$flag # save
while (( $# )); do
flag=$_flag # restore
local target=$1 link=$2
[ -d "${target:?}" ] && flag='/D'
[ -n "$destdir" ] && unset link
# mklink is retarded. CWD must be `dirname $link`
( cd "${destdir:-`dirname "${link:-.}"`}"
link=$( basename "${link:-$target}" )
${DEBUG:+ runv} mklink $flag "$link" "$( cygpath -w "$target" )"
) || break
shift
done
}
readonly -f ln
#https://www.authlite.com/kb/allow-runas-but-block-interactive-logon
#https://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/
is_exec -q sudo ||
function sudo() (
local who cmd preserve
local -a flags=()
local -i OPTIND; local opt OPTARG
while getopts ':hieEu:X' opt; do
case "$opt" in
i) unset preserve ;;
e) cmd=$( VERBOSE=1 is_exec ${EDITOR:-vi} ) || return ;;
E) preserve= ;;
u) who=$OPTARG ;;
X) exec=1 ;;
:) log.error "missing argument (-$OPTARG)" ;;&
\?) flags+=( "-$OPTARG" ) ;;
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME ...
_EOF
return 2
esac
done
shift $((OPTIND - 1))
: ${who:?missing argument \'-u USER\'}
set -e
local line
line=$( getent passwd $who || grep --no-messages "^${who}:" /etc/passwd ) || {
log.error "unknown USER ($who)"; return; }
local -a _env=( `awk -F: '{ printf("USER=%s USERNAME=%s HOME=%s UID=%s", $1, $1, $(NF-1), $3) }' <<< "$line"` )
# preserve certain values regardless
for e in USERPROFILE ALLUSERSPROFILE TERM; do
[ -n "${!e}" ] && _env+=( "$e=\'${!e}\'" )
done
local shell=${line##*:}
is_exec "${shell:=$SHELL}"
${DEBUG:+ runv} ${exec:+exec} env ${preserve-'-i'} "${_env[@]}" ${cmd:-$shell} "${flags[@]}" "$@"
#TODO elvated shell:
# powershell start-process powershell -verb runas
# powershell -Command "Start-Process cmd -ArgumentList '/K cd /D %CD%' -Verb RunAs"
)
readonly -f sudo
# vim: expandtab:ts=4:sw=4