summaryrefslogtreecommitdiffstats
path: root/eval.c
diff options
context:
space:
mode:
Diffstat (limited to 'eval.c')
-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) {