#!/usr/bin/env bash # rdlink [OPTIONS] # [OPTION] # -- : End of options marker # -* : Other options are ignored # # License: GNU GPL V3 or later # Author: Martin Winkler # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . rl::rdlink() { local subject= local work= subject="$(rl::cleanpath "${1:-}")" || true # Follow multiple symlinks while subject="$(rl::quicklink "${subject}")" ; do : # A link was resolved at least once done # Special cases handling { # If subject is still a link, after rl::quicklink call(s) # current user has no permission to access the link itself # (e.g. /proc/**/cwd) if [ -L "${subject}" ] ; then printf "\n" return 1 fi } # Empty output if (dirname $subject) is not a valid path if ! work="$(rl::canon "${subject}")" ; then printf "\n" return 1 else subject="${work}" fi printf "%s\n" "${subject}" } rl::quicklink() { subject= work= # Check if current candidate is a symlink if ! subject=$(readlink -- "${1:-}"); then printf -- "%s\n" "${1:-}" return 1 fi # relative symlink target; prepend its parent direcotry if [[ "${subject}" != "/"* ]]; then work="$(rl::canon "$(dirname -- "${1:-}")")" subject="${work}/${subject}" fi printf "%s\n" "${subject}" return 0 } rl::canon() { local subject= local work= local bname= local run=1 local start= local retval=0 start="${1:-}" while (( run )) ; do if work="$(cd "${start}" >/dev/null 2>&1 && pwd -P)" ; then # Special: `pwd -P` returns with // as root for links starting at / # e.g. $(readlink /proc/self/root) == "/" # $(cd /proc/self/root/mnt && pwd -P) == "//mnt" subject="$(rl::normalize "${work}")" run=0 elif work="$(cd "$(dirname -- "${start}")" >/dev/null 2>&1 && pwd -P)" ; then bname="$(basename -- "${start}")" # Special: / produces // [[ "${work}" == "/" ]] && work= subject="${work}${bname:+"/${bname}"}" # Special: Succeed with valid element after second run; see special below # e.g. /root/. # * /root is valid but not accessible if (( retval )) && [ -e "${subject}" ] ;then retval=0 fi run=0 else # Special: Some paths may be resolvable after normalization # e.g. somedir/.. # * base "somedir" does not exist # * but irrelevant because of /.. # * resolves to pwd but fails by readlink -f work="$(rl::normalize "${start}")" if [[ "${work}" != "${start}" ]] ; then start="${work}" retval=1 continue fi subject="${work}" run=0 && retval=1 fi done printf -- "%s\n" "${subject}" return ${retval} } rl::cleanpath() { local work= local rex_tmp= work="${1:-}" # Remove multiple / while [[ "${work:-}" = *"//"* ]]; do work="${work//'//'/'/'}" done printf -- "%s\n" "${work}" } rl::normalize() { local work= work="${1:-}" # Remove dir/.. sequences. local rex_tmp='[^/][^/]*/\.\./*' while [[ "${work}" =~ $rex_tmp ]] ; do work="${work/"${BASH_REMATCH[0]}"/}" done # Remove /./ and /.$ sequences. rex_tmp='/\.(/|$)' while [[ "$work" =~ $rex_tmp ]]; do work="${work/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]/$//}"}" done # Remove leading ./ work="${work#./*}" # Remove trailing / rex_tmp='(.*[^/])/$' if [[ "${work}" =~ $rex_tmp ]] ; then work="${BASH_REMATCH[1]}" fi printf -- "%s\n" "${work}" } rl::main() { local file= local arg= for arg in "$@"; do case "$arg" in --) shift break ;; -*) shift ;; esac done for file in "$@"; do rl::rdlink "$file" done } # Provide as function to be called when sourced rdlink() { rl::main "$@" } ### Check if script is _sourced ### https://stackoverflow.com/a/28776166 _sourced=0 if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then case ${ZSH_EVAL_CONTEXT:-} in *:file) _sourced=1;; esac elif [ -n "${KSH_VERSION:-}" ]; then [ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" \ != "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && _sourced=1 elif [ -n "${BASH_VERSION:-}" ]; then (return 0 2>/dev/null) && _sourced=1 else # All other shells: examine $0 for known shell binary filenames # Detects `sh` and `dash`; add additional shell filenames as needed. case ${0##*/} in sh|dash) _sourced=1;; esac fi ### if (( ! _sourced )); then rl::main "$@" fi