tftpd: new applet (mostly using existing code for tftp)
[oweals/busybox.git] / findutils / find.c
index 43120250b3b7f9e9abf7ffdcdfd0edcef62d20a9..634fbd189c057e44ab374dd9f480c36a56472ca4 100644 (file)
  * (no output)
  */
 
+/* Testing script
+ * ./busybox find "$@" | tee /tmp/bb_find
+ * echo ==================
+ * /path/to/gnu/find "$@" | tee /tmp/std_find
+ * echo ==================
+ * diff -u /tmp/std_find /tmp/bb_find && echo Identical
+ */
+
 #include <fnmatch.h>
-#include "busybox.h"
+#include "libbb.h"
+#if ENABLE_FEATURE_FIND_REGEX
+#include "xregex.h"
+#endif
+
+/* This is a NOEXEC applet. Be very careful! */
+
 
 USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
 USE_FEATURE_FIND_XDEV(static int xdev_count;)
@@ -60,32 +74,38 @@ typedef struct {
 #endif
 } action;
 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
-#define ACTF(name)         static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
-                        ACTS(print)
-                        ACTS(name,  const char *pattern;)
-USE_FEATURE_FIND_PRINT0(ACTS(print0))
-USE_FEATURE_FIND_TYPE(  ACTS(type,  int type_mask;))
-USE_FEATURE_FIND_PERM(  ACTS(perm,  char perm_char; mode_t perm_mask;))
-USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
-USE_FEATURE_FIND_MMIN(  ACTS(mmin,  char mmin_char; unsigned mmin_mins;))
-USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
-USE_FEATURE_FIND_INUM(  ACTS(inum,  ino_t inode_num;))
-USE_FEATURE_FIND_EXEC(  ACTS(exec,  char **exec_argv; unsigned int *subst_count; int exec_argc;))
-USE_FEATURE_FIND_USER(  ACTS(user,  uid_t uid;))
-USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
-USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
-USE_FEATURE_FIND_SIZE(  ACTS(size,  off_t size;))
-USE_FEATURE_FIND_PRUNE( ACTS(prune))
+#define ACTF(name)         static int func_##name(const char *fileName ATTRIBUTE_UNUSED, \
+                                                  struct stat *statbuf ATTRIBUTE_UNUSED, \
+                                                  action_##name* ap ATTRIBUTE_UNUSED)
+                         ACTS(print)
+                         ACTS(name,  const char *pattern; bool iname;)
+USE_FEATURE_FIND_PATH(   ACTS(path,  const char *pattern;))
+USE_FEATURE_FIND_REGEX(  ACTS(regex, regex_t compiled_pattern;))
+USE_FEATURE_FIND_PRINT0( ACTS(print0))
+USE_FEATURE_FIND_TYPE(   ACTS(type,  int type_mask;))
+USE_FEATURE_FIND_PERM(   ACTS(perm,  char perm_char; mode_t perm_mask;))
+USE_FEATURE_FIND_MTIME(  ACTS(mtime, char mtime_char; unsigned mtime_days;))
+USE_FEATURE_FIND_MMIN(   ACTS(mmin,  char mmin_char; unsigned mmin_mins;))
+USE_FEATURE_FIND_NEWER(  ACTS(newer, time_t newer_mtime;))
+USE_FEATURE_FIND_INUM(   ACTS(inum,  ino_t inode_num;))
+USE_FEATURE_FIND_USER(   ACTS(user,  uid_t uid;))
+USE_FEATURE_FIND_SIZE(   ACTS(size,  char size_char; off_t size;))
+USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
+USE_FEATURE_FIND_PAREN(  ACTS(paren, action ***subexpr;))
+USE_FEATURE_FIND_PRUNE(  ACTS(prune))
+USE_FEATURE_FIND_DELETE( ACTS(delete))
+USE_FEATURE_FIND_EXEC(   ACTS(exec,  char **exec_argv; unsigned *subst_count; int exec_argc;))
+USE_FEATURE_FIND_GROUP(  ACTS(group, gid_t gid;))
 
 static action ***actions;
 static bool need_print = 1;
-static int recurse_flags = action_recurse;
+static int recurse_flags = ACTION_RECURSE;
 
 #if ENABLE_FEATURE_FIND_EXEC
