Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/librefuse fuse_opt_match(3): Support every form of templ...



details:   https://anonhg.NetBSD.org/src/rev/0fa3e6b7fad0
branches:  trunk
changeset: 819045:0fa3e6b7fad0
user:      pho <pho%NetBSD.org@localhost>
date:      Tue Nov 15 00:34:19 2016 +0000

description:
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:

 lib/librefuse/refuse.3     |   6 ++++-
 lib/librefuse/refuse_opt.c |  52 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 48 insertions(+), 10 deletions(-)

diffs (91 lines):

diff -r 6048b2d8e0c7 -r 0fa3e6b7fad0 lib/librefuse/refuse.3
--- a/lib/librefuse/refuse.3    Tue Nov 15 00:32:42 2016 +0000
+++ b/lib/librefuse/refuse.3    Tue Nov 15 00:34:19 2016 +0000
@@ -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 -r 6048b2d8e0c7 -r 0fa3e6b7fad0 lib/librefuse/refuse_opt.c
--- a/lib/librefuse/refuse_opt.c        Tue Nov 15 00:32:42 2016 +0000
+++ b/lib/librefuse/refuse_opt.c        Tue Nov 15 00:34:19 2016 +0000
@@ -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 @@
        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;
 }
 
 /*



Home | Main Index | Thread Index | Old Index