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, 2009-2011 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 3 of the License, or
14 # (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
26 # creates a temporary directory.
28 # - tmp pathname of freshly created temporary directory
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.
36 # Use the mktemp program if available. If not available, hide the error
38 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
39 test -n "$tmp" && test -d "$tmp"
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
47 tmp=$TMPDIR/gl$$-$RANDOM
48 (umask 077 && mkdir "$tmp")
51 echo "$0: cannot create a temporary directory in $TMPDIR" >&2
56 # Support for relocatability.
57 func_find_curr_installdir ()
59 # Determine curr_installdir, even taking into account symlinks.
61 case "$curr_executable" in
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.
69 (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
70 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
74 save_IFS="$IFS"; IFS="$PATH_SEPARATOR"
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"
89 case "$curr_executable" in
91 *) curr_executable=`pwd`/"$curr_executable" ;;
94 sed_dirname='s,/[^/]*$,,'
95 sed_linkdest='s,^.* -> \(.*\),\1,p'
97 lsline=`LC_ALL=C ls -l "$curr_executable"`
100 linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
102 /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
103 *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
108 curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
110 curr_installdir=`cd "$curr_installdir" && pwd`
112 func_find_prefixes ()
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"
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
124 if test "$orig_last" != "$curr_last"; then
127 orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
128 curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`