blob: a57f601a900abb244710b1725e2a20c6cb09d6cd [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001#!/bin/sh
2# rellns-sh - Simplified ln program to generate relative symbolic link.
3# Copyright (C) 1996-2016 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
33# Make both paths absolute.
34if test -d $1; then
35 to=`cd $1 && pwd -P`
36else
37 temp=`echo $1 | sed 's%/*[^/]*$%%'`
38 if test -z "$temp"; then
39 to=`pwd -P`
40 else
41 to=`cd $temp && pwd -P`
42 fi
43 to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`"
44fi
45to=`echo $to | sed 's%^/%%'`
46
47if test -d $2; then
48 from=`echo $2 | sed 's%/*$%%'`
49else
50 from=`echo $2 | sed 's%/*[^/]*$%%'`
51fi
52
53if test -z "$from"; then
54 from=`pwd -P | sed 's%^/%%'`
55else
56 from=`cd $from && pwd -P | sed 's%^/%%'`
57fi
58
59while test -n "$to" && test -n "$from"; do
60 preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'`
61 prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'`
62
63 test "$preto" != "$prefrom" && break
64
65 to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'`
66 from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'`
67done
68
69while test -n "$from"; do
70 rfrom="../$rfrom"
71 from=`echo $from | sed 's%^[^/]*/*%%'`
72done
73
74if $do_print; then
75 echo "$rfrom$to"
76else
77 ln -s $rfrom$to $2
78fi