From root@fillmore-labs.com  Sat May 15 07:30:38 2004
Return-Path: <root@fillmore-labs.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9611316A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 15 May 2004 07:30:38 -0700 (PDT)
Received: from fillmore.dyndns.org (port-212-202-49-130.reverse.qsc.de [212.202.49.130])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 23A5643D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 15 May 2004 07:30:37 -0700 (PDT)
	(envelope-from root@fillmore-labs.com)
Received: from root by fillmore.dyndns.org with local (Exim 4.34; FreeBSD)
	id 1BP0BQ-00007N-Fs
	for FreeBSD-gnats-submit@freebsd.org; Sat, 15 May 2004 16:30:36 +0200
Message-Id: <E1BP0BQ-00007N-Fs@fillmore.dyndns.org>
Date: Sat, 15 May 2004 16:30:36 +0200
From: Oliver Eikemeier <eik@FreeBSD.org>
Reply-To: Oliver Eikemeier <eikemeier@fillmore-labs.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] find(1): new primary `-level n'
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         66667
>Category:       bin
>Synopsis:       [PATCH] find(1): new primary `-level n'
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    eik
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat May 15 07:40:19 PDT 2004
>Closed-Date:    Fri Jun 11 09:41:04 GMT 2004
>Last-Modified:  Fri Jun 11 09:41:04 GMT 2004
>Originator:     Oliver Eikemeier
>Release:        FreeBSD 4.10-STABLE i386
>Organization:
Fillmore Labs - http://www.fillmore-labs.com/
>Environment:
System: FreeBSD 4.10-STABLE

>Description:

Proposal of a new primary `-level n' for find(1), that is true iff the current file
is n levels deep in the tree. The usual +/- modifiers apply to n, with `-level +-1'
essentially being a noop. Useful to find or prune files at a known depth, without
affecting files with similar characteristics at different depths.

>How-To-Repeat:

An example to list all port directories without Tools (ok, this example lists
some additional files, like packages, distfiles and CVS, but you get the idea):

  find /usr/ports -level 1 -name Tools -prune -o -level 2 -type d -print

an rough equivalent using the previous syntax is:

  find /usr/ports -mindepth 2 -maxdepth 2 -regex '.*/Tools/[^/]*' -prune -o -type d -print

Basically, this it what I was trying to do before posting PR 66613.

>Fix:

The patch applies to -CURRENT and -STABLE

--- find-level.patch begins here ---
Index: extern.h
===================================================================
RCS file: /home/ncvs/src/usr.bin/find/extern.h,v
retrieving revision 1.20
diff -u -r1.20 extern.h
--- extern.h	3 Apr 2004 17:10:04 -0000	1.20
+++ extern.h	15 May 2004 13:47:45 -0000
@@ -65,6 +65,7 @@
 #endif
 creat_f	c_group;
 creat_f	c_inum;
+creat_f	c_level;
 creat_f	c_links;
 creat_f	c_ls;
 creat_f	c_mXXdepth;
@@ -94,6 +95,7 @@
 exec_f	f_fstype;
 exec_f	f_group;
 exec_f	f_inum;
+exec_f	f_level;
 exec_f	f_links;
 exec_f	f_ls;
 exec_f	f_name;
Index: find.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/find/find.1,v
retrieving revision 1.60
diff -u -r1.60 find.1
--- find.1	14 May 2004 12:58:13 -0000	1.60
+++ find.1	15 May 2004 13:47:45 -0000
@@ -408,6 +408,10 @@
 Like
 .Ic -regex ,
 but the match is case insensitive.
+.It Ic -level Ar n
+True if the depth of the file relative to the starting point of the traversal
+is 
+.Ar n .
 .It Ic -links Ar n
 True if the file has
 .Ar n
Index: find.h
===================================================================
RCS file: /home/ncvs/src/usr.bin/find/find.h,v
retrieving revision 1.16
diff -u -r1.16 find.h
--- find.h	2 Jun 2002 12:57:41 -0000	1.16
+++ find.h	15 May 2004 14:02:04 -0000
@@ -85,6 +85,7 @@
 			u_long _f_flags;
 			u_long _f_notflags;
 		} fl;
