summaryrefslogtreecommitdiffstats
path: root/bftw.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2015-06-21 21:52:51 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-06-21 21:52:51 -0400
commit1033a017c16ac7f239c2436fbd9cad433672a011 (patch)
tree3dc1c344f8ffe7bddd52e0d110c2d08b3c98fe98 /bftw.c
parent22291402d329a49ab8ab4a272344cab902b2ab3b (diff)
downloadbfs-1033a017c16ac7f239c2436fbd9cad433672a011.tar.xz
bftw: Don't add extra slashes.
This fixes "bfs /" among other things.
Diffstat (limited to 'bftw.c')
-rw-r--r--bftw.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/bftw.c b/bftw.c
index 0d55a65..47ab8b5 100644
--- a/bftw.c
+++ b/bftw.c
@@ -27,6 +27,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -225,7 +226,13 @@ static DIR *dircache_entry_open(dircache *cache, dircache_entry *entry, dynstr *
dircache_entry *parent = entry;
do {
- pathsize += strlen(parent->name) + 1; // name + '/'
+ size_t namelen = strlen(parent->name);
+ pathsize += namelen;
+
+ if (namelen > 0 && parent->name[namelen - 1] != '/') {
+ ++pathsize;
+ }
+
parent = parent->parent;
} while (parent);
@@ -241,11 +248,20 @@ static DIR *dircache_entry_open(dircache *cache, dircache_entry *entry, dynstr *
const char *relpath = path->str;
parent = entry;
- while (1) {
+ while (true) {
size_t namelen = strlen(parent->name);
- segment -= namelen + 1;
+ bool needs_slash = namelen > 0 && parent->name[namelen - 1] != '/';
+
+ segment -= namelen;
+ if (needs_slash) {
+ segment -= 1;
+ }
+
memcpy(segment, parent->name, namelen);
- segment[namelen] = '/';
+
+ if (needs_slash) {
+ segment[namelen] = '/';
+ }
parent = parent->parent;
if (!parent) {