Update.
[platform/upstream/glibc.git] / rellns-sh
1 #! /bin/sh
2 # rellns-sh - Simplified ln program to generate relative symbolic link.
3 # Copyright (C) 1996, 1997 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" >&2
22   exit 1
23 fi
24
25 # A problem with this script is that we must be able to handle symbolic
26 # links somewhere in the paths of either path.  To resolve symlinks we use
27 # the `pwd' program.  But some `pwd' programs are no real programs but
28 # instead aliases (defined by the user) or builtins (as in bash-2).  Both
29 # kinds have in common that they might not give the correct result.  E.g.,
30 # the builtin in bash-2 returns the path which was used to change to the
31 # directory and not the real path.
32 #
33 # To prevent this problem we make sure the real `pwd' somewhere in the
34 # path is used.  Currently there is only support for bash-2 available.
35 # If other shells also have problems we have to add more code here.
36
37 if test "$BASH_VERSINFO" = "2"; then
38   unalias pwd
39   unset pwd
40   enable -n pwd
41 fi
42
43 # Make both paths absolute.
44 if test -d $1; then
45   to=`cd $1 && pwd`
46 else
47   temp=`echo $1 | sed 's%/*[^/]*$%%'`
48   to=`cd $temp && pwd`
49   to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`"
50 fi
51 to=`echo $to | sed 's%^/%%'`
52
53 if test -d $2; then
54   from=`echo $2 | sed 's%/*$%%'`
55 else
56   from=`echo $2 | sed 's%/*[^/]*$%%'`
57 fi
58
59 if test -z "$from"; then
60   from=`pwd`;
61 fi
62 from=`cd $from && pwd | sed 's%^/%%'`
63
64 while test -n "$to" && test -n "$from"; do
65   preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'`
66   prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'`
67
68   test "$preto" != "$prefrom" && break
69
70   to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'`
71   from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'`
72 done
73
74 while test -n "$from"; do
75   rfrom="../$rfrom"
76   from=`echo $from | sed 's%^[^/]*/*%%'`
77 done
78
79 ln -s $rfrom$to $2