blob: 9132ce77a0e10db95ba049c79a7ad17faed42cc6 [file] [log] [blame]
Googler262be5a2020-02-12 09:01:32 -08001#! /bin/sh
2# rellns-sh - Simplified ln program to generate relative symbolic link.
3# Copyright (C) 1996-2014 Free Software Foundation, Inc.
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
17# along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19# With -p, instead of creating the link print the computed relative link
20# name.
21do_print=false
22case $1 in
23 -p)
24 do_print=true
25 shift
26 ;;
27esac
28if test $# -ne 2; then
29 echo "Usage: rellns [-p] SOURCE DEST" >&2
30 exit 1
31fi
32
33if test -x /bin/pwd; then
34 pwd=/bin/pwd
35elif test -x /usr/bin/pwd; then
36 pwd=/usr/bin/pwd
37else
38 pwd='pwd'
39fi
40
41# Make both paths absolute.
42if test -d $1; then
43 to=`cd $1 && $pwd`
44else
45 temp=`echo $1 | sed 's%/*[^/]*$%%'`
46 if test -z "$temp"; then
47 to=`$pwd`
48 else
49 to=`cd $temp && $pwd`
50 fi
51 to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`"
52fi
53to=`echo $to | sed 's%^/%%'`
54
55if test -d $2; then
56 from=`echo $2 | sed 's%/*$%%'`
57else
58 from=`echo $2 | sed 's%/*[^/]*$%%'`
59fi
60
61if test -z "$from"; then
62 from=`$pwd | sed 's%^/%%'`
63else
64 from=`cd $from && $pwd | sed 's%^/%%'`
65fi
66
67while test -n "$to" && test -n "$from"; do
68 preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'`
69 prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'`
70
71 test "$preto" != "$prefrom" && break
72
73 to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'`
74 from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'`
75done
76
77while test -n "$from"; do
78 rfrom="../$rfrom"
79 from=`echo $from | sed 's%^[^/]*/*%%'`
80done
81
82if $do_print; then
83 echo "$rfrom$to"
84else
85 ln -s $rfrom$to $2
86fi