diff options
| author | pho <pho@NetBSD.org> | 2016-11-15 00:34:19 +0000 |
|---|---|---|
| committer | pho <pho@NetBSD.org> | 2016-11-15 00:34:19 +0000 |
| commit | b82ea2be30bbad88ea6f9eb7296bb31287fb5073 (patch) | |
| tree | 42f265a661ca3535f76a73cc444dabaf8380f859 /lib/librefuse | |
| parent | 5e5c612a8428d849ebb754daf6b43ec6b5a96758 (diff) | |
fuse_opt_match(3): Support every form of templates, not just the simple strcmp case
Also it should return 1 for successful matches, not the way around.
Diffstat (limited to 'lib/librefuse')
| -rw-r--r-- | lib/librefuse/refuse.3 | 6 | ||||
| -rw-r--r-- | lib/librefuse/refuse_opt.c | 52 |
2 files changed, 48 insertions, 10 deletions
diff --git a/lib/librefuse/refuse.3 b/lib/librefuse/refuse.3 index b6f4319f4ab..f4d548ff0bc 100644 --- a/lib/librefuse/refuse.3 +++ b/lib/librefuse/refuse.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: refuse.3,v 1.11 2016/11/14 17:48:57 pho Exp $ +.\" $NetBSD: refuse.3,v 1.12 2016/11/15 00:34:19 pho Exp $ .\" .\" Copyright © 2007 Alistair Crooks. All rights reserved. .\" @@ -74,6 +74,10 @@ .Fa "struct fuse_args *args" "int pos" "const char *arg" .Fc .Ft int +.Fo fuse_opt_match +.Fa "const struct fuse_opt *opts" "const char *opt" +.Fc +.Ft int .Fo fuse_opt_parse .Fa "struct fuse_args *args" "void *userdata" .Fa "const struct fuse_opt *descriptions" "fuse_opt_proc_t processingfunc" diff --git a/lib/librefuse/refuse_opt.c b/lib/librefuse/refuse_opt.c index e5fc0d62f08..895ecc2c526 100644 --- a/lib/librefuse/refuse_opt.c +++ b/lib/librefuse/refuse_opt.c @@ -1,4 +1,4 @@ -/* $NetBSD: refuse_opt.c,v 1.16 2016/11/14 17:19:29 pho Exp $ */ +/* $NetBSD: refuse_opt.c,v 1.17 2016/11/15 00:34:19 pho Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -207,19 +207,53 @@ int fuse_opt_add_opt_escaped(char **opts, const char *opt) return add_opt(opts, opt, true); } +static bool match_templ(const char *templ, const char *opt, size_t *sep_idx) +{ + const char *sep = strpbrk(templ, "= "); + + if (sep != NULL && (sep[1] == '\0' || sep[1] == '%')) { + const size_t cmp_len = + sep[0] == '=' ? sep - templ + 1 : sep - templ; + + if (strlen(opt) >= cmp_len && strncmp(templ, opt, cmp_len) == 0) { + if (sep_idx != NULL) + *sep_idx = sep - templ; + return true; + } + else { + return false; + } + } + else { + if (strcmp(templ, opt) == 0) { + if (sep_idx != NULL) + *sep_idx = 0; + return true; + } + else { + return false; + } + } +} + +static const struct fuse_opt * +find_opt(const struct fuse_opt *opts, const char *opt, size_t *sep_idx) +{ + for (; opts != NULL && opts->templ != NULL; opts++) { + if (match_templ(opts->templ, opt, sep_idx)) + return opts; + } + return NULL; +} + /* - * Returns 0 if opt was matched with any option from opts, - * otherwise returns 1. + * Returns 1 if opt was matched with any option from opts, + * otherwise returns 0. */ int fuse_opt_match(const struct fuse_opt *opts, const char *opt) { - while (opts++) { - if (strcmp(opt, opts->templ) == 0) - return 0; - } - - return 1; + return find_opt(opts, opt, NULL) != NULL ? 1 : 0; } /* |
