blob: 4dddefac143814eab5d8258408626bc2909cc6e8 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#! /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
14from=`(cd $1 > /dev/null && ${PWD_CMD-pwd} || echo $1) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
15target=`(cd $2 > /dev/null && ${PWD_CMD-pwd} || echo $2) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
16
17case $from in /* | "") ;; *) from=`${PWD_CMD-pwd}`/$from ;; esac
18case $target in /* | "") ;; *) target=`${PWD_CMD-pwd}`/$target ;; esac
19
20case $target in
21"$from" | "$from/"*)
22 dots=`echo $from | sed s,.,.,g`
23 echo $target | sed "s,^$dots/*,,;s,[^/]$,&/,"
24 exit 0
25 ;;
26esac
27
28case $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 ;;
34esac
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,'`
39prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*/\).*///\1.*,\1,'`
40dots=`echo $prefix | sed s,.,.,g`
41from=`echo $from | sed "s,^$dots,,"`
42target=`echo $target | sed "s,^$dots,,"`
43
44from=`echo $from | sed 's,[^/][^/]*,..,g;s,.$,&/,'`
45echo ${from}$target/
46
47exit 0