Skip to content

Commit 4c13d6c

Browse files
committed
search current directory with PATH
1 parent 77b8ab3 commit 4c13d6c

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/kcptun

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,39 @@ UNAME_S="${UNAME_S:-"$(uname -s)"}"
2828
main() {
2929
local type bin key0 value0 key1 value1
3030

31+
search_in() {
32+
local saved_path name
33+
saved_path="$PATH"
34+
export PATH="$1"
35+
for type in client server; do
36+
for name in "${2}_$type" "${2}-$type" "${type}_${3}_$4"; do
37+
if hash "$name" 2>/dev/null; then
38+
export PATH="$saved_path"
39+
bin="$1/$name"
40+
return
41+
fi
42+
done
43+
if [ "$4" = amd64 ]; then
44+
name="${type}_${3}_386"
45+
if hash "$name" 2>/dev/null; then
46+
export PATH="$saved_path"
47+
bin="$1/$name"
48+
return
49+
fi
50+
fi
51+
done
52+
export PATH="$saved_path"
53+
return 1
54+
}
55+
3156
findBinary() {
32-
local os suffix name arch cwd bn
57+
local os arch bn
3358
os=$(printf "%s" "$UNAME_S" | awk '{print tolower($0)}')
34-
case "$os" in
35-
MINGW*|CYGWIN*|mingw*|cygwin*) os=windows; suffix=".exe";;
36-
esac
59+
case "$os" in MINGW* | CYGWIN* | mingw* | cygwin*) os=windows ;; *) ;; esac
3760
arch="$(printf "%s" "$UNAME_M" | sed -e 's/^[Ii][3-6]86$/386/;s/^[Xx]86$/386/;s/^[Xx]\(86[-_]\)\{0,1\}64$/amd64/;s/^[Aa][Mm][Dd]64$/amd64/')"
38-
cwd="$(pwd)"
3961
bn="${BASENAME%.*}"
4062

41-
for type in client server; do
42-
for name in "${bn}_$type" "${bn}-$type" "${type}_${os}_$arch"; do
43-
bin="$cwd/$name$suffix"
44-
if [ -f "$bin" ] && [ -x "$bin" ]; then return; fi
45-
done
46-
if [ "$arch" = "amd64" ]; then
47-
bin="$cwd/${type}_${os}_386$suffix"
48-
if [ -f "$bin" ] && [ -x "$bin" ]; then return; fi
49-
fi
50-
done
63+
if search_in . "$bn" "$os" "$arch"; then return; fi
5164

5265
for type in client server; do
5366
for bin in "${bn}_$type" "${bn}-$type"; do

0 commit comments

Comments
 (0)