1 #!/bin/sh 2 # 3 # File functions utilized as part of abspath.sh, realpath.sh, etc. 4 # 5 # Copyright (C) 2010, Cisco Systems Inc. 6 # 7 # This program is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # This program is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License along 18 # with this program; if not, write to the Free Software Foundation, Inc., 19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 # 21 # Garrett Cooper, January 2010 22 # 23 # POSIX compliant bourne shell functions for performing make 3.81 24 # compliancy in 3.80 with a minimal set of external commands 25 # [awk(1) // readlink(1) only required]. 26 # 27 28 # 0. Strip all heading and leading space. 29 # Paths: 30 # 1. Empty string - print out $PWD. 31 # 2. Not empty string... 32 # i. Prefix all relative paths with $PWD. 33 # ii. Replace /+ with /. 34 # iii. Replace a/b/../c with a/c 35 # iv. Replace /./ with / 36 # v. Replace trailing /. with / 37 # vi. Replace heading ./ with / 38 # vii. Replace /. with "". 39 40 # testcases/kernel/controllers/libcontrollers/../../../.. 41 _abspath() { 42 echo "$@" | awk -v PWD=$(pwd) '{ 43 sub(/^[[:space:]]+/, ""); sub(/[[:space:]]+$/, ""); # 1. 44 if ($0 == "") { 45 print PWD 46 } else { 47 if (!($0 ~ /^\//)) { # i. 48 $0 = PWD "/" $0 49 } 50 while (gsub(/\/\//, "/")) { }; # ii. 51 while (sub(/\/[^\/]+\/\.\.\/?/, "/")) { }; # iii. 52 while (sub(/\/\.\//, "/")) { }; # iv. 53 sub(/(\/\.)?\/$/, ""); 54 sub(/^\.\//, "/"); 55 sub(/\/\.$/, ""); 56 if ($0 == "") { 57 print "/" 58 } else { 59 if ($0 == ".") { 60 print PWD 61 } else { 62 print 63 } 64 } 65 } 66 }' 67 } 68 69 _realpath() { 70 readlink -f "$@" 71 } 72