aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorUlrich Müller <ulm@gentoo.org>2009-08-13 17:55:48 +0000
committerUlrich Müller <ulm@gentoo.org>2009-08-13 17:55:48 +0000
commitf54d8e52215bb1ef6e6c8342384f72d8444cc120 (patch)
tree0c1bfb759784d178783fed484525366a5694e98b /libs
parentAlign columns in do_list output. (diff)
downloadeselect-f54d8e52215bb1ef6e6c8342384f72d8444cc120.tar.gz
eselect-f54d8e52215bb1ef6e6c8342384f72d8444cc120.tar.bz2
eselect-f54d8e52215bb1ef6e6c8342384f72d8444cc120.zip
POSIX compliant basename and dirname functions, bug 280598.
svn path=/trunk/; revision=603
Diffstat (limited to 'libs')
-rw-r--r--libs/path-manipulation.bash.in44
1 files changed, 40 insertions, 4 deletions
diff --git a/libs/path-manipulation.bash.in b/libs/path-manipulation.bash.in
index b400a98..f08290e 100644
--- a/libs/path-manipulation.bash.in
+++ b/libs/path-manipulation.bash.in
@@ -14,14 +14,50 @@
# You should have received a copy of the GNU General Public License along with
# eselect. If not, see <http://www.gnu.org/licenses/>.
-# basename wrapper
+# basename and dirname functions
+# transparent bash-only replacements for the external commands
+# extended pattern matching (shopt -s extglob) is required
basename() {
- echo "${1##*/}"
+ local path=$1 suf=$2
+
+ if [[ -z ${path} ]]; then
+ echo
+ return
+ fi
+
+ # remove any trailing slashes
+ path=${path%%*(/)}
+
+ # remove everything up to and including the last slash
+ path=${path##*/}
+
+ # remove any suffix
+ [[ ${suf} != "${path}" ]] && path=${path%"${suf}"}
+
+ # output the result, or "/" if we ended up with a null string
+ echo "${path:-/}"
}
-# dirname wrapper
dirname() {
- echo "${1%/*}"
+ local path=$1
+
+ if [[ -z ${path} ]]; then
+ echo .
+ return
+ fi
+
+ # remove any trailing slashes
+ path=${path%%*(/)}
+
+ # if the path contains only non-slash characters, then dirname is cwd
+ [[ ${path:-/} != */* ]] && path=.
+
+ # remove any trailing slashes followed by non-slash characters
+ path=${path%/*}
+ path=${path%%*(/)}
+
+ # output the result, or "/" if we ended up with a null string
+ echo "${path:-/}"
}
canonicalise() {