diff options
-rw-r--r-- | lib/dirtree.c | 8 | ||||
-rwxr-xr-x | tests/find.test | 3 |
2 files changed, 8 insertions, 3 deletions
diff --git a/lib/dirtree.c b/lib/dirtree.c index 37581829..2bd7c404 100644 --- a/lib/dirtree.c +++ b/lib/dirtree.c @@ -85,15 +85,17 @@ char *dirtree_path(struct dirtree *node, int *plen) { struct dirtree *nn; char *path; - int ll, len; + int ii, ll, len; + if (!node->parent) return xstrdup(node->name); ll = len = plen ? *plen : 0; - for (nn = node; nn; nn = nn->parent) if (*nn->name) len += strlen(nn->name)+1; + for (nn = node; nn; nn = nn->parent) + if ((ii = strlen(nn->name))) len += ii+1-(nn->name[ii-1]=='/'); if (plen) *plen = len; - if (!len) return xstrdup(""); path = xmalloc(len)+len-ll; for (nn = node; nn; nn = nn->parent) if ((len = strlen(nn->name))) { *--path = '/'*(nn != node); + if (nn->name[len-1]=='/') len--; memcpy(path -= len, nn->name, len); } diff --git a/tests/find.test b/tests/find.test index a770f366..71a55010 100755 --- a/tests/find.test +++ b/tests/find.test @@ -140,4 +140,7 @@ ln -s nowhere broken testing "-H broken" "find -H broken" "broken\n" "" "" testing "-L broken" "find -L broken" "broken\n" "" "" +testing "one slash" 'find /etc/ -maxdepth 1 | grep /passwd\$' '/etc/passwd\n' \ + '' '' + rm -rf dir |