+		short _v_data;			/* level depth (-1 to N) */
 		nlink_t _l_data;		/* link count */
 		off_t _o_data;			/* file size */
 		time_t _t_data;			/* time value */
@@ -120,6 +121,7 @@
 #define	p_data	p_un._p_data
 #define	t_data	p_un._t_data
 #define	u_data	p_un._u_data
+#define	v_data	p_un._v_data
 #define	re_data	p_un._re_data
 #define	e_argv	p_un.ex._e_argv
 #define	e_orig	p_un.ex._e_orig
Index: function.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/find/function.c,v
retrieving revision 1.49
diff -u -r1.49 function.c
--- function.c	3 Apr 2004 17:10:04 -0000	1.49
+++ function.c	15 May 2004 13:47:45 -0000
@@ -943,6 +943,34 @@
 }
 
 /*
+ * -level n functions --
+ *
+ *	True if the file is at level n in the search tree.
+ */
+int
+f_level(plan, entry)
+	PLAN *plan;
+	FTSENT *entry;
+{
+	COMPARE(entry->fts_level, plan->v_data);
+}
+
+PLAN *
+c_level(option, argvp)
+	OPTION *option;
+	char ***argvp;
+{
+	char *nlevel;
+	PLAN *new;
+
+	nlevel = nextarg(option, argvp);
+
+	new = palloc(option);
+	new->v_data = find_parsenum(new, option->name, nlevel, NULL);
+	return new;
+}
+
+/*
  * -links n functions --
  *
  *	True if the file has n links.
Index: option.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/find/option.c,v
retrieving revision 1.21
diff -u -r1.21 option.c
--- option.c	3 Apr 2004 17:10:04 -0000	1.21
+++ option.c	15 May 2004 13:47:45 -0000
@@ -91,6 +91,7 @@
 	{ "-inum",	c_inum,		f_inum,		0 },
 	{ "-ipath",	c_name,		f_path,		F_IGNCASE },
 	{ "-iregex",	c_regex,	f_regex,	F_IGNCASE },
+	{ "-level",	c_level,	f_level,	0 },
 	{ "-links",	c_links,	f_links,	0 },
 	{ "-ls",	c_ls,		f_ls,		0 },
 	{ "-maxdepth",	c_mXXdepth,	f_always_true,	F_MAXDEPTH },
--- find-level.patch ends here ---

>Release-Note:
>Audit-Trail:

From: Oliver Eikemeier <eikemeier@fillmore-labs.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/66667: [PATCH] find(1): new primary `-level n'
Date: Thu, 20 May 2004 10:39:01 +0200

 Updated patches in
 
  <http://people.freebsd.org/~eik/patches/find-depth.5.patch>
  MD5 (find-depth.5.patch) = 5e52ac3c090726c609a9b582f6fbc993
 
  <http://people.freebsd.org/~eik/patches/find-depth.4.patch>
  MD5 (find-depth.4.patch) = 6b67d3ac20c8fb5857d2d54f0365b518
 
 see also
   <http://lists.freebsd.org/pipermail/freebsd-current/2004-May/027424.html>
 
 -Oliver
State-Changed-From-To: open->patched 
State-Changed-By: eik 
State-Changed-When: Fri May 28 19:17:36 CEST 2004 
State-Changed-Why:  
awaiting MFC 


Responsible-Changed-From-To: freebsd-bugs->eik 
Responsible-Changed-By: eik 
Responsible-Changed-When: Fri May 28 19:17:36 CEST 2004 
Responsible-Changed-Why:  
take 

http://www.freebsd.org/cgi/query-pr.cgi?pr=66667 
State-Changed-From-To: patched->closed 
State-Changed-By: eik 
State-Changed-When: Fri Jun 11 11:40:31 CEST 2004 
State-Changed-Why:  
MFC finished 

http://www.freebsd.org/cgi/query-pr.cgi?pr=66667 
>Unformatted:
