]>
Commit | Line | Data |
---|---|---|
f0e44959 UD |
1 | #! /bin/sh |
2 | # rellns-sh - Simplified ln program to generate relative symbolic link. | |
b168057a | 3 | # Copyright (C) 1996-2015 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 | ||
63a34b0f UD |
33 | if test -x /bin/pwd; then |
34 | pwd=/bin/pwd | |
35 | elif test -x /usr/bin/pwd; then | |
36 | pwd=/usr/bin/pwd | |
37 | else | |
38 | pwd='pwd' | |
39 | fi | |
40 | ||
c131718c UD |
41 | # Make both paths absolute. |
42 | if test -d $1; then | |
63a34b0f | 43 | to=`cd $1 && $pwd` |
c131718c UD |
44 | else |
45 | temp=`echo $1 | sed 's%/*[^/]*$%%'` | |
b4012b75 | 46 | if test -z "$temp"; then |
63a34b0f | 47 | to=`$pwd` |
b4012b75 | 48 | else |
63a34b0f | 49 | to=`cd $temp && $pwd` |
b4012b75 | 50 | fi |
c131718c UD |
51 | to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`" |
52 | fi | |
53 | to=`echo $to | sed 's%^/%%'` | |
f0e44959 | 54 | |
c131718c UD |
55 | if test -d $2; then |
56 | from=`echo $2 | sed 's%/*$%%'` | |
57 | else | |
58 | from=`echo $2 | sed 's%/*[^/]*$%%'` | |
59 | fi | |
f0e44959 | 60 | |
c131718c | 61 | if test -z "$from"; then |
63a34b0f | 62 | from=`$pwd | sed 's%^/%%'` |
63551311 | 63 | else |
63a34b0f | 64 | from=`cd $from && $pwd | sed 's%^/%%'` |
c131718c | 65 | fi |
f0e44959 | 66 | |
68dbb3a6 UD |
67 | while test -n "$to" && test -n "$from"; do |
68 | preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'` | |
69 | prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'` | |
f0e44959 | 70 | |
68dbb3a6 | 71 | test "$preto" != "$prefrom" && break |
f0e44959 | 72 | |
68dbb3a6 UD |
73 | to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'` |
74 | from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'` | |
75 | done | |
f0e44959 | 76 | |
68dbb3a6 UD |
77 | while test -n "$from"; do |
78 | rfrom="../$rfrom" | |
79 | from=`echo $from | sed 's%^[^/]*/*%%'` | |
80 | done | |
81 | ||
aaa8cb4b AS |
82 | if $do_print; then |
83 | echo "$rfrom$to" | |
84 | else | |
85 | ln -s $rfrom$to $2 | |
86 | fi |