Module Name:    src
Committed By:   pho
Date:           Tue Nov 15 00:34:19 UTC 2016

Modified Files:
        src/lib/librefuse: refuse.3 refuse_opt.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/lib/librefuse/refuse.3
cvs rdiff -u -r1.16 -r1.17 src/lib/librefuse/refuse_opt.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/librefuse/refuse.3
diff -u src/lib/librefuse/refuse.3:1.11 src/lib/librefuse/refuse.3:1.12
--- src/lib/librefuse/refuse.3:1.11	Mon Nov 14 17:48:57 2016
+++ src/lib/librefuse/refuse.3	Tue Nov 15 00:34:19 2016
@@ -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"

Index: src/lib/librefuse/refuse_opt.c
diff -u src/lib/librefuse/refuse_opt.c:1.16 src/lib/librefuse/refuse_opt.c:1.17
--- src/lib/librefuse/refuse_opt.c:1.16	Mon Nov 14 17:19:29 2016
+++ src/lib/librefuse/refuse_opt.c	Tue Nov 15 00:34:19 2016
@@ -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
 	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;
 }
 
 /*

Reply via email to