Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Gentoo Portage Developer (http://www.linux-archive.org/gentoo-portage-developer/)
-   -   use `readlink -f` if it works (http://www.linux-archive.org/gentoo-portage-developer/706799-use-readlink-f-if-works.html)

Mike Frysinger 09-24-2012 12:28 AM

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

Signed-off-by: Mike Frysinger <vapier@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


All times are GMT. The time now is 07:34 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.