Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 30 Apr 2000 03:45:47 +0100
From:      Ben Smithurst <ben@scientia.demon.co.uk>
To:        Kris Kennaway <kris@FreeBSD.org>
Cc:        FreeBSD-hackers@FreeBSD.org
Subject:   Re: Adding -maxdepth and -mindepth options to find(1)
Message-ID:  <20000430034547.C22035@strontium.scientia.demon.co.uk>
In-Reply-To: <20000430025604.B22035@strontium.scientia.demon.co.uk>
References:  <20000429001436.F17098@strontium.scientia.demon.co.uk> <Pine.BSF.4.21.0004291304200.16747-100000@freefall.freebsd.org> <20000430025604.B22035@strontium.scientia.demon.co.uk>

next in thread | previous in thread | raw e-mail | index | archive | help

--yrj/dFKFPuw6o+aM
Content-Type: text/plain; charset=us-ascii

> Turns out the patch doesn't quite work as it stands...  Something like
> 'find . -name foo -maxdepth 1' won't work because the maxdepth code
> will only be executed if the previous conditions match.  (So 'find .
> -maxdepth 1 -name foo' works fine.) I guess I'll have to find a way
> around that which isn't too ugly.

The attached patch should actually work, and includes some -Wall fixes
and added $FreeBSD$ tags.  It doesn't seem quite as clean, as it
involves modifying find_execute rather than keeping all the code for the
operators separate, but I don't really see a way around this.

-- 
Ben Smithurst / ben@scientia.demon.co.uk / PGP: 0x99392F7D

--yrj/dFKFPuw6o+aM
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="find.diff"

Index: extern.h
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/extern.h,v
retrieving revision 1.9
diff -u -r1.9 extern.h
--- extern.h	1999/12/19 15:43:18	1.9
+++ extern.h	2000/04/30 02:03:36
@@ -80,9 +80,12 @@
 PLAN	*c_xdev __P((void));
 PLAN	*c_openparen __P((void));
 PLAN	*c_closeparen __P((void));
+PLAN	*c_maxdepth __P((char *));
+PLAN	*c_mindepth __P((char *));
 PLAN	*c_mmin __P((char *));
 PLAN	*c_mtime __P((char *));
 PLAN	*c_not __P((void));
 PLAN	*c_or __P((void));
 
-extern int ftsoptions, isdeprecated, isdepth, isoutput, issort, isxargs;
+extern int ftsoptions, isdeprecated, isdepth, isoutput, issort, isxargs,
+    mindepth, maxdepth;
Index: find.1
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/find.1,v
retrieving revision 1.23
diff -u -r1.23 find.1
--- find.1	2000/03/01 10:48:32	1.23
+++ find.1	2000/04/28 19:48:12
@@ -245,6 +245,12 @@
 If the file is a symbolic link, the pathname of the linked\-to file will be
 displayed preceded by ``\->''.
 The format is identical to that produced by ``ls \-dgils''.
+.It Ic -maxdepth Ar n
+True if the depth of the current file into the tree is less than or equal to
+.Ar n .
+.It Ic -mindepth Ar n
+True if the depth of the current file into the tree is greater than or equal to
+.Ar n .
 .It Ic -mmin Ar n 
 True if the difference between the file last modification time and the time
 .Nm find
Index: find.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/find.c,v
retrieving revision 1.7
diff -u -r1.7 find.c
--- find.c	1998/11/29 11:34:30	1.7
+++ find.c	2000/04/30 02:43:35
@@ -35,7 +35,9 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
+static const char sccsid[] = "@(#)find.c	8.5 (Berkeley) 8/5/94";
+static const char rcsid[] =
+	"$FreeBSD$";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -206,12 +208,21 @@
 			continue;
 		}
 
+		if (mindepth != -1 && entry->fts_level < mindepth)
+			continue;
+
 		/*
 		 * Call all the functions in the execution plan until one is
 		 * false or all have been executed.  This is where we do all
 		 * the work specified by the user on the command line.
 		 */
 		for (p = plan; p && (p->eval)(p, entry); p = p->next);
+
+		if (maxdepth != -1 && entry->fts_level >= maxdepth) {
+			if (fts_set(tree, entry, FTS_SKIP))
+				err(1, "%s", entry->fts_path);
+			continue;
+		}
 	}
 	if (errno)
 		err(1, "fts_read");
Index: find.h
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/find.h,v
retrieving revision 1.6
diff -u -r1.6 find.h
--- find.h	1999/12/19 15:43:18	1.6
+++ find.h	2000/04/28 19:44:31
@@ -46,7 +46,7 @@
         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_PRUNE, N_SIZE, N_TYPE, N_USER, N_XDEV,
-	N_PRINT0, N_DELETE
+	N_PRINT0, N_DELETE, N_MAXDEPTH, N_MINDEPTH
 };
 
 /* node definition */