-static unsigned int count_subst(const char *str)
+static unsigned count_subst(const char *str)
 {
-       unsigned int count = 0;
-       while ((str = strstr(str, "{}"))) {
+       unsigned count = 0;
+       while ((str = strstr(str, "{}")) != NULL) {
                count++;
                str++;
        }
@@ -93,7 +113,7 @@ static unsigned int count_subst(const char *str)
 }
 
 
-static char* subst(const char *src, unsigned int count, const char* filename)
+static char* subst(const char *src, unsigned count, const char* filename)
 {
        char *buf, *dst, *end;
        size_t flen = strlen(filename);
@@ -111,46 +131,87 @@ static char* subst(const char *src, unsigned int count, const char* filename)
 }
 #endif
 
+/* Return values of ACTFs ('action functions') are a bit mask:
+ * bit 1=1: prune (use SKIP constant for setting it)
+ * bit 0=1: matched successfully (TRUE)
+ */
 
 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
 {
        int cur_group;
        int cur_action;
-       int rc = TRUE;
+       int rc = 0;
        action **app, *ap;
 
+       /* "action group" is a set of actions ANDed together.
+        * groups are ORed together.
+        * We simply evaluate each group until we find one in which all actions
+        * succeed. */
+
+       /* -prune is special: if it is encountered, then we won't
+        * descend into current directory. It doesn't matter whether
+        * action group (in which -prune sits) will succeed or not:
+        * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
+        * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
+        *     not starting with 'f' */
+
+       /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
+        * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
+        * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
+        * On return, bit is restored.  */
+
        cur_group = -1;
        while ((app = appp[++cur_group])) {
+               rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
                cur_action = -1;
                while (1) {
                        ap = app[++cur_action];
-                       if (!ap) {
-                               /* all actions in group were successful */
-                               return rc;
-                       }
-                       rc = ap->f(fileName, statbuf, ap);
+                       if (!ap) /* all actions in group were successful */
+                               return rc ^ TRUE; /* restore TRUE bit */
+                       rc |= TRUE ^ ap->f(fileName, statbuf, ap);
 #if ENABLE_FEATURE_FIND_NOT
-                       if (ap->invert) rc = !rc;
+                       if (ap->invert) rc ^= TRUE;
 #endif
-                       if (!rc) {
-                               /* current group failed, try next */
+                       if (rc & TRUE) /* current group failed, try next */
                                break;
-                       }
                }
        }
-       return rc;
+       return rc ^ TRUE; /* restore TRUE bit */
 }
 
 
 ACTF(name)
 {
-       const char *tmp = strrchr(fileName, '/');
-       if (tmp == NULL)
-               tmp = fileName;
-       else
-               tmp++;
-       return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
+       const char *tmp = bb_basename(fileName);
+       if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
+               tmp--;
+               while (tmp != fileName && *--tmp != '/')
+                       continue;
+               if (*tmp == '/')
+                       tmp++;
+       }
+       return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
+}
+
+#if ENABLE_FEATURE_FIND_PATH
+ACTF(path)
+{
+       return fnmatch(ap->pattern, fileName, 0) == 0;
 }
