Rather than always re-implementing `readlink -f` in shell, probe the host
tool first to see if it works.

Signed-off-by: Mike Frysinger <vap...@gentoo.org>
---
 bin/misc-functions.sh | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/bin/misc-functions.sh b/bin/misc-functions.sh
index ac08bd9..c8b7cc8 100755
--- a/bin/misc-functions.sh
+++ b/bin/misc-functions.sh
@@ -43,7 +43,20 @@ install_symlink_html_docs() {
 }
 
 # replacement for "readlink -f" or "realpath"
+READLINK_F_WORKS=""
 canonicalize() {
+       if [[ -z ${READLINK_F_WORKS} ]] ; then
+               if [[ $(readlink -f -- /../ 2>/dev/null) == "/" ]] ; then
+                       READLINK_F_WORKS=true
+               else
+                       READLINK_F_WORKS=false
+               fi
+       fi
+       if ${READLINK_F_WORKS} ; then
+               readlink -f -- "$@"
+               return
+       fi
+
        local f=$1 b n=10 wd=$(pwd)
        while (( n-- > 0 )); do
                while [[ ${f: -1} = / && ${#f} -gt 1 ]]; do
-- 
1.7.12


Reply via email to