diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2017-01-14 15:03:26 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2017-01-14 15:03:26 -0500 |
commit | ab64ca5d29147385b099edb8874d6e101b48c1e0 (patch) | |
tree | 2b60bbfc65286d1e9f2b44c5da06d7fc101461f0 | |
parent | b626b46a51cf40fc91583da63084ef40efebc85e (diff) | |
download | bfs-ab64ca5d29147385b099edb8874d6e101b48c1e0.tar.xz |
Simplify exec_chdir()
The previous code recomputed the name offset for no reason, and had an
embarrassing typo that was hypothetically a bug (`} if` instead of
`} else if`).
-rw-r--r-- | eval.c | 26 |
1 files changed, 10 insertions, 16 deletions
@@ -366,28 +366,22 @@ static void exec_chdir(const struct BFTW *ftwbuf) { return; } + size_t nameoff = ftwbuf->nameoff; + + if (nameoff == 0 && ftwbuf->path[nameoff] != '/') { + // The path is something like "foo", so we're already in the + // right directory + return; + } + char *path = strdup(ftwbuf->path); if (!path) { perror("strdup()"); _Exit(EXIT_FAILURE); } - // Skip trailing slashes - char *end = path + strlen(path); - while (end > path && end[-1] == '/') { - --end; - } - - // Remove the last component - while (end > path && end[-1] != '/') { - --end; - } - if (end > path) { - *end = '\0'; - } if (path[0] != '/') { - // The path is something like "foo", so we're already in the - // right directory - return; + if (nameoff > 0) { + path[nameoff] = '\0'; } if (chdir(path) != 0) { |