update from main archive 961101
[platform/upstream/glibc.git] / rellns-sh
1 #! /bin/sh
2 # rellns-sh - Simplified ln program to generate relative symbolic link.
3 # Copyright (C) 1996 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, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 if test $# -ne 2; then
21   echo "Usage: rellns SOURCE DEST"
22   exit 1
23 fi
24
25 case $1 in
26 /*)
27   # Make both paths absolute.
28   to=`echo $1 | sed 's%^/%%'`
29
30   if test -d $2; then
31     fromname=.
32     from=`echo $2 | sed 's%/$%%' | sed 's%^/%%'`
33   else
34     fromname=`echo $2 | sed 's%.*/\([^/]*\)$%\1%'`
35     from=`echo $2 | sed "s%/*$fromname$%%" | sed 's%^/%%'`
36   fi
37
38   case $from in
39   /*) ;;
40   ?*) from=`cd $from && pwd | sed 's%^/%%'` ;;
41   *) from=`pwd | sed 's%^/%%'` ;;
42   esac
43
44   while test -n "$to" && test -n "$from"; do
45     preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'`
46     prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'`
47
48     test "$preto" != "$prefrom" && break
49
50     to=`echo $to | sed 's%^[^/]*/\(.*\)$%\1%'`
51     from=`echo $from | sed 's%^[^/]*/\(.*\)$%\1%'`
52   done
53
54   while test -n "$from"; do
55     rfrom="../$rfrom"
56     from=`echo $from | sed 's%^[^/]*/*%%'`
57   done
58
59   ln -s $rfrom$to $2
60   ;;
61 *)
62   # Nothing to do, the path is already relative.
63   ln -s $1 $2
64   ;;
65 esac
66
67 exit 0