find: fix a regression introduced with -HLP support
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 9 Jan 2014 15:07:11 +0000 (16:07 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 9 Jan 2014 15:08:09 +0000 (16:08 +0100)
function                                             old     new   delta
find_main                                            294     342     +48

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
findutils/find.c
testsuite/find.tests [new file with mode: 0755]

index 53d8239c72572e2f538fec656693d89f47264f54..5d5e24bfb35a8fa26ca0f2c319c2d25f393dad03 100644 (file)
@@ -1291,9 +1291,27 @@ int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int find_main(int argc UNUSED_PARAM, char **argv)
 {
        int i, firstopt, status = EXIT_SUCCESS;
+       char **past_HLP, *saved;
 
        INIT_G();
 
+       /* "find -type f" + getopt("+HLP") => disaster.
+        * Need to avoid getopt running into a non-HLP option.
+        * Do this by temporarily storing NULL there:
+        */
+       past_HLP = argv;
+       for (;;) {
+               saved = *++past_HLP;
+               if (!saved)
+                       break;
+               if (saved[0] != '-')
+                       break;
+               if (!saved[1])
+                       break; /* it is "-" */
+               if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
+                       break;
+       }
+       *past_HLP = NULL;
        /* "+": stop on first non-option */
        i = getopt32(argv, "+HLP");
        if (i & (1<<0))
@@ -1301,7 +1319,8 @@ int find_main(int argc UNUSED_PARAM, char **argv)
        if (i & (1<<1))
                G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
        /* -P is default and is ignored */
-       argv += optind;
+       argv = past_HLP; /* same result as "argv += optind;" */
+       *past_HLP = saved;
 
        for (firstopt = 0; argv[firstopt]; firstopt++) {
                if (argv[firstopt][0] == '-')
diff --git a/testsuite/find.tests b/testsuite/find.tests
new file mode 100755 (executable)
index 0000000..345d1e8
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# Copyright 2014 by Denys Vlasenko <vda.linux@googlemail.com>
+# Licensed under GPLv2, see file LICENSE in this source tree.
+
+. ./testing.sh
+
+# testing "description" "command" "result" "infile" "stdin"
+
+mkdir -p find.tempdir
+touch find.tempdir/testfile
+
+testing "find -type f" \
+       "cd find.tempdir && find -type f 2>&1" \
+       "./testfile\n" \
+       "" ""
+
+# testing "description" "command" "result" "infile" "stdin"
+
+rm -rf find.tempdir
+
+exit $FAILCOUNT