From eikemeier@fillmore-labs.com  Thu May 13 09:39:42 2004
Return-Path: <eikemeier@fillmore-labs.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 322D916A4CE
	for <FreeBSD-gnats-submit@FreeBSD.org>; Thu, 13 May 2004 09:39:42 -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 4BE8143D58
	for <FreeBSD-gnats-submit@FreeBSD.org>; Thu, 13 May 2004 09:39:41 -0700 (PDT)
	(envelope-from eikemeier@fillmore-labs.com)
Received: from [172.16.0.2] (helo=fillmore-labs.com)
	by fillmore.dyndns.org with esmtp (Exim 4.34; FreeBSD)
	id 1BOJF5-000OYy-UD
	for FreeBSD-gnats-submit@FreeBSD.org; Thu, 13 May 2004 18:39:40 +0200
Message-Id: <40A3A4C3.5000604@fillmore-labs.com>
Date: Thu, 13 May 2004 18:39:31 +0200
From: Oliver Eikemeier <eikemeier@fillmore-labs.com>
To: FreeBSD-gnats-submit@FreeBSD.org
Subject: find(1): -mindepth and -maxdepth doesn't work as expected

>Number:         66613
>Category:       bin
>Synopsis:       find(1): -mindepth and -maxdepth doesn't work as expected
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    eik
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 13 09:40:20 PDT 2004
>Closed-Date:    Mon May 24 02:39:44 PDT 2004
>Last-Modified:  Mon May 24 02:39:44 PDT 2004
>Originator:     Oliver Eikemeier
>Release:        FreeBSD 4.10-STABLE i386
>Organization:
Fillmore Labs - http://www.fillmore-labs.com
>Environment:
System: FreeBSD nuuk.fillmore-labs.com 4.10-STABLE

>Description:

find(1) evaluates the primaries -maxdepth and -mindepth in a global context,
before the tree traversal begins, and not on a file level as documented.

>How-To-Repeat:

The manpage find(1) contains the following flags under PRIMARIES:

-maxdepth n
        True if the depth of the current file into the tree is less than
        or equal to n.

-mindepth n
        True if the depth of the current file into the tree is greater
        than or equal to n.

Assuming that they are evaluated in the expression (as documented) I tried

  find /usr/ports -name Tools -prune -o -mindepth 2 -maxdepth 2 -type d -print

to print all sencond-level directories under ports excluding Tools.

  /usr/ports/Tools/portbuild
  /usr/ports/Tools/scripts

where listed in the output. Looking at the source and comparing FreeBSD's
find implementation with these of other OSs, I realized that this was
actually an expectation bug.

>Fix:

-maxdepth and -mindepth are global flags, and should be documented as such.
The example above is equivalent to

  find /usr/ports -mindepth 2 -maxdepth 2 \( -name Tools -prune -o -type d -print \)

See also STANDARDS of find(1), where this behaviour is documented for -depth,
-follow and -xdev:

  Historically, the -d, -H and -x options were implemented using the pri-
  maries -depth, -follow, and -xdev.  These primaries always evaluated to
  true.  As they were really global variables that took effect before the
  traversal began, some legal expressions could have unexpected results.
  An example is the expression -print -o -depth.  As -print always evalu-
  ates to true, the standard order of evaluation implies that -depth would
  never be evaluated.  This is not the case.

They should read something like:

-maxdepth n
        Always true; parsed before expression is evaluated. Descend at most 
        n directory levels below the command line arguments.  -maxdepth 0 
        limits the whole search to the command line arguments.

-mindepth n
        Always true; parsed before expression is evaluated. Do not apply any 
        tests or actions at levels less than n. -mindepth 1 processes all but 
        the command line arguments. 

Where expression refers to the parameter in the usage line. If this is acceptable,
I will submit a patch against find.1

>Release-Note:
>Audit-Trail:

From: Oliver Eikemeier <eikemeier@fillmore-labs.com>
To: FreeBSD-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/66613: find(1): -mindepth and -maxdepth doesn't work as expected
Date: Fri, 14 May 2004 00:34:52 +0200

 Ok, here is the patch, applies to -CURRENT and -STABLE:
 
 Index: find.1
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/find/find.1,v
 retrieving revision 1.59
 diff -u -r1.59 find.1
 --- find.1	21 Apr 2004 00:42:55 -0000	1.59
 +++ find.1	13 May 2004 22:22:29 -0000
 @@ -427,11 +427,23 @@
  .Nm ls Fl dgils .
  .Ek
  .It Ic -maxdepth Ar n
 -True if the depth of the current file into the tree is less than or equal to
 +Always true; do not apply any tests or actions at levels less than
  .Ar n .
 +If any
 +.Ic -maxdepth
 +primary is specified, it applies to the entire expression even if it would
 +not normally be evaluated.
 +.Ic -maxdepth Ar 0
 +limits the whole search to the command line arguments.
  .It Ic -mindepth Ar n
 -True if the depth of the current file into the tree is greater than or equal to
 -.Ar n .
 +Always true; descend at most
 +.Ar n
 +directory levels below the command line arguments. If any
 +.Ic -mindepth
 +primary is specified, it applies to the entire expression even if it would
 +not normally be evaluated.
 +.Ic -mindepth Ar 1
 +processes all but the command line arguments.
  .It Ic -mmin Ar n
  True if the difference between the file last modification time and the time
  .Nm
 
 
 I'm not sure if `Ic' is the right tag, it is in line with the rest of the man
 page. Be aware that -mindepth and -maxdepth are not included in the posix
 standard, they are GNU extensions to find(1). The semantics are compatible with
 GNU find, though.
 
 -Oliver
State-Changed-From-To: open->patched 
State-Changed-By: eik 
State-Changed-When: Fri May 14 14:58:33 CEST 2004 
State-Changed-Why:  
awaiting MFC 


Responsible-Changed-From-To: freebsd-bugs->eik 
Responsible-Changed-By: eik 
Responsible-Changed-When: Fri May 14 14:58:33 CEST 2004 
Responsible-Changed-Why:  
take 

http://www.freebsd.org/cgi/query-pr.cgi?pr=66613 
State-Changed-From-To: patched->closed 
State-Changed-By: eik 
State-Changed-When: Mon May 24 11:38:52 CEST 2004 
State-Changed-Why:  
MFC done, revision 1.23.2.23 

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