This patch adds the -mindepth and -maxdepth options to find(1), which

behave as in GNU find (and of course as described in the manual page
    diff included).  I think these options would be useful for some people.

    Some missing $FreeBSD$ tags are also added.

    The patch was slightly modified (send-pr mangling of TABS).

PR:		bin/18941
Submitted by:	Ben Smithurst <ben@scientia.demon.co.uk>
This commit is contained in:
Ollivier Robert 2000-06-12 11:12:41 +00:00
parent 987170f752
commit c76bc8f3bf
11 changed files with 115 additions and 6 deletions

View File

@ -1,6 +1,7 @@
# @(#)Makefile 8.1 (Berkeley) 6/6/93
# $FreeBSD$
CFLAGS+= -Wall
PROG= find
SRCS= find.c function.c ls.c main.c misc.c operator.c option.c setflags.c
.PATH: ${.CURDIR}/../../lib/libc/gen

View File

@ -80,9 +80,12 @@ PLAN *c_user __P((char *));
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 mindepth, maxdepth;

View File

@ -245,6 +245,12 @@ will be displayed instead of the size in bytes.
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

View File

@ -35,7 +35,12 @@
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)find.c 8.5 (Berkeley) 8/5/94";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/types.h>
@ -206,12 +211,21 @@ find_execute(plan, paths)
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");

View File

@ -46,7 +46,7 @@ enum ntype {
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 */

View File

@ -35,8 +35,12 @@
*/
#ifndef lint
static char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
static char rcsid[] = "$FreeBSD$";
#if 0
static const char sccsid[] = "@(#)function.c 8.10 (Berkeley) 5/4/95";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/param.h>
@ -716,6 +720,67 @@ c_ls()
return (palloc(N_LS, f_ls));
}
/*
* -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 --
*
@ -1010,9 +1075,6 @@ c_flags(flags_str)
#endif
return new;
}
/*
/*
* -print functions --

View File

@ -32,7 +32,12 @@
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)ls.c 8.1 (Berkeley) 6/6/93";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/param.h>

View File

@ -41,7 +41,12 @@ char copyright[] =
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/types.h>
@ -67,6 +72,7 @@ int isdepth; /* do directories on post-order visit */
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));

View File

@ -35,7 +35,12 @@
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)misc.c 8.2 (Berkeley) 4/1/94";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/types.h>

View File

@ -35,7 +35,12 @@
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)operator.c 8.1 (Berkeley) 6/6/93";
#else
static const char rcsid[] =
"$FreeBSD$";
#endif
#endif /* not lint */
#include <sys/types.h>

View File

@ -84,6 +84,8 @@ static OPTION const options[] = {
{ "-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 },