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 17 May 2004 09:18:05 -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; @@ -87,6 +88,7 @@ exec_f f_always_true; exec_f f_closeparen; exec_f f_delete; +exec_f f_depth; exec_f f_empty; exec_f f_exec; exec_f f_expr; @@ -94,6 +96,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.61 diff -u -r1.61 find.1 --- find.1 16 May 2004 10:35:14 -0000 1.61 +++ find.1 17 May 2004 09:19:15 -0000 @@ -136,6 +136,14 @@ This option is equivalent to the deprecated .Ic -depth primary. +.Fl d +can be useful when +.Nm +is used with +.Xr cpio 1 +to process files that are contained in directories with unusual permissions. +It ensures that you have write permission while you are placing files in a +directory, then sets the directory's permissions as the last thing. .It Fl f Specify a file hierarchy for .Nm @@ -259,19 +267,18 @@ .Dq Pa \&. for security reasons. Depth\-first traversal processing is implied by this option. -.It Ic -depth -Always true; +.It Ic -depth Op Ar n +When +.Ar n +is given: +true if the depth of the file relative to the starting point of the traversal +is +.Ar n . +Without argument: +always true; same as the .Fl d option. -.Ic -depth -can be useful when -.Nm -is used with -.Xr cpio 1 -to process files that are contained in directories with unusual permissions. -It ensures that you have write permission while you are placing files in a -directory, then sets the directory's permissions as the last thing. .It Ic -empty True if the current file or directory is empty. .It Ic -exec Ar utility Oo Ar argument ... Oc Li \&; @@ -408,6 +415,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.c =================================================================== RCS file: /home/ncvs/src/usr.bin/find/find.c,v retrieving revision 1.16 diff -u -r1.16 find.c --- find.c 14 Jun 2003 13:00:21 -0000 1.16 +++ find.c 17 May 2004 10:12:43 -0000 @@ -186,6 +186,11 @@ err(1, "ftsopen"); for (rval = 0; (entry = fts_read(tree)) != NULL;) { + if (maxdepth != -1 && entry->fts_level >= maxdepth) { + if (fts_set(tree, entry, FTS_SKIP)) + err(1, "%s", entry->fts_path); + } + switch (entry->fts_info) { case FTS_D: if (isdepth) @@ -225,12 +230,6 @@ * the work specified by the user on the command line. */ for (p = plan; p && (p->execute)(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; - } } /* Finish any pending -exec ... {} + functions. */ for (p = plan; p != NULL; p = p->next) 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 17 May 2004 09:18:05 -0000 @@ -57,6 +57,7 @@ #define F_TIME2_C 0x00000020 /* one of -newer?c */ #define F_TIME2_T 0x00000040 /* one of -newer?t */ #define F_MAXDEPTH F_TIME_A /* maxdepth vs. mindepth */ +#define F_DEPTH F_TIME_A /* -depth n vs. -d */ /* command line function modifiers */ #define F_EQUAL 0x00000000 /* [acm]min [acm]time inum links size */ #define F_LESSTHAN 0x00000100 @@ -85,6 +86,7 @@ u_long _f_flags; u_long _f_notflags; } fl; + short _d_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 +122,7 @@ #define p_data p_un._p_data #define t_data p_un._t_data #define u_data p_un._u_data +#define d_data p_un._d_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 17 May 2004 09:30:05 -0000 @@ -65,6 +65,7 @@ #include #include #include +#include #include "find.h" @@ -462,11 +463,9 @@ /* - * -depth functions -- + * always_true -- * - * Always true, causes descent of the directory hierarchy to be done - * so that all entries in a directory are acted on before the directory - * itself. + * Always true, used for -maxdepth, -mindepth, -xdev and -follow */ int f_always_true(PLAN *plan __unused, FTSENT *entry __unused) @@ -474,14 +473,57 @@ return 1; } +/* + * -depth functions -- + * + * With argument: True if the file is at level n. + * Without argument: Always true, causes descent of the directory hierarchy + * to be done so that all entries in a directory are acted on before the + * directory itself. + */ +int +f_depth(PLAN *plan, FTSENT *entry) +{ + if (plan->flags & F_DEPTH) + COMPARE(entry->fts_level, plan->d_data); + else + return 1; +} + PLAN * -c_depth(OPTION *option, char ***argvp __unused) +c_depth(OPTION *option, char ***argvp) { - isdepth = 1; + PLAN *new; + char *str; - return palloc(option); + new = palloc(option); + + str = **argvp; + if (str && !(new->flags & F_DEPTH)) { + /* Skip leading + or -. */ + if (*str == '+' || *str == '-') + str++; + /* Skip sign. */ + if (*str == '+' || *str == '-') + str++; + if (isdigit(*str)) + new->flags |= F_DEPTH; + } + + if (new->flags & F_DEPTH) { + /* -depth n */ + char *ndepth; + + ndepth = nextarg(option, argvp); + new->d_data = find_parsenum(new, option->name, ndepth, NULL); + } else { + /* -d */ + isdepth = 1; + } + + return new; } - + /* * -empty functions -- * 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 17 May 2004 09:18:05 -0000 @@ -72,7 +72,7 @@ { "-cnewer", c_newer, f_newer, F_TIME_C }, { "-ctime", c_Xtime, f_Xtime, F_TIME_C }, { "-delete", c_delete, f_delete, 0 }, - { "-depth", c_depth, f_always_true, 0 }, + { "-depth", c_depth, f_depth, 0 }, { "-empty", c_empty, f_empty, 0 }, { "-exec", c_exec, f_exec, 0 }, { "-execdir", c_exec, f_exec, F_EXECDIR }, @@ -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_depth, f_depth, F_DEPTH }, { "-links", c_links, f_links, 0 }, { "-ls", c_ls, f_ls, 0 }, { "-maxdepth", c_mXXdepth, f_always_true, F_MAXDEPTH },