]> rtime.felk.cvut.cz Git - l4.git/blob - l4/mk/rel2abs.sh
update
[l4.git] / l4 / mk / rel2abs.sh
1 #! /usr/bin/env bash
2 #
3 # Convert relative path to absolute one
4 #
5 # Adam Lackorzynski <adam@os.inf.tu-dresden.de>
6 #
7
8 help()
9 {
10   echo PWD=\$PWD $0 relpath1 [relpath2 [..]]
11   exit $1
12 }
13
14 convertpath()
15 {
16   relpath=$1
17   basepath=$PWD
18   # sanity checks
19   [ -z "$relpath" -o -z "$basepath" ] && help 1
20   [ "${basepath#/}" = "${basepath}" ] && help 1
21   [ "${basepath/\/..\//}" = "${basepath}" ] || help 1
22   [ "${basepath/\/.\//}" = "${basepath}" ] || help 1
23   [ "${basepath/%\/../}" = "${basepath}" ] || help 1
24   [ "${basepath/%\/./}" = "${basepath}" ] || help 1
25
26
27   # remove slashes at the end
28   while [ "${relpath%/}" != "${relpath}" ];
29   do relpath="${relpath%/}"; done
30
31   # remove double/multi slashes
32   while [ "${relpath/\/\///}" != "${relpath}" ];
33   do relpath=${relpath/\/\///}; done
34
35   # is relpath relative?
36   if [ "${relpath#/}" != "${relpath}" ]; then
37     basepath=''
38     relpath=${relpath#/}
39   fi
40
41   relpath="$relpath/"
42
43   while [ -n "$relpath" ];
44   do
45     elem=${relpath%%/*}
46     relpath=${relpath#*/}
47
48     case $elem in
49       .) # skip
50         ;;
51       ..)
52          basepath=${basepath%/*}
53         ;;
54       *)
55          basepath=$basepath/$elem
56         ;;
57     esac
58
59   done
60
61   [ -z "$basepath" ] && basepath=/$basepath
62
63   echo $basepath
64 }
65
66 while [ -n "$1" ];
67 do
68   convertpath $1
69   shift
70 done
71
72 exit 0