From ab64ca5d29147385b099edb8874d6e101b48c1e0 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 14 Jan 2017 15:03:26 -0500 Subject: 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`). --- eval.c | 26 ++++++++++---------------- 1 file 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) { -- cgit v1.2.3