From 4c13d6ce8c314fc2c3e544a32206c44bc026ad25 Mon Sep 17 00:00:00 2001 From: zhanhb <6323014+zhanhb@users.noreply.github.com> Date: Wed, 1 Dec 2021 09:48:34 +0800 Subject: [PATCH] search current directory with PATH --- src/kcptun | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/kcptun b/src/kcptun index fceb2e2..c3949ec 100755 --- a/src/kcptun +++ b/src/kcptun @@ -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