Skip to content

Commit

Permalink
search current directory with PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanhb committed Dec 1, 2021
1 parent 77b8ab3 commit 4c13d6c
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/kcptun
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,39 @@ UNAME_S="${UNAME_S:-"$(uname -s)"}"
main() {
local type bin key0 value0 key1 value1

search_in() {
local saved_path name
saved_path="$PATH"
export PATH="$1"
for type in client server; do
for name in "${2}_$type" "${2}-$type" "${type}_${3}_$4"; do
if hash "$name" 2>/dev/null; then
export PATH="$saved_path"
bin="$1/$name"
return
fi
done
if [ "$4" = amd64 ]; then
name="${type}_${3}_386"
if hash "$name" 2>/dev/null; then
export PATH="$saved_path"
bin="$1/$name"
return
fi
fi
done
export PATH="$saved_path"
return 1
}

findBinary() {
local os suffix name arch cwd bn
local os arch bn
os=$(printf "%s" "$UNAME_S" | awk '{print tolower($0)}')
case "$os" in
MINGW*|CYGWIN*|mingw*|cygwin*) os=windows; suffix=".exe";;
esac
case "$os" in MINGW* | CYGWIN* | mingw* | cygwin*) os=windows ;; *) ;; esac
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/')"
cwd="$(pwd)"
bn="${BASENAME%.*}"

for type in client server; do
for name in "${bn}_$type" "${bn}-$type" "${type}_${os}_$arch"; do
bin="$cwd/$name$suffix"
if [ -f "$bin" ] && [ -x "$bin" ]; then return; fi
done
if [ "$arch" = "amd64" ]; then
bin="$cwd/${type}_${os}_386$suffix"
if [ -f "$bin" ] && [ -x "$bin" ]; then return; fi
fi
done
if search_in . "$bn" "$os" "$arch"; then return; fi

for type in client server; do
for bin in "${bn}_$type" "${bn}-$type"; do
Expand Down

0 comments on commit 4c13d6c

Please sign in to comment.