From 40b620f61bc26e59c20adc74500062a558c4999e Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Thu, 28 Apr 2022 19:26:18 -0400 Subject: add POSIX same_file fallback, absolute file check --- nextbin | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/nextbin b/nextbin index 006b158..d688fa9 100755 --- a/nextbin +++ b/nextbin @@ -23,9 +23,35 @@ die() { exit 1 } -[ / -ef / ] || exec bash "$0" "$orig_exe" "$@" || die "test -ef doesn't work, and bash not found" [ -n "$PATH" ] || die 'PATH is empty' +# shellcheck disable=SC3013 +if [ / -ef / ] && ! [ / -ef /dev/null ]; then + same_file() { + # shellcheck disable=SC3013 + [ "$1" -ef "$2" ] + } +else + if command -v bash >/dev/null 2>&1; then + exec bash "$0" "$orig_exe" "$@" + fi + ino() { + ls=$(ls -i "$1") + printf '%s\n' "${ls%% *}" + } + dev() { + df -P "$1" | awk 'END{print $1}' + } + same_file() { + [ "$(ino "$1")" = "$(ino "$2")" ] && [ "$(dev "$1")" = "$(dev "$2")" ] + } +fi + +case $orig_exe in + /*) ;; + *) die 'non-absolute $0' +esac + exe_name=${orig_exe##*/} path=$PATH phase=1 @@ -34,10 +60,10 @@ while :; do exe=${path%%:*}/$exe_name if [ -f "$exe" ] && [ -x "$exe" ]; then if [ "$phase" = 1 ]; then - if [ "$exe" -ef "$orig_exe" ]; then + if same_file "$exe" "$orig_exe"; then phase=2 fi - elif [ "$exe" -ef "$orig_exe" ]; then + elif same_file "$exe" "$orig_exe"; then [ -z "$found" ] || die 'duplicate PATH entries found' elif [ -z "$found" ]; then found=$exe -- cgit v1.2.3-70-g09d2