OSDN Git Service

Alexandre Oliva writes:
[uclinux-h8/uClibc.git] / extra / scripts / relative_path.sh
1 #! /bin/sh
2 # This script computes a relative pathname from $1 to $2
3 # They are assumed to not contain . or .. pathname components,
4 # but if both directories exist and cd/pwd canonicalizes pathnames,
5 # this shouldn't matter.  PWD_CMD may be set to some pwd command that does.
6
7 # Copyright 2003 Alexandre Oliva <aoliva@redhat.com>
8
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation, Inc.,
21 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 from=`(cd $1 > /dev/null && ${PWD_CMD-pwd} || echo $1) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
25 target=`(cd $2 > /dev/null && ${PWD_CMD-pwd} || echo $2) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
26
27 case $from in /* | "") ;; *) from=`${PWD_CMD-pwd}`/$from ;; esac
28 case $target in /* | "") ;; *) target=`${PWD_CMD-pwd}`/$target ;; esac
29
30 case $target in
31 "$from" | "$from/"*)
32   dots=`echo $from | sed s,.,.,g`
33   echo $target | sed "s,^$dots/*,,;s,[^/]$,&/,"
34   exit 0
35   ;;
36 esac
37
38 case $from in
39 "$target/"*)
40   dots=`echo $target | sed s,.,.,g`
41   echo $from/ | sed "s,^$dots/*,,;s,[^/]$,&/,;s,[^/]*/*,../,g;s,[^/]$,&/,"
42   exit 0
43   ;;
44 esac
45
46 prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*\).*///\1.*,\1,'`
47 dots=`echo $prefix | sed s,.,.,g`
48 from=`echo $from | sed "s,^$dots,,"`
49 target=`echo $target | sed "s,^$dots,,"`
50
51 from=`echo $from | sed 's,[^/][^/]*,..,g;s,.$,&/,'`
52 echo ${from}$target/
53
54 exit 0