lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #! /bin/sh |
| 2 | # |
| 3 | # Copyright 2003 Alexandre Oliva <aoliva@redhat.com> |
| 4 | # Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org> |
| 5 | # |
| 6 | # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 7 | # |
| 8 | |
| 9 | # This script computes a relative pathname from $1 to $2 |
| 10 | # They are assumed to not contain . or .. pathname components, |
| 11 | # but if both directories exist and cd/pwd canonicalizes pathnames, |
| 12 | # this shouldn't matter. PWD_CMD may be set to some pwd command that does. |
| 13 | |
| 14 | from=`(cd $1 > /dev/null && ${PWD_CMD-pwd} || echo $1) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'` |
| 15 | target=`(cd $2 > /dev/null && ${PWD_CMD-pwd} || echo $2) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'` |
| 16 | |
| 17 | case $from in /* | "") ;; *) from=`${PWD_CMD-pwd}`/$from ;; esac |
| 18 | case $target in /* | "") ;; *) target=`${PWD_CMD-pwd}`/$target ;; esac |
| 19 | |
| 20 | case $target in |
| 21 | "$from" | "$from/"*) |
| 22 | dots=`echo $from | sed s,.,.,g` |
| 23 | echo $target | sed "s,^$dots/*,,;s,[^/]$,&/," |
| 24 | exit 0 |
| 25 | ;; |
| 26 | esac |
| 27 | |
| 28 | case $from in |
| 29 | "$target/"*) |
| 30 | dots=`echo $target | sed s,.,.,g` |
| 31 | echo $from/ | sed "s,^$dots/*,,;s,[^/]$,&/,;s,[^/]*/*,../,g;s,[^/]$,&/," |
| 32 | exit 0 |
| 33 | ;; |
| 34 | esac |
| 35 | |
| 36 | # Without trailing slash, from=/usr/lib and target=/uclibc/lib |
| 37 | # mistakenly concludes that prefix=/u |
| 38 | #prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*\).*///\1.*,\1,'` |
| 39 | prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*/\).*///\1.*,\1,'` |
| 40 | dots=`echo $prefix | sed s,.,.,g` |
| 41 | from=`echo $from | sed "s,^$dots,,"` |
| 42 | target=`echo $target | sed "s,^$dots,,"` |
| 43 | |
| 44 | from=`echo $from | sed 's,[^/][^/]*,..,g;s,.$,&/,'` |
| 45 | echo ${from}$target/ |
| 46 | |
| 47 | exit 0 |