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
9 # Copyright (C) 2003, 2005-2007 Free Software Foundation, Inc.
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 2, or (at your option)
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.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 # creates a temporary directory.
29 # - tmp pathname of freshly created temporary directory
32 # Use the environment variable TMPDIR, falling back to /tmp. This allows
33 # users to specify a different temporary directory, for example, if their
34 # /tmp is filled up or too small.
37 # Use the mktemp program if available. If not available, hide the error
39 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
40 test -n "$tmp" && test -d "$tmp"
43 # Use a simple mkdir command. It is guaranteed to fail if the directory
44 # already exists. $RANDOM is bash specific and expands to empty in shells
45 # other than bash, ksh and zsh. Its use does not increase security;
46 # rather, it minimizes the probability of failure in a very cluttered /tmp
48 tmp=$TMPDIR/gl$$-$RANDOM
49 (umask 077 && mkdir "$tmp")
52 echo "$0: cannot create a temporary directory in $TMPDIR" >&2
57 # Support for relocatability.
58 func_find_curr_installdir ()
60 # Determine curr_installdir, even taking into account symlinks.
62 case "$curr_executable" in
64 *) # Need to look in the PATH.
65 if test "${PATH_SEPARATOR+set}" != set; then
67 { echo "#! /bin/sh"; echo "exit 0"; } > "$tmp"/conf.sh
68 chmod +x "$tmp"/conf.sh
69 if (PATH="/nonexistent;$tmp"; conf.sh) >/dev/null 2>&1; then
76 save_IFS="$IFS"; IFS="$PATH_SEPARATOR"
79 test -z "$dir" && dir=.
80 for exec_ext in ''; do
81 if test -f "$dir/$curr_executable$exec_ext"; then
82 curr_executable="$dir/$curr_executable$exec_ext"
91 case "$curr_executable" in
93 *) curr_executable=`pwd`/"$curr_executable" ;;
96 sed_dirname='s,/[^/]*$,,'
97 sed_linkdest='s,^.* -> \(.*\),\1,p'
99 lsline=`LC_ALL=C ls -l "$curr_executable"`
102 linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
104 /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
105 *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
110 curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
112 curr_installdir=`cd "$curr_installdir" && pwd`
114 func_find_prefixes ()
116 # Compute the original/current installation prefixes by stripping the
117 # trailing directories off the original/current installation directories.
118 orig_installprefix="$orig_installdir"
119 curr_installprefix="$curr_installdir"
121 orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
122 curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
123 if test -z "$orig_last" || test -z "$curr_last"; then
126 if test "$orig_last" != "$curr_last"; then
129 orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
130 curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`