summaryrefslogtreecommitdiff
path: root/libdde_linux26/mk/rel2abs.sh
blob: 33dd8a5b7ce08920f2dbda65791a91a9664fbf6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /bin/bash
#
# Convert relative path to absolute one
#
# Adam Lackorzynski <adam@os.inf.tu-dresden.de>
#

help()
{
  echo PWD=\$PWD $0 relpath1 [relpath2 [..]]
  exit $1
}

convertpath()
{
  relpath=$1
  basepath=$PWD
  # sanity checks
  [ -z "$relpath" -o -z "$basepath" ] && help 1
  [ "${basepath#/}" = "${basepath}" ] && help 1
  [ "${basepath/\/..\//}" = "${basepath}" ] || help 1
  [ "${basepath/\/.\//}" = "${basepath}" ] || help 1
  [ "${basepath/%\/../}" = "${basepath}" ] || help 1
  [ "${basepath/%\/./}" = "${basepath}" ] || help 1


  # remove slashes at the end
  while [ "${relpath%/}" != "${relpath}" ];
  do relpath="${relpath%/}"; done

  # remove double/multi slashes
  while [ "${relpath/\/\///}" != "${relpath}" ];
  do relpath=${relpath/\/\///}; done

  # is relpath relative?
  if [ "${relpath#/}" != "${relpath}" ]; then
    basepath=''
    relpath=${relpath#/}
  fi

  relpath="$relpath/"

  while [ -n "$relpath" ];
  do
    elem=${relpath%%/*}
    relpath=${relpath#*/}

    case $elem in
      .) # skip
	;;
      ..)
	 basepath=${basepath%/*}
	;;
      *)
	 basepath=$basepath/$elem
	;;
    esac

  done

  [ -z "$basepath" ] && basepath=/$basepath

  echo $basepath
}

while [ -n "$1" ];
do
  convertpath $1
  shift
done

exit 0