Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 29 Apr 2000 00:14:36 +0100
From:      Ben Smithurst <ben@scientia.demon.co.uk>
To:        FreeBSD-hackers@FreeBSD.org
Subject:   Adding -maxdepth and -mindepth options to find(1)
Message-ID:  <20000429001436.F17098@strontium.scientia.demon.co.uk>

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

--MnLPg7ZWsaic7Fhd
Content-Type: text/plain; charset=us-ascii

Hi,

A request came up on the FreeBSD-questions list earlier for the
-maxdepth option as found in GNU find to work in FreeBSD's find.  It
turns out that -prune will do what this guy wanted, but would it be a
good idea to add -maxdepth (and -mindepth just to be consistent) for
any other people who might want it?  I've attached a patch to implement
this, if I don't get any feedback I'll send-pr it instead, I thought I
might get a few opinions here first.

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

--MnLPg7ZWsaic7Fhd
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/28 19:42:08
@@ -80,6 +80,8 @@
 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));
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.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/28 22:42:14
@@ -717,6 +717,66 @@
 }
 
 /*
+ * -maxdepth n functions --
+ *
+ *	Does the same as -prune if the level of the current file is greater
+ *	than the specified maximum depth.
+ */
+int
+f_maxdepth(plan, entry)
+	PLAN *plan;
+	FTSENT *entry;
+{
+	extern FTS *tree;
+
+	if (entry->fts_level >= plan->t_data && fts_set(tree, entry, FTS_SKIP))
+		err(1, "%s", entry->fts_path);
+	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);
+	new->t_data = 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 (entry->fts_level >= plan->t_data);
+}
+
+PLAN *
+c_mindepth(arg)
+	char *arg;
+{
+	PLAN *new;
+
+	new = palloc(N_MINDEPTH, f_mindepth);
+	new->t_data = find_parsenum(new, "-mindepth", arg, NULL);
+	return (new);
+}
+
+/*
  * -mtime n functions --
  *
  *	True if the difference between the file modification time and the
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/28 19:41:56
@@ -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 },

--MnLPg7ZWsaic7Fhd--


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?20000429001436.F17098>