]>
Commit | Line | Data |
---|---|---|
48d0341c | 1 | #!/bin/sh |
f0e44959 | 2 | # rellns-sh - Simplified ln program to generate relative symbolic link. |
688903eb | 3 | # Copyright (C) 1996-2018 Free Software Foundation, Inc. |
f0e44959 UD |
4 | # Written by Ulrich Drepper <drepper@cygnus.com>, October 1996 |
5 | # | |
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, or (at your option) | |
9 | # any later version. | |
10 | # | |
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. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License | |
59ba27a6 | 17 | # along with this program; if not, see <http://www.gnu.org/licenses/>. |
f0e44959 | 18 | |
aaa8cb4b AS |
19 | # With -p, instead of creating the link print the computed relative link |
20 | # name. | |
21 | do_print=false | |
22 | case $1 in | |
23 | -p) | |
24 | do_print=true | |
25 | shift | |
26 | ;; | |
27 | esac | |
f0e44959 | 28 | if test $# -ne 2; then |
aaa8cb4b | 29 | echo "Usage: rellns [-p] SOURCE DEST" >&2 |
68dbb3a6 UD |
30 | exit 1 |
31 | fi | |
32 | ||
c131718c UD |
33 | # Make both paths absolute. |
34 | if test -d $1; then | |
db340c90 | 35 | to=`cd $1 && pwd -P` |
c131718c UD |
36 | else |
37 | temp=`echo $1 | sed 's%/*[^/]*$%%'` | |
b4012b75 | 38 | if test -z "$temp"; then |
db340c90 | 39 | to=`pwd -P` |
b4012b75 | 40 | else |
db340c90 | 41 | to=`cd $temp && pwd -P` |
b4012b75 | 42 | fi |
c131718c UD |
43 | to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`" |
44 | fi | |
45 | to=`echo $to | sed 's%^/%%'` | |
f0e44959 | 46 | |
c131718c UD |
47 | if test -d $2; then |
48 | from=`echo $2 | sed 's%/*$%%'` | |
49 | else | |
50 | from=`echo $2 | sed 's%/*[^/]*$%%'` | |
51 | fi | |
f0e44959 | 52 | |
c131718c | 53 | if test -z "$from"; then |
db340c90 | 54 | from=`pwd -P | sed 's%^/%%'` |
63551311 | 55 | else |
db340c90 | 56 | from=`cd $from && pwd -P | sed 's%^/%%'` |
c131718c | 57 | fi |
f0e44959 | 58 | |
68dbb3a6 UD |
59 | while test -n "$to" && test -n "$from"; do |
60 | preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'` | |
61 | prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'` | |
f0e44959 | 62 | |
68dbb3a6 | 63 | test "$preto" != "$prefrom" && break |
f0e44959 | 64 | |
68dbb3a6 UD |
65 | to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'` |
66 | from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'` | |
67 | done | |
f0e44959 | 68 | |
68dbb3a6 UD |
69 | while test -n "$from"; do |
70 | rfrom="../$rfrom" | |
71 | from=`echo $from | sed 's%^[^/]*/*%%'` | |
72 | done | |
73 | ||
aaa8cb4b AS |
74 | if $do_print; then |
75 | echo "$rfrom$to" | |
76 | else | |
77 | ln -s $rfrom$to $2 | |
78 | fi |