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-2014 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 # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which 66 # contains only /bin. Note that ksh looks also at the FPATH variable, 67 # so we have to set that as well for the test. 68 PATH_SEPARATOR=: 69 (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \ 70 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \ 71 || PATH_SEPARATOR=';' 72 } 73 fi 74 save_IFS="$IFS"; IFS="$PATH_SEPARATOR" 75 for dir in $PATH; do 76 IFS="$save_IFS" 77 test -z "$dir" && dir=. 78 for exec_ext in ''; do 79 if test -f "$dir/$curr_executable$exec_ext"; then 80 curr_executable="$dir/$curr_executable$exec_ext" 81 break 2 82 fi 83 done 84 done 85 IFS="$save_IFS" 86 ;; 87 esac 88 # Make absolute. 89 case "$curr_executable" in 90 /* | ?:/* | ?:\\*) ;; 91 *) curr_executable=`pwd`/"$curr_executable" ;; 92 esac 93 # Resolve symlinks. 94 sed_dirname='s,/[^/]*$,,' 95 sed_linkdest='s,^.* -> \(.*\),\1,p' 96 while : ; do 97 lsline=`LC_ALL=C ls -l "$curr_executable"` 98 case "$lsline" in 99 *" -> "*) 100 linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"` 101 case "$linkdest" in 102 /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;; 103 *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;; 104 esac ;; 105 *) break ;; 106 esac 107 done 108 curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'` 109 # Canonicalize. 110 curr_installdir=`cd "$curr_installdir" && pwd` 111} 112func_find_prefixes () 113{ 114 # Compute the original/current installation prefixes by stripping the 115 # trailing directories off the original/current installation directories. 116 orig_installprefix="$orig_installdir" 117 curr_installprefix="$curr_installdir" 118 while true; do 119 orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'` 120 curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'` 121 if test -z "$orig_last" || test -z "$curr_last"; then 122 break 123 fi 124 if test "$orig_last" != "$curr_last"; then 125 break 126 fi 127 orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'` 128 curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'` 129 done 130} 131