From 6be3a5242ce4855734a4cdd5770b6ea7adaf2b3d Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Thu, 19 Jun 2014 11:32:11 +0200
Subject: find: exit code fixes for find -exec

function                                             old     new   delta
func_exec                                            127     100     -27

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 findutils/find.c     | 17 +++++++++--------
 testsuite/find.tests | 26 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/findutils/find.c b/findutils/find.c
index 8ac3da7a0..493f72e61 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -634,6 +634,7 @@ static int do_exec(action_exec *ap, const char *fileName)
 			} else {
 				int j = 0;
 				while (ap->filelist[j]) {
+					/* 2nd arg here should be ap->subst_count[i], but it is always 1: */
 					*pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
 					free(ap->filelist[j]);
 					j++;
@@ -669,16 +670,16 @@ ACTF(exec)
 {
 # if ENABLE_FEATURE_FIND_EXEC_PLUS
 	if (ap->filelist) {
-		int rc = 0;
+		int rc;
 
+		ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
+		ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
+		ap->file_len += strlen(fileName) + sizeof(char*) + 1;
 		/* If we have lots of files already, exec the command */
+		rc = 1;
 		if (ap->file_len >= 32*1024)
 			rc = do_exec(ap, NULL);
-
-		ap->file_len += strlen(fileName) + sizeof(char*) + 1;
-		ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
-		ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
-		return rc == 0; /* return 1 if exitcode 0 */
+		return rc;
 	}
 # endif
 	return do_exec(ap, fileName);
@@ -698,8 +699,8 @@ static int flush_exec_plus(void)
 #  if ENABLE_FEATURE_FIND_NOT
 					if (ap->invert) rc = !rc;
 #  endif
-					if (rc)
-						return rc;
+					if (rc == 0)
+						return 1;
 				}
 			}
 		}
diff --git a/testsuite/find.tests b/testsuite/find.tests
index 345d1e82e..f041106c3 100755
--- a/testsuite/find.tests
+++ b/testsuite/find.tests
@@ -15,6 +15,32 @@ testing "find -type f" \
 	"./testfile\n" \
 	"" ""
 
+optional FEATURE_FIND_EXEC
+testing "find -exec exitcode 1" \
+	"cd find.tempdir && find testfile -exec true {} \; 2>&1; echo \$?" \
+	"0\n" \
+	"" ""
+SKIP=
+optional FEATURE_FIND_EXEC_PLUS
+testing "find -exec exitcode 2" \
+	"cd find.tempdir && find testfile -exec true {} + 2>&1; echo \$?" \
+	"0\n" \
+	"" ""
+SKIP=
+# Surprisingly, "-exec false ;" results in exitcode 0! "-exec false +" is different!!!
+optional FEATURE_FIND_EXEC
+testing "find -exec exitcode 3" \
+	"cd find.tempdir && find testfile -exec false {} \; 2>&1; echo \$?" \
+	"0\n" \
+	"" ""
+SKIP=
+optional FEATURE_FIND_EXEC_PLUS
+testing "find -exec exitcode 4" \
+	"cd find.tempdir && find testfile -exec false {} + 2>&1; echo \$?" \
+	"1\n" \
+	"" ""
+SKIP=
+
 # testing "description" "command" "result" "infile" "stdin"
 
 rm -rf find.tempdir
-- 
cgit v1.2.3