+#endif
+#if ENABLE_FEATURE_FIND_REGEX
+ACTF(regex)
+{
+       regmatch_t match;
+       if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
+               return 0; /* no match */
+       if (match.rm_so)
+               return 0; /* match doesn't start at pos 0 */
+       if (fileName[match.rm_eo])
+               return 0; /* match doesn't end exactly at end of pathname */
+       return 1;
+}
+#endif
 #if ENABLE_FEATURE_FIND_TYPE
 ACTF(type)
 {
@@ -212,33 +273,33 @@ ACTF(inum)
 ACTF(exec)
 {
        int i, rc;
-       char *argv[ap->exec_argc+1];
+       char *argv[ap->exec_argc + 1];
        for (i = 0; i < ap->exec_argc; i++)
                argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
        argv[i] = NULL; /* terminate the list */
-       rc = wait4pid(spawn(argv));
-       if (rc)
-               bb_perror_msg("%s", argv[0]);
-       for (i = 0; i < ap->exec_argc; i++)
-               free(argv[i]);
-       return rc == 0; /* return 1 if success */
+
+       rc = spawn_and_wait(argv);
+       if (rc < 0)
+               bb_simple_perror_msg(argv[0]);
+
+       i = 0;
+       while (argv[i])
+               free(argv[i++]);
+       return rc == 0; /* return 1 if exitcode 0 */
 }
 #endif
-
 #if ENABLE_FEATURE_FIND_USER
 ACTF(user)
 {
        return (statbuf->st_uid == ap->uid);
 }
 #endif
-
 #if ENABLE_FEATURE_FIND_GROUP
 ACTF(group)
 {
        return (statbuf->st_gid == ap->gid);
 }
 #endif
-
 #if ENABLE_FEATURE_FIND_PRINT0
 ACTF(print0)
 {
@@ -246,20 +307,27 @@ ACTF(print0)
        return TRUE;
 }
 #endif
-
 ACTF(print)
 {
        puts(fileName);
        return TRUE;
 }
-
 #if ENABLE_FEATURE_FIND_PAREN
 ACTF(paren)
 {
        return exec_actions(ap->subexpr, fileName, statbuf);
 }
 #endif
-
+#if ENABLE_FEATURE_FIND_SIZE
+ACTF(size)
+{
+       if (ap->size_char == '+')
+               return statbuf->st_size > ap->size;
+       if (ap->size_char == '-')
+               return statbuf->st_size < ap->size;
+       return statbuf->st_size == ap->size;
+}
+#endif
 #if ENABLE_FEATURE_FIND_PRUNE
 /*
  * -prune: if -depth is not given, return true and do not descend
@@ -269,36 +337,72 @@ ACTF(paren)
  */
 ACTF(prune)
 {
-       return SKIP;
+       return SKIP + TRUE;
 }
 #endif
-
-#if ENABLE_FEATURE_FIND_SIZE
-ACTF(size)
+#if ENABLE_FEATURE_FIND_DELETE
+ACTF(delete)
 {
-       return statbuf->st_size == ap->size;
+       int rc;
+       if (S_ISDIR(statbuf->st_mode)) {
+               rc = rmdir(fileName);
+       } else {
+               rc = unlink(fileName);
+       }
+       if (rc < 0)
+               bb_simple_perror_msg(fileName);
+       return TRUE;
+}
+#endif
+#if ENABLE_FEATURE_FIND_CONTEXT
+ACTF(context)
+{
+       security_context_t con;
+       int rc;
+
+       if (recurse_flags & ACTION_FOLLOWLINKS) {
+               rc = getfilecon(fileName, &con);
+       } else {
+               rc = lgetfilecon(fileName, &con);
+       }
+       if (rc < 0)
+               return FALSE;
+       rc = strcmp(ap->context, con);
+       freecon(con);
+       return rc == 0;
 }
 #endif
 
 
-static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
+static int fileAction(const char *fileName,
+               struct stat *statbuf,
+               void *userData SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED),
+               int depth SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED))
 {
        int i;
-#ifdef CONFIG_FEATURE_FIND_XDEV
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+       int maxdepth = (int)(ptrdiff_t)userData;
+
+       if (depth > maxdepth) return SKIP;
+#endif
+
+#if ENABLE_FEATURE_FIND_XDEV
        if (S_ISDIR(statbuf->st_mode) && xdev_count) {
                for (i = 0; i < xdev_count; i++) {
-                       if (xdev_dev[i] != statbuf->st_dev)
-                               return SKIP;
+                       if (xdev_dev[i] == statbuf->st_dev)
+                               break;
                }
+               if (i == xdev_count)
+                       return SKIP;
        }
 #endif
        i = exec_actions(actions, fileName, statbuf);
        /* Had no explicit -print[0] or -exec? then print */
-       if (i && need_print)
+       if ((i & TRUE) && need_print)
                puts(fileName);
        /* Cannot return 0: our caller, recursive_action(),
         * will perror() and skip dirs (if called on dir) */
-       return i == 0 ? TRUE : i;
+       return (i & SKIP) ? SKIP : TRUE;
 }
 
 
@@ -329,8 +433,9 @@ static int find_type(const char *type)
 }
 #endif
 
