diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2017-02-09 20:25:59 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2017-02-09 20:25:59 -0500 |
commit | 924826a817e397c89509963a1d13d951c9d51ce5 (patch) | |
tree | d9eb1cd82578c46ffed3a20897331dc82847437d | |
parent | 65af85882ae46cd8750b7f03519c3b291fc693db (diff) | |
download | bfs-924826a817e397c89509963a1d13d951c9d51ce5.tar.xz |
bftw: Make the nameoff of "///" point to "/"
This simplifies a few things such as -name handling for ///.
-rw-r--r-- | bftw.c | 3 | ||||
-rw-r--r-- | eval.c | 11 | ||||
-rw-r--r-- | printf.c | 4 | ||||
-rwxr-xr-x | tests.sh | 3 | ||||
-rw-r--r-- | tests/test_execdir_slashes.out | 2 | ||||
-rw-r--r-- | tests/test_printf_slashes.out | 2 |
6 files changed, 14 insertions, 11 deletions
@@ -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; } @@ -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; } } @@ -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; @@ -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 @@ -(/)/(///) +(/)/(/) |