From c88f0b2a324fd8037931b79f16e210f404280271 Mon Sep 17 00:00:00 2001 From: roberto Date: Mon, 12 Jun 2000 11:12:41 +0000 Subject: 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 --- usr.bin/find/Makefile | 1 + usr.bin/find/extern.h | 3 +++ usr.bin/find/find.1 | 6 +++++ usr.bin/find/find.c | 14 ++++++++++ usr.bin/find/find.h | 2 +- usr.bin/find/function.c | 72 +++++++++++++++++++++++++++++++++++++++++++++---- usr.bin/find/ls.c | 5 ++++ usr.bin/find/main.c | 6 +++++ usr.bin/find/misc.c | 5 ++++ usr.bin/find/operator.c | 5 ++++ usr.bin/find/option.c | 2 ++ 11 files changed, 115 insertions(+), 6 deletions(-) diff --git a/usr.bin/find/Makefile b/usr.bin/find/Makefile index 0b7db36..6ca99d0 100644 --- a/usr.bin/find/Makefile +++ b/usr.bin/find/Makefile @@ -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 diff --git a/usr.bin/find/extern.h b/usr.bin/find/extern.h index 8119e32..16832d4 100644 --- a/usr.bin/find/extern.h +++ b/usr.bin/find/extern.h @@ -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; diff --git a/usr.bin/find/find.1 b/usr.bin/find/find.1 index cd51df3..afc2ff5 100644 --- a/usr.bin/find/find.1 +++ b/usr.bin/find/find.1 @@ -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 diff --git a/usr.bin/find/find.c b/usr.bin/find/find.c index 56e2340..159ae2a 100644 --- a/usr.bin/find/find.c +++ b/usr.bin/find/find.c @@ -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 @@ -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"); diff --git a/usr.bin/find/find.h b/usr.bin/find/find.h index 36a9193..35aadac 100644 --- a/usr.bin/find/find.h +++ b/usr.bin/find/find.h @@ -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 */ diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index b53f182..bb8b174 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -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 @@ -717,6 +721,67 @@ c_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 -- * * True if the difference between the file modification time and the @@ -1010,9 +1075,6 @@ c_flags(flags_str) #endif return new; } - - /* - /* * -print functions -- diff --git a/usr.bin/find/ls.c b/usr.bin/find/ls.c index 93de47a..2335111 100644 --- a/usr.bin/find/ls.c +++ b/usr.bin/find/ls.c @@ -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 diff --git a/usr.bin/find/main.c b/usr.bin/find/main.c index 21770fb..2979026 100644 --- a/usr.bin/find/main.c +++ b/usr.bin/find/main.c @@ -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 @@ -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)); diff --git a/usr.bin/find/misc.c b/usr.bin/find/misc.c index 71316ee..36045a3 100644 --- a/usr.bin/find/misc.c +++ b/usr.bin/find/misc.c @@ -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 diff --git a/usr.bin/find/operator.c b/usr.bin/find/operator.c index 3654bca..0cb67cc 100644 --- a/usr.bin/find/operator.c +++ b/usr.bin/find/operator.c @@ -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 diff --git a/usr.bin/find/option.c b/usr.bin/find/option.c index b1ebd45..6ae15d3 100644 --- a/usr.bin/find/option.c +++ b/usr.bin/find/option.c @@ -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 }, -- cgit v1.1