summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-01-14 15:03:26 -0500
committerTavian Barnes <tavianator@tavianator.com>2017-01-14 15:03:26 -0500
commitab64ca5d29147385b099edb8874d6e101b48c1e0 (patch)
tree2b60bbfc65286d1e9f2b44c5da06d7fc101461f0
parentb626b46a51cf40fc91583da63084ef40efebc85e (diff)
downloadbfs-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.c26
1 files changed, 10 insertions, 16 deletions
diff --git a/eval.c b/eval.c
index 0946dcc..6a3da7a 100644
--- a/eval.c
+++ b/eval.c
@@ -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) {