diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2016-02-13 15:57:41 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2016-02-13 15:57:41 -0500 |
commit | 4cd28ed2aa3f098a1d35dd44ecec27002fadb89b (patch) | |
tree | 4265775ce79279c6483c1859f12dc89e620c7c43 /bftw.c | |
parent | a54c9309c3291a960fcbcbc9e6407330a9edd044 (diff) | |
download | bfs-4cd28ed2aa3f098a1d35dd44ecec27002fadb89b.tar.xz |
Fix -name handling when the root has trailing slashes.
Diffstat (limited to 'bftw.c')
-rw-r--r-- | bftw.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -600,14 +600,27 @@ static void bftw_set_error(struct bftw_state *state, int error) { } /** + * Figure out the name offset in a path. + */ +static size_t basename_offset(const char *path) { + size_t i; + + // Strip trailing slashes + for (i = strlen(path); i > 0 && path[i - 1] == '/'; --i); + + // Find the beginning of the name + for (; i > 0 && path[i - 1] != '/'; --i); + + return i; +} + +/** * Initialize the buffers with data about the current path. */ static void bftw_init_buffers(struct bftw_state *state, const struct dirent *de) { struct BFTW *ftwbuf = &state->ftwbuf; ftwbuf->path = state->path.str; - ftwbuf->nameoff = 0; ftwbuf->error = 0; - ftwbuf->depth = 0; ftwbuf->visit = (state->status == BFTW_GC ? BFTW_POST : BFTW_PRE); ftwbuf->statbuf = NULL; ftwbuf->at_fd = AT_FDCWD; @@ -624,6 +637,9 @@ static void bftw_init_buffers(struct bftw_state *state, const struct dirent *de) } dircache_entry_base(&state->cache, current, &ftwbuf->at_fd, &ftwbuf->at_path); + } else { + ftwbuf->nameoff = basename_offset(ftwbuf->path); + ftwbuf->depth = 0; } if (de) { |