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.
7 # Copyright 2003 Alexandre Oliva <aoliva@redhat.com>
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.
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.
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
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,/*$,,'`
27 case $from in /* | "") ;; *) from=`${PWD_CMD-pwd}`/$from ;; esac
28 case $target in /* | "") ;; *) target=`${PWD_CMD-pwd}`/$target ;; esac
32 dots=`echo $from | sed s,.,.,g`
33 echo $target | sed "s,^$dots/*,,;s,[^/]$,&/,"
40 dots=`echo $target | sed s,.,.,g`
41 echo $from/ | sed "s,^$dots/*,,;s,[^/]$,&/,;s,[^/]*/*,../,g;s,[^/]$,&/,"
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,,"`
51 from=`echo $from | sed 's,[^/][^/]*,..,g;s,.$,&/,'`