aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorElie De Brauwer <eliedebrauwer@gmail.com>2012-02-12 14:14:58 +0100
committerElie De Brauwer <eliedebrauwer@gmail.com>2012-02-12 14:14:58 +0100
commit528e3ba874f0d89f5a4d1f8ce8c04adbaa2ec1e4 (patch)
tree456c2a52361df5944bbe58a9a3121852e9d6e2a0 /toys
parent2bd3a5df73723ef3aa274aab2d18de0ad9b31e25 (diff)
downloadtoybox-528e3ba874f0d89f5a4d1f8ce8c04adbaa2ec1e4.tar.gz
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
Diffstat (limited to 'toys')
-rw-r--r--toys/basename.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/toys/basename.c b/toys/basename.c
index ef571032..a6a3bb96 100644
--- a/toys/basename.c
+++ b/toys/basename.c
@@ -15,7 +15,7 @@ config BASENAME
help
usage: basename string [suffix]
- Return non-directory portion of a pathname
+ Return non-directory portion of a pathname removing suffix
*/
#include "toys.h"
@@ -37,9 +37,11 @@ void basename_main(void)
// chop off the suffix if provided
if (suffix) {
- char *s = strstr(base, suffix);
- if (s && s != base) *s = 0;
+ int suflen = strlen(suffix);
+ int reslen = strlen(base);
+ if (suflen < reslen && !strcmp( base+reslen-suflen, suffix))
+ base[reslen-suflen] = 0;
}
-
+
puts(base);
}