Subject: find -iname, case insensitive matching
To: None <netbsd-internal@netbsd.org>
From: Niels Provos <provos@citi.umich.edu>
List: tech-userlevel
Date: 08/03/2003 14:34:32
Hi,

FreeBSD, Linux, OpenBSD, etc... support the -iname option to find.  It
allows case insenitive matching of names.

The following diffs implements this feature.

Comments?

Niels.

Index: extern.h
===================================================================
RCS file: /cvsroot/src/usr.bin/find/extern.h,v
retrieving revision 1.18
diff -u -r1.18 extern.h
--- extern.h	2003/02/23 14:43:25	1.18
+++ extern.h	2003/08/03 18:31:29
@@ -64,6 +64,7 @@
 PLAN	*c_follow __P((char ***, int));
 PLAN	*c_fstype __P((char ***, int));
 PLAN	*c_group __P((char ***, int));
+PLAN	*c_iname __P((char ***, int));
 PLAN	*c_inum __P((char ***, int));
 PLAN	*c_iregex __P((char ***, int));
 PLAN	*c_links __P((char ***, int));
Index: find.1
===================================================================
RCS file: /cvsroot/src/usr.bin/find/find.1,v
retrieving revision 1.45
diff -u -r1.45 find.1
--- find.1	2003/06/26 17:46:36	1.45
+++ find.1	2003/08/03 18:31:29
@@ -291,6 +291,11 @@
 is numeric and there is no such group name, then
 .Ar gname
 is treated as a group id.
+.It Ic -iname Ar pattern
+True if the last component of the pathname being examined
+matches
+.Ar pattern .
+Case insensitive.
 .It Ic -inum Ar n
 True if the file has inode number
 .Ar n  .
@@ -614,6 +619,7 @@
 .Ic -empty ,
 .Ic -follow ,
 .Ic -fstype ,
+.Ic -iname ,
 .Ic -inum ,
 .Ic -iregex ,
 .Ic -links ,
Index: find.h
===================================================================
RCS file: /cvsroot/src/usr.bin/find/find.h,v
retrieving revision 1.16
diff -u -r1.16 find.h
--- find.h	2003/02/23 14:41:30	1.16
+++ find.h	2003/08/03 18:31:30
@@ -46,7 +46,7 @@
 	N_AMIN, N_ANEWER, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CNEWER, N_CTIME,
 	N_DEPTH, N_EMPTY,
 	N_EXEC, N_EXECDIR, N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP,
-	N_INUM, N_IREGEX, N_LINKS, N_LS, N_MINDEPTH, N_MAXDEPTH,
+	N_INAME, N_INUM, N_IREGEX, N_LINKS, N_LS, N_MINDEPTH, N_MAXDEPTH,
 	N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK,
 	N_OPENPAREN, N_OR, N_PATH, N_PERM, N_PRINT, N_PRINT0, N_PRINTX,
 	N_PRUNE, N_REGEX, N_SIZE, N_TYPE, N_USER, N_XDEV
Index: function.c
===================================================================
RCS file: /cvsroot/src/usr.bin/find/function.c,v
retrieving revision 1.44
diff -u -r1.44 function.c
--- function.c	2003/07/12 13:57:49	1.44
+++ function.c	2003/08/03 18:31:30
@@ -94,6 +94,7 @@
 	int	f_flags __P((PLAN *, FTSENT *));
 	int	f_fstype __P((PLAN *, FTSENT *));
 	int	f_group __P((PLAN *, FTSENT *));
+	int	f_iname __P((PLAN *, FTSENT *));
 	int	f_inum __P((PLAN *, FTSENT *));
 	int	f_links __P((PLAN *, FTSENT *));
 	int	f_ls __P((PLAN *, FTSENT *));
@@ -1088,6 +1089,34 @@
 
 	(*argvp)++;
 	new = palloc(N_NAME, f_name);
+	new->c_data = pattern;
+	return (new);
+}
+ 
+/*
+ * -iname functions --
+ *
+ *	Similar to -name, but does case insensitive matching
+ *	
+ */
+int
+f_iname(plan, entry)
+	PLAN *plan;
+	FTSENT *entry;
+{
+	return (!fnmatch(plan->c_data, entry->fts_name, FNM_CASEFOLD));
+}
+ 
+PLAN *
+c_iname(argvp, isok)
+	char ***argvp;
+	int isok;
+{
+	char *pattern = **argvp;
+	PLAN *new;
+
+	(*argvp)++;
+	new = palloc(N_INAME, f_iname);
 	new->c_data = pattern;
 	return (new);
 }
Index: option.c
===================================================================
RCS file: /cvsroot/src/usr.bin/find/option.c,v
retrieving revision 1.18
diff -u -r1.18 option.c
--- option.c	2002/09/27 15:56:28	1.18
+++ option.c	2003/08/03 18:31:30
@@ -80,6 +80,7 @@
 	{ "-follow",	N_FOLLOW,	c_follow,	0 },
 	{ "-fstype",	N_FSTYPE,	c_fstype,	1 },
 	{ "-group",	N_GROUP,	c_group,	1 },
+	{ "-iname",	N_INAME,	c_iname,	1 },
 	{ "-inum",	N_INUM,		c_inum,		1 },
 	{ "-iregex",	N_IREGEX,	c_iregex,	1 },
 	{ "-links",	N_LINKS,	c_links,	1 },