diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2016-02-13 15:57:41 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2016-02-13 15:57:41 -0500 |
commit | 4cd28ed2aa3f098a1d35dd44ecec27002fadb89b (patch) | |
tree | 4265775ce79279c6483c1859f12dc89e620c7c43 /eval.c | |
parent | a54c9309c3291a960fcbcbc9e6407330a9edd044 (diff) | |
download | bfs-4cd28ed2aa3f098a1d35dd44ecec27002fadb89b.tar.xz |
Fix -name handling when the root has trailing slashes.
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -16,6 +16,7 @@ #include <fcntl.h> #include <fnmatch.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/resource.h> #include <sys/stat.h> @@ -311,7 +312,30 @@ bool eval_links(const struct expr *expr, struct eval_state *state) { */ bool eval_name(const struct expr *expr, struct eval_state *state) { struct BFTW *ftwbuf = state->ftwbuf; - return fnmatch(expr->sdata, ftwbuf->path + ftwbuf->nameoff, 0) == 0; + + const char *name = ftwbuf->path + ftwbuf->nameoff; + char *copy = NULL; + if (ftwbuf->depth == 0) { + // 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 (!copy) { + eval_error(state); + return false; + } + copy[slash - name] = '\0'; + name = copy; + } + } + + bool ret = fnmatch(expr->sdata, name, 0) == 0; + free(copy); + return ret; } /** |