3 # Copyright © 2020 Helmut Grohne <helmut@subdivi.de>
4 # Copyright © 2020 Guillem Jover <guillem@debian.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 PROGNAME
=$
(basename "$0")
25 PKGDATADIR_DEFAULT
=src
26 PKGDATADIR
="${DPKG_DATADIR:-$PKGDATADIR_DEFAULT}"
28 # shellcheck source=src/sh/dpkg-error.sh
29 .
"$PKGDATADIR/sh/dpkg-error.sh"
34 Debian $PROGNAME version $version.
36 This is free software; see the GNU General Public License version 2 or
37 later for copying conditions. There is NO warranty.
44 Usage: $PROGNAME [<option>...] <pathname>
47 -z, --zero end output line with NUL, not newline.
48 --instdir <directory> set the root directory.
49 --root <directory> set the root directory.
50 --version show the version.
51 -?, --help show this help message.
57 local root
="$DPKG_ROOT"
62 # Check whether the path is relative and make it absolute otherwise.
63 if [ "$src" = "${src#/}" ]; then
68 # Remove prefixed slashes.
69 while [ "$src" != "${src#/}" ]; do
72 while [ -n "$src" ]; do
73 # Get the first directory component.
75 # Remove the first directory component from src.
77 # Remove prefixed slashes.
78 while [ "$src" != "${src#/}" ]; do
81 # Resolve the first directory component.
82 if [ "$prefix" = .
]; then
83 # Ignore, stay at the same directory.
85 elif [ "$prefix" = ..
]; then
86 # Go up one directory.
88 if [ -n "$root" ] && [ "${result#"$root"}" = "$result" ]; then
91 elif [ -h "$result/$prefix" ]; then
93 if [ "$loop" -gt 25 ]; then
94 error
"too many levels of symbolic links"
96 # Resolve the symlink within $result.
97 dst
=$
(readlink
"$result/$prefix")
100 # Absolute pathname, reset result back to $root.
102 src
="$dst${src:+/$src}"
103 # Remove prefixed slashes.
104 while [ "$src" != "${src#/}" ]; do
110 src
="$dst${src:+/$src}"
114 # Otherwise append the prefix.
115 result
="$result/$prefix"
118 # We are done, print the resolved pathname, w/o $root.
119 result
="${result#"$root"}"
120 printf "%s$EOL" "${result:-/}"
125 DPKG_ROOT
="${DPKG_ROOT:-}"
128 while [ $# -ne 0 ]; do
138 DPKG_ROOT
="${1#--instdir=}"
141 DPKG_ROOT
="${1#--root=}"
156 badusage
"unknown option: $1"
165 # Normalize root directory.
166 DPKG_ROOT
="${DPKG_ROOT:+$(realpath "$DPKG_ROOT")}"
167 # Remove default root dir.
168 if [ "$DPKG_ROOT" = "/" ]; then
172 [ -n "$pathname" ] || badusage
"missing pathname"
173 if [ "${pathname#"$DPKG_ROOT"}" != "$pathname" ]; then
174 error
"link '$pathname' includes root prefix '$DPKG_ROOT'"
177 canonicalize
"$pathname"