*: add -Wunused-parameter; fix resulting breakage
[oweals/busybox.git] / findutils / find.c
index 07cae971eca889725c00000b420070ae10cdac3c..50c790161ea0d94e471f74130249e50c0da53f03 100644 (file)
@@ -74,9 +74,11 @@ 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)
+#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;)
+                         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))
@@ -188,8 +190,9 @@ ACTF(name)
                if (*tmp == '/')
                        tmp++;
        }
-       return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
+       return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
 }
+
 #if ENABLE_FEATURE_FIND_PATH
 ACTF(path)
 {
@@ -277,7 +280,7 @@ ACTF(exec)
 
        rc = spawn_and_wait(argv);
        if (rc < 0)
-               bb_perror_msg("%s", argv[0]);
+               bb_simple_perror_msg(argv[0]);
 
        i = 0;
        while (argv[i])
@@ -347,7 +350,7 @@ ACTF(delete)
                rc = unlink(fileName);
        }
        if (rc < 0)
-               bb_perror_msg("%s", fileName);
+               bb_simple_perror_msg(fileName);
        return TRUE;
 }
 #endif
@@ -383,9 +386,11 @@ static int fileAction(const char *fileName, struct stat *statbuf, void *userData
 #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);
@@ -425,8 +430,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 == '+')
@@ -455,6 +461,7 @@ static action*** parse_params(char **argv)
        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      ,)
@@ -469,38 +476,38 @@ static action*** parse_params(char **argv)
        USE_FEATURE_FIND_CONTEXT(PARM_context   ,)
        };
 
-       static const char *const params[] = {
-                                "-a"      ,
-                                "-o"      ,
-       USE_FEATURE_FIND_NOT(    "!"       ,)
+       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
-                                "-print"  ,
-       USE_FEATURE_FIND_PRINT0( "-print0" ,)
-       USE_FEATURE_FIND_DEPTH(  "-depth"  ,)
-       USE_FEATURE_FIND_PRUNE(  "-prune"  ,)
-       USE_FEATURE_FIND_DELETE( "-delete" ,)
-       USE_FEATURE_FIND_EXEC(   "-exec"   ,)
-       USE_FEATURE_FIND_PAREN(  "("       ,)
+                                "-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"   ,
-       USE_FEATURE_FIND_PATH(   "-path"   ,)
-       USE_FEATURE_FIND_REGEX(  "-regex"  ,)
-       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_USER(   "-user"   ,)
-       USE_FEATURE_FIND_GROUP(  "-group"  ,)
-       USE_FEATURE_FIND_SIZE(   "-size"   ,)
-       USE_FEATURE_FIND_CONTEXT("-context",)
-               NULL
-       };
+                                "-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;
@@ -540,7 +547,7 @@ static action*** parse_params(char **argv)
  */
        while (*argv) {
                const char *arg = argv[0];
-               int parm = index_in_str_array(params, arg);
+               int parm = index_in_strings(params, arg);
                const char *arg1 = argv[1];
 
                if (parm >= PARM_name) {
@@ -652,10 +659,11 @@ static action*** parse_params(char **argv)
                        argv = endarg;
                }
 #endif
-               else if (parm == PARM_name) {
+               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) {
@@ -758,12 +766,12 @@ static action*** parse_params(char **argv)
 #define XATOU_SFX xatoul_sfx
 #endif
                        static const struct suffix_mult find_suffixes[] = {
-                               { "c", 1 },
-                               { "w", 2 },
-                               { "b"+1, 512 },
-                               { "b", 512 },
-                               { "k", 1024 },
-                               { NULL, 0 }
+                               { "c", 1 },
+                               { "w", 2 },
+                               { "", 512 },
+                               { "b", 512 },
+                               { "k", 1024 },
+                               { }
                        };
                        action_size *ap;
                        ap = ALLOC_ACTION(size);
@@ -776,8 +784,9 @@ static action*** parse_params(char **argv)
                        action_context *ap;
                        ap = ALLOC_ACTION(context);
                        ap->context = NULL;
-                       if (selinux_raw_to_trans_context(arg1, &ap->context))
-                               bb_perror_msg("%s", arg1);
+                       /* SELinux headers erroneously declare non-const parameter */
+                       if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
+                               bb_simple_perror_msg(arg1);
                }
 #endif
                else {
@@ -791,17 +800,16 @@ 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"    ,)
-USE_FEATURE_FIND_MAXDEPTH("-maxdepth",)
-               NULL
-       };
+       static const char options[] ALIGN1 =
+                         "-follow\0"
+USE_FEATURE_FIND_XDEV(    "-xdev\0"    )
+USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
+                         ;
        enum {
-               OPT_FOLLOW,
+                         OPT_FOLLOW,
 USE_FEATURE_FIND_XDEV(    OPT_XDEV    ,)
 USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
        };
@@ -838,7 +846,7 @@ USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
        /* (-a will be ignored by recursive parser later) */
        argp = &argv[firstopt];
        while ((arg = argp[0])) {
-               int opt = index_in_str_array(options, arg);
+               int opt = index_in_strings(options, arg);
                if (opt == OPT_FOLLOW) {
                        recurse_flags |= ACTION_FOLLOWLINKS;
                        argp[0] = (char*)"-a";
@@ -881,7 +889,9 @@ USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
                                fileAction,     /* file action */
                                fileAction,     /* dir action */
 #if ENABLE_FEATURE_FIND_MAXDEPTH
-                               (void*)maxdepth,/* user data */
+                               /* double cast suppresses
+                                * "cast to ptr from int of different size" */
+                               (void*)(ptrdiff_t)maxdepth,/* user data */
 #else
                                NULL,           /* user data */
 #endif