nstrftime: use clearer code for padding
[gnulib.git] / build-aux / relocatable.sh.in
blobcce37a12977e551a5228df5d64c5b9654972db70
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.
9 # Copyright (C) 2003-2024 Free Software Foundation, Inc.
11 # This file is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published
13 # by the Free Software Foundation; either version 2 of the License,
14 # or (at your option) any later version.
16 # This file 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 <https://www.gnu.org/licenses/>.
25 # Support for relocatability.
26 func_find_curr_installdir ()
28 # Determine curr_installdir, even taking into account symlinks.
29 curr_executable="$0"
30 case "$curr_executable" in
31 */* | *\\*) ;;
32 *) # Need to look in the PATH.
33 if test "${PATH_SEPARATOR+set}" != set; then
34 # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which
35 # contains only /bin. Note that ksh looks also at the FPATH variable,
36 # so we have to set that as well for the test.
37 PATH_SEPARATOR=:
38 (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
39 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
40 || PATH_SEPARATOR=';'
43 saved_IFS="$IFS"; IFS="$PATH_SEPARATOR"
44 for dir in $PATH; do
45 IFS="$saved_IFS"
46 test -z "$dir" && dir=.
47 for exec_ext in ''; do
48 if test -f "$dir/$curr_executable$exec_ext"; then
49 curr_executable="$dir/$curr_executable$exec_ext"
50 break 2
52 done
53 done
54 IFS="$saved_IFS"
56 esac
57 # Make absolute.
58 case "$curr_executable" in
59 /* | ?:/* | ?:\\*) ;;
60 *) curr_executable=`pwd`/"$curr_executable" ;;
61 esac
62 # Resolve symlinks.
63 sed_dirname='s,/[^/]*$,,'
64 sed_linkdest='s,^.* -> \(.*\),\1,p'
65 while : ; do
66 lsline=`LC_ALL=C ls -l "$curr_executable"`
67 case "$lsline" in
68 *" -> "*)
69 linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
70 case "$linkdest" in
71 /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
72 *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
73 esac ;;
74 *) break ;;
75 esac
76 done
77 curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
78 # Canonicalize.
79 curr_installdir=`cd "$curr_installdir" && pwd`
81 func_find_prefixes ()
83 # Compute the original/current installation prefixes by stripping the
84 # trailing directories off the original/current installation directories.
85 orig_installprefix="$orig_installdir"
86 curr_installprefix="$curr_installdir"
87 while true; do
88 orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
89 curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
90 if test -z "$orig_last" || test -z "$curr_last"; then
91 break
93 if test "$orig_last" != "$curr_last"; then
94 break
96 orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
97 curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`
98 done