From 924826a817e397c89509963a1d13d951c9d51ce5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 9 Feb 2017 20:25:59 -0500 Subject: bftw: Make the nameoff of "///" point to "/" This simplifies a few things such as -name handling for ///. --- bftw.c | 3 +++ eval.c | 11 +++-------- printf.c | 4 ++++ tests.sh | 3 ++- tests/test_execdir_slashes.out | 2 +- tests/test_printf_slashes.out | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bftw.c b/bftw.c index 5775d54..a3f3345 100644 --- a/bftw.c +++ b/bftw.c @@ -772,6 +772,9 @@ static size_t basename_offset(const char *path) { // Find the beginning of the name for (; i > 0 && path[i - 1] != '/'; --i); + // Strip leading slashes + for (; path[i] == '/' && path[i + 1]; ++i); + return i; } diff --git a/eval.c b/eval.c index 3a97585..512a05a 100644 --- a/eval.c +++ b/eval.c @@ -286,7 +286,6 @@ static const char *exec_format_path(const struct expr *expr, const struct BFTW * if (name[0] == '/') { // Must be a root path ("/", "//", etc.) - assert(ftwbuf->nameoff == 0); return name; } @@ -311,7 +310,7 @@ err: } static void exec_free_path(const char *path, const struct BFTW *ftwbuf) { - if (path != ftwbuf->path) { + if (path != ftwbuf->path && path != ftwbuf->path + ftwbuf->nameoff) { dstrfree((char *)path); } } @@ -630,16 +629,12 @@ bool eval_name(const struct expr *expr, struct eval_state *state) { // Any trailing slashes are not part of the name. This can only // happen for the root path. const char *slash = strchr(name, '/'); - if (slash == name) { - // The name of "/" (or "//", etc.) is "/" - name = "/"; - } else if (slash) { - copy = strdup(name); + if (slash && slash > name) { + copy = strndup(name, slash - name); if (!copy) { eval_error(state); return false; } - copy[slash - name] = '\0'; name = copy; } } diff --git a/printf.c b/printf.c index cad4dc0..0341288 100644 --- a/printf.c +++ b/printf.c @@ -145,6 +145,10 @@ static int bfs_printf_h(FILE *file, const struct bfs_printf_directive *directive buf = "."; } + if (!buf) { + return -1; + } + int ret = fprintf(file, directive->str, buf); free(copy); return ret; diff --git a/tests.sh b/tests.sh index bf5aab8..3289f9a 100755 --- a/tests.sh +++ b/tests.sh @@ -177,6 +177,7 @@ bsd_tests=( test_delete_root test_execdir_slash test_execdir_slash_pwd + test_execdir_slashes test_regex test_iregex test_regex_parens @@ -257,6 +258,7 @@ gnu_tests=( test_delete_root test_execdir_slash test_execdir_slash_pwd + test_execdir_slashes test_regex test_iregex test_regex_parens @@ -291,7 +293,6 @@ bfs_tests=( test_perm_symbolic_missing_action test_perm_leading_plus_symbolic test_perm_octal_plus - test_execdir_slashes test_hidden test_nohidden ) diff --git a/tests/test_execdir_slashes.out b/tests/test_execdir_slashes.out index 187b81f..b498fd4 100644 --- a/tests/test_execdir_slashes.out +++ b/tests/test_execdir_slashes.out @@ -1 +1 @@ -/// +/ diff --git a/tests/test_printf_slashes.out b/tests/test_printf_slashes.out index 60710e5..5571971 100644 --- a/tests/test_printf_slashes.out +++ b/tests/test_printf_slashes.out @@ -1 +1 @@ -(/)/(///) +(/)/(/) -- cgit v1.2.3