1# The functions in this file provide support for relocatability of 2# shell scripts. They should be included near the beginning of each 3# shell script in a relocatable program, by adding @relocatable_sh@ 4# and causing the script to be expanded with AC_CONFIG_FILES. A 5# small amount of additional code must be added and adapted to the 6# package by hand; see doc/relocatable-maint.texi (in Gnulib) for 7# details. 8# 9# Copyright (C) 2003, 2005-2007 Free Software Foundation, Inc. 10# 11# This program is free software: you can redistribute it and/or modify 12# it under the terms of the GNU General Public License as published by 13# the Free Software Foundation; either version 3 of the License, or 14# (at your option) any later version. 15# 16# This program is distributed in the hope that it will be useful, 17# but WITHOUT ANY WARRANTY; without even the implied warranty of 18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19# GNU General Public License for more details. 20# 21# You should have received a copy of the GNU General Public License 22# along with this program. If not, see <http://www.gnu.org/licenses/>. 23# 24 25# func_tmpdir 26# creates a temporary directory. 27# Sets variable 28# - tmp pathname of freshly created temporary directory 29func_tmpdir () 30{ 31 # Use the environment variable TMPDIR, falling back to /tmp. This allows 32 # users to specify a different temporary directory, for example, if their 33 # /tmp is filled up or too small. 34 : ${TMPDIR=/tmp} 35 { 36 # Use the mktemp program if available. If not available, hide the error 37 # message. 38 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` && 39 test -n "$tmp" && test -d "$tmp" 40 } || 41 { 42 # Use a simple mkdir command. It is guaranteed to fail if the directory 43 # already exists. $RANDOM is bash specific and expands to empty in shells 44 # other than bash, ksh and zsh. Its use does not increase security; 45 # rather, it minimizes the probability of failure in a very cluttered /tmp 46 # directory. 47 tmp=$TMPDIR/gl$$-$RANDOM 48 (umask 077 && mkdir "$tmp") 49 } || 50 { 51 echo "$0: cannot create a temporary directory in $TMPDIR" >&2 52 { (exit 1); exit 1; } 53 } 54} 55 56# Support for relocatability. 57func_find_curr_installdir () 58{ 59 # Determine curr_installdir, even taking into account symlinks. 60 curr_executable="$0" 61 case "$curr_executable" in 62 */* | *\\*) ;; 63 *) # Need to look in the PATH. 64 if test "${PATH_SEPARATOR+set}" != set; then 65 func_tmpdir 66 { echo "#! /bin/sh"; echo "exit 0"; } > "$tmp"/conf.sh 67 chmod +x "$tmp"/conf.sh 68 if (PATH="/nonexistent;$tmp"; conf.sh) >/dev/null 2>&1; then 69 PATH_SEPARATOR=';' 70 else 71 PATH_SEPARATOR=: 72 fi 73 rm -rf "$tmp" 74 fi 75 save_IFS="$IFS"; IFS="$PATH_SEPARATOR" 76 for dir in $PATH; do 77 IFS="$save_IFS" 78 test -z "$dir" && dir=. 79 for exec_ext in ''; do 80 if test -f "$dir/$curr_executable$exec_ext"; then 81 curr_executable="$dir/$curr_executable$exec_ext" 82 break 2 83 fi 84 done 85 done 86 IFS="$save_IFS" 87 ;; 88 esac 89 # Make absolute. 90 case "$curr_executable" in 91 /* | ?:/* | ?:\\*) ;; 92 *) curr_executable=`pwd`/"$curr_executable" ;; 93 esac 94 # Resolve symlinks. 95 sed_dirname='s,/[^/]*$,,' 96 sed_linkdest='s,^.* -> \(.*\),\1,p' 97 while : ; do 98 lsline=`LC_ALL=C ls -l "$curr_executable"` 99 case "$lsline" in 100 *" -> "*) 101 linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"` 102 case "$linkdest" in 103 /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;; 104 *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;; 105 esac ;; 106 *) break ;; 107 esac 108 done 109 curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'` 110 # Canonicalize. 111 curr_installdir=`cd "$curr_installdir" && pwd` 112} 113func_find_prefixes () 114{ 115 # Compute the original/current installation prefixes by stripping the 116 # trailing directories off the original/current installation directories. 117 orig_installprefix="$orig_installdir" 118 curr_installprefix="$curr_installdir" 119 while true; do 120 orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'` 121 curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'` 122 if test -z "$orig_last" || test -z "$curr_last"; then 123 break 124 fi 125 if test "$orig_last" != "$curr_last"; then 126 break 127 fi 128 orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'` 129 curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'` 130 done 131} 132