[Libguestfs] [PATCH v2 2/4] mllib: Add a binding for fnmatch(3) in glibc or gnulib.

Richard W.M. Jones
Wed, 14 Dec 2016 13:12:00 -0000

This is taken from supermin.
---
 mllib/Makefile.am |  3 +++
 mllib/fnmatch-c.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mllib/fnmatch.ml  | 29 ++++++++++++++++++++++++
 mllib/fnmatch.mli | 37 +++++++++++++++++++++++++++++++
 4 files changed, 135 insertions(+)
 create mode 100644 mllib/fnmatch-c.c
 create mode 100644 mllib/fnmatch.ml
 create mode 100644 mllib/fnmatch.mli

diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 3949b1e..4cf4955 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -33,6 +33,7 @@ SOURCES_MLI = \
 	curl.mli \
 	dev_t.mli \
 	exit.mli \
+	fnmatch.mli \
 	fsync.mli \
 	getopt.mli \
 	JSON.mli \
@@ -59,6 +60,7 @@ SOURCES_ML = \
 	URI.ml \
 	mkdtemp.ml \
 	visit.ml \
+	fnmatch.ml \
 	planner.ml \
 	regedit.ml \
 	StatVFS.ml \
@@ -76,6 +78,7 @@ SOURCES_C = \
 	common_utils-c.c \
 	dev_t-c.c \
 	exit-c.c \
+	fnmatch-c.c \
 	fsync-c.c \
 	getopt-c.c \
 	mkdtemp-c.c \
diff --git a/mllib/fnmatch-c.c b/mllib/fnmatch-c.c
new file mode 100644
index 0000000..dc2984e
--- /dev/null
+++ b/mllib/fnmatch-c.c
@@ -0,0 +1,66 @@
+/* Binding for fnmatch.
+ * Copyright (C) 2009-2016 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <fnmatch.h>
+#include <errno.h>
+
+#include <caml/alloc.h>
+#include <caml/memory.h>
+#include <caml/mlvalues.h>
+#include <caml/unixsupport.h>
+
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+
+/* NB: These flags must appear in the same order as fnmatch.ml */
+static int flags[] = {
+  FNM_NOESCAPE,
+  FNM_PATHNAME,
+  FNM_PERIOD,
+  FNM_FILE_NAME,
+  FNM_LEADING_DIR,
+  FNM_CASEFOLD,
+};
+
+value
+guestfs_int_mllib_fnmatch (value patternv, value strv, value flagsv)
+{
+  CAMLparam3 (patternv, strv, flagsv);
+  int f = 0, r;
+
+  /* Convert flags to bitmask. */
+  while (flagsv != Val_int (0)) {
+    f |= flags[Int_val (Field (flagsv, 0))];
+    flagsv = Field (flagsv, 1);
+  }
+
+  r = fnmatch (String_val (patternv), String_val (strv), f);
+
+  if (r == 0)
+    CAMLreturn (Val_true);
+  else if (r == FNM_NOMATCH)
+    CAMLreturn (Val_false);
+  else {
+    /* XXX The fnmatch specification doesn't mention what errors can
+     * be returned by fnmatch.  Assume they are errnos for now.
+     */
+    unix_error (errno, (char *) "fnmatch", patternv);
+  }
+}
diff --git a/mllib/fnmatch.ml b/mllib/fnmatch.ml
new file mode 100644
index 0000000..7ea844f
--- /dev/null
+++ b/mllib/fnmatch.ml
@@ -0,0 +1,29 @@
+(* Binding for fnmatch.
+ * Copyright (C) 2009-2016 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *)
+
+(* NB: These flags must appear in the same order as fnmatch-c.c *)
+type flag +| FNM_NOESCAPE
+| FNM_PATHNAME
+| FNM_PERIOD
+| FNM_FILE_NAME
+| FNM_LEADING_DIR
+| FNM_CASEFOLD
+
+external fnmatch : string -> string -> flag list -> bool +
"guestfs_int_mllib_fnmatch"
diff --git a/mllib/fnmatch.mli b/mllib/fnmatch.mli
new file mode 100644
index 0000000..105e398
--- /dev/null
+++ b/mllib/fnmatch.mli
@@ -0,0 +1,37 @@
+(* Binding for fnmatch.
+ * Copyright (C) 2009-2016 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *)
+
+(** Binding for the fnmatch(3) function in glibc or gnulib. *)
+
+type flag +| FNM_NOESCAPE
+| FNM_PATHNAME
+| FNM_PERIOD
+| FNM_FILE_NAME
+| FNM_LEADING_DIR
+| FNM_CASEFOLD
+(** Flags passed to the fnmatch function. *)
+
+val fnmatch : string -> string -> flag list -> bool
+(** The [fnmatch pattern filename flags] function checks whether
+    the [filename] argument matches the wildcard in the [pattern]
+    argument.  The [flags] is a list of flags.  Consult the
+    fnmatch(3) man page for details of the flags.
+
+    The [filename] might be a filename element or a full path
+    (depending on the pattern and flags). *)
--
2.10.2