| 1 | #!/bin/sh |
| 2 | # rellns-sh - Simplified ln program to generate relative symbolic link. |
| 3 | # Copyright (C) 1996-2026 Free Software Foundation, Inc. |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2, or (at your option) |
| 8 | # any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 17 | |
| 18 | # With -p, instead of creating the link print the computed relative link |
| 19 | # name. |
| 20 | do_print=false |
| 21 | case $1 in |
| 22 | -p) |
| 23 | do_print=true |
| 24 | shift |
| 25 | ;; |
| 26 | esac |
| 27 | if test $# -ne 2; then |
| 28 | echo "Usage: rellns [-p] SOURCE DEST" >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | # Make both paths absolute. |
| 33 | if test -d $1; then |
| 34 | to=`cd $1 && pwd -P` |
| 35 | else |
| 36 | temp=`echo $1 | sed 's%/*[^/]*$%%'` |
| 37 | if test -z "$temp"; then |
| 38 | to=`pwd -P` |
| 39 | else |
| 40 | to=`cd $temp && pwd -P` |
| 41 | fi |
| 42 | to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`" |
| 43 | fi |
| 44 | to=`echo $to | sed 's%^/%%'` |
| 45 | |
| 46 | if test -d $2; then |
| 47 | from=`echo $2 | sed 's%/*$%%'` |
| 48 | else |
| 49 | from=`echo $2 | sed 's%/*[^/]*$%%'` |
| 50 | fi |
| 51 | |
| 52 | if test -z "$from"; then |
| 53 | from=`pwd -P | sed 's%^/%%'` |
| 54 | else |
| 55 | from=`cd $from && pwd -P | sed 's%^/%%'` |
| 56 | fi |
| 57 | |
| 58 | while test -n "$to" && test -n "$from"; do |
| 59 | preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'` |
| 60 | prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'` |
| 61 | |
| 62 | test "$preto" != "$prefrom" && break |
| 63 | |
| 64 | to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'` |
| 65 | from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'` |
| 66 | done |
| 67 | |
| 68 | while test -n "$from"; do |
| 69 | rfrom="../$rfrom" |
| 70 | from=`echo $from | sed 's%^[^/]*/*%%'` |
| 71 | done |
| 72 | |
| 73 | if $do_print; then |
| 74 | echo "$rfrom$to" |
| 75 | else |
| 76 | ln -s $rfrom$to $2 |
| 77 | fi |