diff --git a/ghcup b/ghcup index ef28f57..2ad25f9 100755 --- a/ghcup +++ b/ghcup @@ -1130,29 +1130,35 @@ get_distro_alias() { # and # https://stackoverflow.com/a/246128 # -# If the file does not exist, just prints the argument unchanged. +# If the file does not exist, just prints it appended to the current directory. # @STDOUT: realpath of the given file posix_realpath() { [ -z "$1" ] && die "Internal error: no argument given to posix_realpath" + current_loop=0 + max_loops=50 mysource=$1 while [ -h "${mysource}" ]; do + current_loop=$((current_loop+1)) mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" mysource="$(readlink "${mysource}")" [ "${mysource%${mysource#?}}"x != '/x' ] && mysource="${mydir}/${mysource}" + + if [ ${current_loop} -gt ${max_loops} ] ; then + (>&2 echo "${1}: Too many levels of symbolic links") + break + fi done mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null 2>&1 && pwd )" # TODO: better distinguish between "does not exist" and "permission denied" if [ -z "${mydir}" ] ; then (>&2 echo "${1}: Permission denied") - elif [ ! -e "$1" ] ; then - echo "${mysource}" else echo "${mydir%/}/$(basename "${mysource}")" fi - unset mysource mydir posix_realpath_error + unset current_loop max_loops mysource mydir }