Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Oct 2002 22:40:51 +0000
From:      Dima Dorfman <dima@trit.org>
To:        arch@freebsd.org
Subject:   fnmatch() in the kernel
Message-ID:  <20021004224006.GA304@trit.org>

next in thread | raw e-mail | index | archive | help
The DEVFS rule subsystem has a need for glob-style pattern matching in
the kernel (so you can specify a pattern to describe which devices you
want your rule to match).  To effect this, I would like to import
fnmatch(3) into the kernel.  Would libkern be a good place for it?
Attached are the diff to libkern.h and the diff between the kernel
fnmatch.c and the userland fnmatch.c.  If anybody has objections to
this or thinks it should go somewhere else, please speak up.

The overhead for having this in the kernel is ~1 kB of text on i386,
so I do not plan to make this optional (esp. since it will be used by
DEVFS).

On a procedural note, should I ask for a repo-copy of
lib/libc/gen/fnmatch.c to sys/libkern/fnmatch.c, or is this not
necessary in this case?  (I think it is appropriate, but I don't
recall ever seeing something like this done.)

Thanks,

Dima.

Index: sys/libkern.h
===================================================================
RCS file: /ref/cvsf/src/sys/sys/libkern.h,v
retrieving revision 1.31
diff -u -r1.31 libkern.h
--- sys/libkern.h	2 Sep 2002 20:16:22 -0000	1.31
+++ sys/libkern.h	4 Oct 2002 21:40:16 -0000
@@ -74,6 +74,7 @@
 #ifndef	HAVE_INLINE_FLS
 int	 fls(int);
 #endif
+int	 fnmatch(const char *, const char *, int);
 int	 locc(int, char *, u_int);
 void	 qsort(void *base, size_t nmemb, size_t size,
 	    int (*compar)(const void *, const void *));
@@ -112,5 +113,17 @@
 			*bb++ = c;
 	return (b);
 }
+
+/* fnmatch() return values. */
+#define	FNM_NOMATCH	1	/* Match failed. */
+
+/* fnmatch() flags. */
+#define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
+#define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */
+#define	FNM_PERIOD	0x04	/* Period must be matched by period. */
+#define	FNM_LEADING_DIR	0x08	/* Ignore /<tail> after Imatch. */
+#define	FNM_CASEFOLD	0x10	/* Case insensitive search. */
+#define	FNM_IGNORECASE	FNM_CASEFOLD
+#define	FNM_FILE_NAME	FNM_PATHNAME
 
 #endif /* !_SYS_LIBKERN_H_ */

--- ../../lib/libc/gen/fnmatch.c	Fri Feb  1 01:32:19 2002
+++ fnmatch.c	Fri Oct  4 21:51:24 2002
@@ -32,25 +32,18 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD$
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
 /*
  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  * Compares a filename or pathname to a pattern.
  */
 
-#include <ctype.h>
-#include <fnmatch.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "collate.h"
+#include <sys/param.h>
+#include <sys/ctype.h>
+#include <sys/libkern.h>
 
 #define	EOS	'\0'
 
@@ -101,12 +94,12 @@
 			if (c == EOS)
 				if (flags & FNM_PATHNAME)
 					return ((flags & FNM_LEADING_DIR) ||
-					    strchr(string, '/') == NULL ?
+					    index(string, '/') == NULL ?
 					    0 : FNM_NOMATCH);
 				else
 					return (0);
 			else if (c == '/' && flags & FNM_PATHNAME) {
-				if ((string = strchr(string, '/')) == NULL)
+				if ((string = index(string, '/')) == NULL)
 					return (FNM_NOMATCH);
 				break;
 			}
@@ -218,16 +211,12 @@
 			if (flags & FNM_CASEFOLD)
 				c2 = tolower((unsigned char)c2);
 
-			if (__collate_load_error ?
-			    c <= test && test <= c2 :
-			       __collate_range_cmp(c, test) <= 0
-			    && __collate_range_cmp(test, c2) <= 0
-			   )
+			if (c <= test && test <= c2)
 				ok = 1;
 		} else if (c == test)
 			ok = 1;
 	} while ((c = *pattern++) != ']');
 
-	*newp = (char *)pattern;
+	*newp = (char *)(uintptr_t)pattern;
 	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
 }

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021004224006.GA304>