-#if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
- || ENABLE_FEATURE_FIND_MMIN
+#if ENABLE_FEATURE_FIND_PERM \
+ || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
+ || ENABLE_FEATURE_FIND_SIZE
 static const char* plus_minus_num(const char* str)
 {
        if (*str == '-' || *str == '+')
@@ -342,59 +447,70 @@ static const char* plus_minus_num(const char* str)
 static action*** parse_params(char **argv)
 {
        enum {
-                               PARM_a         ,
-                               PARM_o         ,
-       USE_FEATURE_FIND_NOT(   PARM_char_not  ,)
-                               PARM_print     ,
-       USE_FEATURE_FIND_PRINT0(PARM_print0    ,)
-                               PARM_name      ,
-       USE_FEATURE_FIND_TYPE(  PARM_type      ,)
-       USE_FEATURE_FIND_PERM(  PARM_perm      ,)
-       USE_FEATURE_FIND_MTIME( PARM_mtime     ,)
-       USE_FEATURE_FIND_MMIN(  PARM_mmin      ,)
-       USE_FEATURE_FIND_NEWER( PARM_newer     ,)
-       USE_FEATURE_FIND_INUM(  PARM_inum      ,)
-       USE_FEATURE_FIND_EXEC(  PARM_exec      ,)
-       USE_FEATURE_FIND_USER(  PARM_user      ,)
-       USE_FEATURE_FIND_GROUP( PARM_group     ,)
-       USE_FEATURE_FIND_DEPTH( PARM_depth     ,)
-       USE_FEATURE_FIND_PAREN( PARM_char_brace,)
-       USE_FEATURE_FIND_SIZE(  PARM_size      ,)
-       USE_FEATURE_FIND_PRUNE( PARM_prune     ,)
+                                PARM_a         ,
+                                PARM_o         ,
+       USE_FEATURE_FIND_NOT(    PARM_char_not  ,)
 #if ENABLE_DESKTOP
-                               PARM_and       ,
-                               PARM_or        ,
-       USE_FEATURE_FIND_NOT(   PARM_not       ,)
-#endif
+                                PARM_and       ,
+                                PARM_or        ,
+       USE_FEATURE_FIND_NOT(    PARM_not       ,)
+#endif
+                                PARM_print     ,
+       USE_FEATURE_FIND_PRINT0( PARM_print0    ,)
+       USE_FEATURE_FIND_DEPTH(  PARM_depth     ,)
+       USE_FEATURE_FIND_PRUNE(  PARM_prune     ,)
+       USE_FEATURE_FIND_DELETE( PARM_delete    ,)
+       USE_FEATURE_FIND_EXEC(   PARM_exec      ,)
+       USE_FEATURE_FIND_PAREN(  PARM_char_brace,)
+       /* All options starting from here require argument */
+                                PARM_name      ,
+                                PARM_iname     ,
+       USE_FEATURE_FIND_PATH(   PARM_path      ,)
+       USE_FEATURE_FIND_REGEX(  PARM_regex     ,)
+       USE_FEATURE_FIND_TYPE(   PARM_type      ,)
+       USE_FEATURE_FIND_PERM(   PARM_perm      ,)
+       USE_FEATURE_FIND_MTIME(  PARM_mtime     ,)
+       USE_FEATURE_FIND_MMIN(   PARM_mmin      ,)
+       USE_FEATURE_FIND_NEWER(  PARM_newer     ,)
+       USE_FEATURE_FIND_INUM(   PARM_inum      ,)
+       USE_FEATURE_FIND_USER(   PARM_user      ,)
+       USE_FEATURE_FIND_GROUP(  PARM_group     ,)
+       USE_FEATURE_FIND_SIZE(   PARM_size      ,)
+       USE_FEATURE_FIND_CONTEXT(PARM_context   ,)
        };
 
-       static const char *const params[] = {
-                               "-a"     ,
-                               "-o"     ,
-       USE_FEATURE_FIND_NOT(   "!"      ,)
-                               "-print" ,
-       USE_FEATURE_FIND_PRINT0("-print0",)
-                               "-name"  ,
-       USE_FEATURE_FIND_TYPE(  "-type"  ,)
-       USE_FEATURE_FIND_PERM(  "-perm"  ,)
-       USE_FEATURE_FIND_MTIME( "-mtime" ,)
-       USE_FEATURE_FIND_MMIN(  "-mmin"  ,)
-       USE_FEATURE_FIND_NEWER( "-newer" ,)
-       USE_FEATURE_FIND_INUM(  "-inum"  ,)
-       USE_FEATURE_FIND_EXEC(  "-exec"  ,)
-       USE_FEATURE_FIND_USER(  "-user"  ,)
-       USE_FEATURE_FIND_GROUP( "-group" ,)
-       USE_FEATURE_FIND_DEPTH( "-depth" ,)
-       USE_FEATURE_FIND_PAREN( "("      ,)
-       USE_FEATURE_FIND_SIZE(  "-size" ,)
-       USE_FEATURE_FIND_PRUNE( "-prune"  ,)
+       static const char params[] ALIGN1 =
+                                "-a\0"
+                                "-o\0"
+       USE_FEATURE_FIND_NOT(    "!\0"       )
 #if ENABLE_DESKTOP
-                               "-and"   ,
-                               "-or"    ,
-       USE_FEATURE_FIND_NOT(   "-not"   ,)
-#endif
-                               NULL
-       };
+                                "-and\0"
+                                "-or\0"
+       USE_FEATURE_FIND_NOT(    "-not\0"    )
+#endif
+                                "-print\0"
+       USE_FEATURE_FIND_PRINT0( "-print0\0" )
+       USE_FEATURE_FIND_DEPTH(  "-depth\0"  )
+       USE_FEATURE_FIND_PRUNE(  "-prune\0"  )
+       USE_FEATURE_FIND_DELETE( "-delete\0" )
+       USE_FEATURE_FIND_EXEC(   "-exec\0"   )
+       USE_FEATURE_FIND_PAREN(  "(\0"       )
+       /* All options starting from here require argument */
+                                "-name\0"
+                                "-iname\0"
+       USE_FEATURE_FIND_PATH(   "-path\0"   )
+       USE_FEATURE_FIND_REGEX(  "-regex\0"  )
+       USE_FEATURE_FIND_TYPE(   "-type\0"   )
+       USE_FEATURE_FIND_PERM(   "-perm\0"   )
+       USE_FEATURE_FIND_MTIME(  "-mtime\0"  )
+       USE_FEATURE_FIND_MMIN(   "-mmin\0"   )
+       USE_FEATURE_FIND_NEWER(  "-newer\0"  )
+       USE_FEATURE_FIND_INUM(   "-inum\0"   )
+       USE_FEATURE_FIND_USER(   "-user\0"   )
+       USE_FEATURE_FIND_GROUP(  "-group\0"  )
+       USE_FEATURE_FIND_SIZE(   "-size\0"   )
+       USE_FEATURE_FIND_CONTEXT("-context\0")
+                                ;
 
        action*** appp;
        unsigned cur_group = 0;
@@ -434,8 +550,19 @@ static action*** parse_params(char **argv)
  */
        while (*argv) {
                const char *arg = argv[0];
+               int parm = index_in_strings(params, arg);
                const char *arg1 = argv[1];
-               int parm = index_in_str_array(params, arg);
+
+               if (parm >= PARM_name) {
+                       /* All options starting from -name require argument */
+                       if (!arg1)
+                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
+                       argv++;
+               }
+
+               /* We can use big switch() here, but on i386
+                * it doesn't give smaller code. Other arches? */
+
        /* --- Operators --- */
                if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
                        /* no further special handling required */
@@ -469,33 +596,107 @@ static action*** parse_params(char **argv)
                        (void) ALLOC_ACTION(print0);
                }
 #endif
-               else if (parm == PARM_name) {
-                       action_name *ap;
-                       if (!*++argv)
+#if ENABLE_FEATURE_FIND_DEPTH
+               else if (parm == PARM_depth) {
+                       recurse_flags |= ACTION_DEPTHFIRST;
+               }
+#endif
+#if ENABLE_FEATURE_FIND_PRUNE
+               else if (parm == PARM_prune) {
+                       USE_FEATURE_FIND_NOT( invert_flag = 0; )
+                       (void) ALLOC_ACTION(prune);
+               }
+#endif
+#if ENABLE_FEATURE_FIND_DELETE
+               else if (parm == PARM_delete) {
+                       need_print = 0;
+                       recurse_flags |= ACTION_DEPTHFIRST;
+                       (void) ALLOC_ACTION(delete);
+               }
+#endif
+#if ENABLE_FEATURE_FIND_EXEC
+               else if (parm == PARM_exec) {
+                       int i;
+                       action_exec *ap;
+                       need_print = 0;
+                       USE_FEATURE_FIND_NOT( invert_flag = 0; )
+                       ap = ALLOC_ACTION(exec);
+                       ap->exec_argv = ++argv; /* first arg after -exec */
+                       ap->exec_argc = 0;
+                       while (1) {
+                               if (!*argv) /* did not see ';' until end */
+                                       bb_error_msg_and_die("-exec CMD must end by ';'");
+                               if (LONE_CHAR(argv[0], ';'))
+                                       break;
+                               argv++;
+                               ap->exec_argc++;
+                       }
+                       if (ap->exec_argc == 0)
                                bb_error_msg_and_die(bb_msg_requires_arg, arg);
+                       ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
+                       i = ap->exec_argc;
+                       while (i--)
+                               ap->subst_count[i] = count_subst(ap->exec_argv[i]);
+               }
+#endif
+#if ENABLE_FEATURE_FIND_PAREN
+               else if (parm == PARM_char_brace) {
+                       action_paren *ap;
+                       char **endarg;
+                       unsigned nested = 1;
+
+                       endarg = argv;
+                       while (1) {
+                               if (!*++endarg)
+                                       bb_error_msg_and_die("unpaired '('");
+                               if (LONE_CHAR(*endarg, '('))
+                                       nested++;
+                               else if (LONE_CHAR(*endarg, ')') && !--nested) {
+                                       *endarg = NULL;
+                                       break;
+                               }
+                       }
+                       ap = ALLOC_ACTION(paren);
+                       ap->subexpr = parse_params(argv + 1);
+                       *endarg = (char*) ")"; /* restore NULLed parameter */
+                       argv = endarg;
+               }
+#endif
+               else if (parm == PARM_name || parm == PARM_iname) {
+                       action_name *ap;
                        ap = ALLOC_ACTION(name);
                        ap->pattern = arg1;
+                       ap->iname = (parm == PARM_iname);
                }
+#if ENABLE_FEATURE_FIND_PATH
+               else if (parm == PARM_path) {
+                       action_path *ap;
+                       ap = ALLOC_ACTION(path);
+                       ap->pattern = arg1;
+               }
+#endif
+#if ENABLE_FEATURE_FIND_REGEX
+               else if (parm == PARM_regex) {
+                       action_regex *ap;
+                       ap = ALLOC_ACTION(regex);
+                       xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
+               }
+#endif
 #if ENABLE_FEATURE_FIND_TYPE
                else if (parm == PARM_type) {
                        action_type *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(type);
                        ap->type_mask = find_type(arg1);
                }
 #endif
 #if ENABLE_FEATURE_FIND_PERM
-/* TODO:
- * -perm mode   File's permission bits are exactly mode (octal or symbolic).
+/* -perm mode   File's permission bits are exactly mode (octal or symbolic).
  *              Symbolic modes use mode 0 as a point of departure.
  * -perm -mode  All of the permission bits mode are set for the file.
  * -perm +mode  Any of the permission bits mode are set for the file.
  */
                else if (parm == PARM_perm) {
                        action_perm *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(perm);
                        ap->perm_char = arg1[0];
                        arg1 = plus_minus_num(arg1);
@@ -507,8 +708,6 @@ static action*** parse_params(char **argv)
 #if ENABLE_FEATURE_FIND_MTIME
                else if (parm == PARM_mtime) {
                        action_mtime *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(mtime);
                        ap->mtime_char = arg1[0];
                        ap->mtime_days = xatoul(plus_minus_num(arg1));
@@ -517,8 +716,6 @@ static action*** parse_params(char **argv)
 #if ENABLE_FEATURE_FIND_MMIN
                else if (parm == PARM_mmin) {
                        action_mmin *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(mmin);
                        ap->mmin_char = arg1[0];
                        ap->mmin_mins = xatoul(plus_minus_num(arg1));
@@ -526,54 +723,23 @@ static action*** parse_params(char **argv)
 #endif
 #if ENABLE_FEATURE_FIND_NEWER
                else if (parm == PARM_newer) {
-                       action_newer *ap;
                        struct stat stat_newer;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
-                       xstat(arg1, &stat_newer);
+                       action_newer *ap;
                        ap = ALLOC_ACTION(newer);
+                       xstat(arg1, &stat_newer);
                        ap->newer_mtime = stat_newer.st_mtime;
                }
 #endif
 #if ENABLE_FEATURE_FIND_INUM
                else if (parm == PARM_inum) {
                        action_inum *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(inum);
                        ap->inode_num = xatoul(arg1);
                }
 #endif
-#if ENABLE_FEATURE_FIND_EXEC
-               else if (parm == PARM_exec) {
-                       int i;
-                       action_exec *ap;
-                       need_print = 0;
-                       USE_FEATURE_FIND_NOT( invert_flag = 0; )
-                       ap = ALLOC_ACTION(exec);
-                       ap->exec_argv = ++argv; /* first arg after -exec */
-                       ap->exec_argc = 0;
-                       while (1) {
-                               if (!*argv) /* did not see ';' util end */
-                                       bb_error_msg_and_die(bb_msg_requires_arg, arg);
-                               if (LONE_CHAR(argv[0], ';'))
-                                       break;
-                               argv++;
-                               ap->exec_argc++;
-                       }
-                       if (ap->exec_argc == 0)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
-                       ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
-                       i = ap->exec_argc;
-                       while (i--)
-                               ap->subst_count[i] = count_subst(ap->exec_argv[i]);
-               }
-#endif
 #if ENABLE_FEATURE_FIND_USER
                else if (parm == PARM_user) {
                        action_user *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(user);
                        ap->uid = bb_strtou(arg1, NULL, 10);
                        if (errno)
@@ -583,59 +749,53 @@ static action*** parse_params(char **argv)
 #if ENABLE_FEATURE_FIND_GROUP
                else if (parm == PARM_group) {
                        action_group *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(group);
                        ap->gid = bb_strtou(arg1, NULL, 10);
                        if (errno)
                                ap->gid = xgroup2gid(arg1);
                }
 #endif
-#if ENABLE_FEATURE_FIND_DEPTH
-               else if (parm == PARM_depth) {
-                       recurse_flags |= action_depthFirst;
-               }
-#endif
-#if ENABLE_FEATURE_FIND_PAREN
-               else if (parm == PARM_char_brace) {
-                       action_paren *ap;
-                       char **endarg;
-                       unsigned nested = 1;
-
-                       endarg = argv;
-                       while (1) {
-                               if (!*++endarg)
-                                       bb_error_msg_and_die("unpaired '('");
-                               if (LONE_CHAR(*endarg, '('))
-                                       nested++;
-                               else if (LONE_CHAR(*endarg, ')') && !--nested) {
-                                       *endarg = NULL;
-                                       break;
-                               }
-                       }
-                       ap = ALLOC_ACTION(paren);
-                       ap->subexpr = parse_params(argv + 1);
-                       *endarg = (char*) ")"; /* restore NULLed parameter */
-                       argv = endarg;
-               }
-#endif
-#if ENABLE_FEATURE_FIND_PRUNE
-               else if (parm == PARM_prune) {
-                       USE_FEATURE_FIND_NOT( invert_flag = 0; )
-                       (void) ALLOC_ACTION(prune);
-               }
-#endif
 #if ENABLE_FEATURE_FIND_SIZE
                else if (parm == PARM_size) {
+/* -size n[bckw]: file uses n units of space
+ * b (default): units are 512-byte blocks
+ * c: 1 byte
+ * k: kilobytes
+ * w: 2-byte words
+ */
+#if ENABLE_LFS
+#define XATOU_SFX xatoull_sfx
+#else
+#define XATOU_SFX xatoul_sfx
+#endif
+                       static const struct suffix_mult find_suffixes[] = {
+                               { "c", 1 },
+                               { "w", 2 },
+                               { "", 512 },
+                               { "b", 512 },
+                               { "k", 1024 },
+                               { }
+                       };
                        action_size *ap;
-                       if (!*++argv)
-                               bb_error_msg_and_die(bb_msg_requires_arg, arg);
                        ap = ALLOC_ACTION(size);
-                       ap->size = XATOOFF(arg1);
+                       ap->size_char = arg1[0];
+                       ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
                }
 #endif
-               else
+#if ENABLE_FEATURE_FIND_CONTEXT
+               else if (parm == PARM_context) {
+                       action_context *ap;
+                       ap = ALLOC_ACTION(context);
+                       ap->context = NULL;
+                       /* SELinux headers erroneously declare non-const parameter */
+                       if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
+                               bb_simple_perror_msg(arg1);
+               }
+#endif
+               else {
+                       bb_error_msg("unrecognized: %s", arg);
                        bb_show_usage();
+               }
                argv++;
        }
        return appp;
@@ -643,19 +803,26 @@ static action*** parse_params(char **argv)
 }
 
 
-int find_main(int argc, char **argv);
+int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int find_main(int argc, char **argv)
 {
-       static const char * const options[] = {
-               "-follow",
-USE_FEATURE_FIND_XDEV( "-xdev", )
-               NULL
+       static const char options[] ALIGN1 =
+                         "-follow\0"
+USE_FEATURE_FIND_XDEV(    "-xdev\0"    )
+USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
+                         ;
+       enum {
+                         OPT_FOLLOW,
+USE_FEATURE_FIND_XDEV(    OPT_XDEV    ,)
+USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
        };
 
-       bool dereference = FALSE;
        char *arg;
        char **argp;
        int i, firstopt, status = EXIT_SUCCESS;
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+       int maxdepth = INT_MAX;
+#endif
 
        for (firstopt = 1; firstopt < argc; firstopt++) {
                if (argv[firstopt][0] == '-')
@@ -676,19 +843,19 @@ USE_FEATURE_FIND_XDEV( "-xdev", )
 /* All options always return true. They always take effect
  * rather than being processed only when their place in the
  * expression is reached.
- * We implement: -follow, -xdev
+ * We implement: -follow, -xdev, -maxdepth
  */
        /* Process options, and replace then with -a */
        /* (-a will be ignored by recursive parser later) */
        argp = &argv[firstopt];
        while ((arg = argp[0])) {
-               i = index_in_str_array(options, arg);
-               if (i == 0) { /* -follow */
-                       dereference = TRUE;
+               int opt = index_in_strings(options, arg);
+               if (opt == OPT_FOLLOW) {
+                       recurse_flags |= ACTION_FOLLOWLINKS;
                        argp[0] = (char*)"-a";
                }
 #if ENABLE_FEATURE_FIND_XDEV
-               else if (i == 1) { /* -xdev */
+               if (opt == OPT_XDEV) {
                        struct stat stbuf;
                        if (!xdev_count) {
                                xdev_count = firstopt - 1;
@@ -703,6 +870,16 @@ USE_FEATURE_FIND_XDEV( "-xdev", )
                        }
                        argp[0] = (char*)"-a";
                }
+#endif
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+               if (opt == OPT_MAXDEPTH) {
+                       if (!argp[1])
+                               bb_show_usage();
+                       maxdepth = xatoi_u(argp[1]);
+                       argp[0] = (char*)"-a";
+                       argp[1] = (char*)"-a";
+                       argp++;
+               }
 #endif
                argp++;
        }
@@ -711,10 +888,16 @@ USE_FEATURE_FIND_XDEV( "-xdev", )
 
        for (i = 1; i < firstopt; i++) {
                if (!recursive_action(argv[i],
-                               recurse_flags|(1<<dereference), /* flags */
+                               recurse_flags /* flags */
                                fileAction,     /* file action */
                                fileAction,     /* dir action */
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+                               /* double cast suppresses
+                                * "cast to ptr from int of different size" */
+                               (void*)(ptrdiff_t)maxdepth,/* user data */
+#else
                                NULL,           /* user data */
+#endif
                                0))             /* depth */
                        status = EXIT_FAILURE;
        }