Index: function.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/function.c,v
retrieving revision 1.22
diff -u -r1.22 function.c
--- function.c	2000/02/05 18:42:34	1.22
+++ function.c	2000/04/30 02:16:30
@@ -35,8 +35,9 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)function.c	8.10 (Berkeley) 5/4/95";
-static char rcsid[] = "$FreeBSD: src/usr.bin/find/function.c,v 1.22 2000/02/05 18:42:34 joe Exp $";
+static const char sccsid[] = "@(#)function.c	8.10 (Berkeley) 5/4/95";
+static const char rcsid[] =
+	"$FreeBSD: src/usr.bin/find/function.c,v 1.22 2000/02/05 18:42:34 joe Exp $";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -717,6 +718,68 @@
 }
 
 /*
+ * -maxdepth n functions --
+ *
+ *	Does the same as -prune if the level of the current file is greater
+ *	than the specified maximum depth.
+ *
+ *	Note that -maxdepth and -mindepth are handled specially in
+ *	find_execute() so their f_* functions here do nothing.
+ */
+int
+f_maxdepth(plan, entry)
+	PLAN *plan;
+	FTSENT *entry;
+{
+	return (1);
+}
+
+PLAN *
+c_maxdepth(arg)
+	char *arg;
+{
+	PLAN *new;
+
+	if (*arg == '-')
+		/* all other errors handled by find_parsenum() */
+		errx(1, "-maxdepth: %s: value must be positive", arg);
+
+	new = palloc(N_MAXDEPTH, f_maxdepth);
+	maxdepth = find_parsenum(new, "-maxdepth", arg, NULL);
+	return (new);
+
+}
+
+/*
+ * -mindepth n functions --
+ *
+ *	True if the current file is at or deeper than the specified minimum
+ *	depth.
+ */
+int
+f_mindepth(plan, entry)
+	PLAN *plan;
+	FTSENT *entry;
+{
+	return (1);
+}
+
+PLAN *
+c_mindepth(arg)
+	char *arg;
+{
+	PLAN *new;
+
+	if (*arg == '-')
+		/* all other errors handled by find_parsenum() */
+		errx(1, "-maxdepth: %s: value must be positive", arg);
+
+	new = palloc(N_MINDEPTH, f_mindepth);
+	mindepth = find_parsenum(new, "-mindepth", arg, NULL);
+	return (new);
+}
+
+/*
  * -mtime n functions --
  *
  *	True if the difference between the file modification time and the
@@ -1005,9 +1068,6 @@
 #endif
 	return new;
 }
-  
-  /*
-
 
 /*
  * -print functions --
Index: ls.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/ls.c,v
retrieving revision 1.5
diff -u -r1.5 ls.c
--- ls.c	1998/07/06 21:01:14	1.5
+++ ls.c	2000/04/30 02:18:13
@@ -32,7 +32,9 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c	8.1 (Berkeley) 6/6/93";
+static const char sccsid[] = "@(#)ls.c	8.1 (Berkeley) 6/6/93";
+static const char rcsid[] =
+	"$FreeBSD$";
 #endif /* not lint */
 
 #include <sys/param.h>
Index: main.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/main.c,v
retrieving revision 1.9
diff -u -r1.9 main.c
--- main.c	1998/11/29 11:34:30	1.9
+++ main.c	2000/04/30 02:18:17
@@ -41,7 +41,9 @@
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
+static const char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
+static const char rcsid[] =
+	"$FreeBSD$";
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -67,6 +69,7 @@
 int isoutput;			/* user specified output operator */
 int issort;         		/* do hierarchies in lexicographical order */
 int isxargs;			/* don't permit xargs delimiting chars */
+int mindepth = -1, maxdepth = -1; /* minimum and maximum depth */
 
 static void usage __P((void));
 
Index: misc.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/misc.c,v
retrieving revision 1.2
diff -u -r1.2 misc.c
--- misc.c	1995/05/30 06:30:13	1.2
+++ misc.c	2000/04/30 02:18:20
@@ -35,7 +35,9 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 4/1/94";
+static const char sccsid[] = "@(#)misc.c	8.2 (Berkeley) 4/1/94";
+static const char rcsid[] =
+	"$FreeBSD$";
 #endif /* not lint */
 
 #include <sys/types.h>
Index: operator.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/operator.c,v
retrieving revision 1.5
diff -u -r1.5 operator.c
--- operator.c	1998/11/29 12:17:09	1.5
+++ operator.c	2000/04/30 02:18:23
@@ -35,7 +35,9 @@
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)operator.c	8.1 (Berkeley) 6/6/93";
+static const char sccsid[] = "@(#)operator.c	8.1 (Berkeley) 6/6/93";
+static const char rcsid[] =
+	"$FreeBSD$";
 #endif /* not lint */
 
 #include <sys/types.h>
Index: option.c
===================================================================
RCS file: /usr/cvs/src/usr.bin/find/option.c,v
retrieving revision 1.9
diff -u -r1.9 option.c
--- option.c	1999/12/19 15:43:19	1.9
+++ option.c	2000/04/30 02:18:28
@@ -84,6 +84,8 @@
 	{ "-inum",	N_INUM,		c_inum,		O_ARGV },
 	{ "-links",	N_LINKS,	c_links,	O_ARGV },
 	{ "-ls",	N_LS,		c_ls,		O_ZERO },
+	{ "-maxdepth",	N_MAXDEPTH,	c_maxdepth,	O_ARGV },
+	{ "-mindepth",	N_MINDEPTH,	c_mindepth,	O_ARGV },
 	{ "-mmin",	N_MMIN,	        c_mmin,	        O_ARGV },
 	{ "-mtime",	N_MTIME,	c_mtime,	O_ARGV },
 	{ "-name",	N_NAME,		c_name,		O_ARGV },

--yrj/dFKFPuw6o+aM--


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




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