grep: for -L, exitcode 0 means files *without* matches were found, closes 13151

This is a recent change in GNU grep as well (after 3.1)

function                                             old     new   delta
grep_file                                           1215    1228     +13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-08-15 00:39:30 +02:00
parent 5c69ad0ecd
commit a77f3ecf68
2 changed files with 22 additions and 15 deletions

View File

@ -167,13 +167,11 @@ enum {
OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0, OPT_z = IF_EXTRA_COMPAT( (1 << OPTBIT_z)) + 0,
}; };
#define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l)
#define PRINT_LINE_NUM (option_mask32 & OPT_n) #define PRINT_LINE_NUM (option_mask32 & OPT_n)
#define BE_QUIET (option_mask32 & OPT_q) #define BE_QUIET (option_mask32 & OPT_q)
#define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s) #define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s)
#define PRINT_MATCH_COUNTS (option_mask32 & OPT_c) #define PRINT_MATCH_COUNTS (option_mask32 & OPT_c)
#define FGREP_FLAG (option_mask32 & OPT_F) #define FGREP_FLAG (option_mask32 & OPT_F)
#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
#define NUL_DELIMITED (option_mask32 & OPT_z) #define NUL_DELIMITED (option_mask32 & OPT_z)
struct globals { struct globals {
@ -476,13 +474,13 @@ static int grep_file(FILE *file)
* even if errors were detected" */ * even if errors were detected" */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* if we're just printing filenames, we stop after the first match */ /* -l "print filenames with matches": stop after the first match */
if (PRINT_FILES_WITH_MATCHES) { if (option_mask32 & OPT_l) {
puts(cur_file); puts(cur_file);
/* fall through to "return 1" */ return 1;
} }
/* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */ /* -L "print filenames without matches": return early too */
return 1; /* one match */ return 0; /* 0: we do not print fname, hence it's "not a match" */
} }
#if ENABLE_FEATURE_GREP_CONTEXT #if ENABLE_FEATURE_GREP_CONTEXT
@ -602,15 +600,16 @@ static int grep_file(FILE *file)
printf("%d\n", nmatches); printf("%d\n", nmatches);
} }
/* grep -L: print just the filename */ /* grep -L: "print filenames without matches" */
if (PRINT_FILES_WITHOUT_MATCHES) { if (option_mask32 & OPT_L) {
/* nmatches is zero, no need to check it: /* nmatches is zero, no need to check it:
* we return 1 early if we detected a match * we return 0 early if -L and we detect a match
* and PRINT_FILES_WITHOUT_MATCHES is set */ */
puts(cur_file); puts(cur_file);
return 1; /* 1: we printed fname, hence it's "a match" */
} }
return nmatches; return nmatches != 0; /* we return not a count, but a boolean */
} }
#if ENABLE_FEATURE_CLEAN_UP #if ENABLE_FEATURE_CLEAN_UP
@ -687,7 +686,7 @@ static int FAST_FUNC file_action_grep(const char *filename,
return 0; return 0;
} }
cur_file = filename; cur_file = filename;
*(int*)matched += grep_file(file); *(int*)matched |= grep_file(file);
fclose(file); fclose(file);
return 1; return 1;
} }
@ -844,7 +843,7 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
if (!(option_mask32 & OPT_h)) if (!(option_mask32 & OPT_h))
print_filename = 1; print_filename = 1;
matched += grep_dir(cur_file); matched |= grep_dir(cur_file);
goto grep_done; goto grep_done;
} }
} }
@ -857,7 +856,7 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
continue; continue;
} }
} }
matched += grep_file(file); matched |= grep_file(file);
fclose_if_not_stdin(file); fclose_if_not_stdin(file);
grep_done: ; grep_done: ;
} while (*argv && *++argv); } while (*argv && *++argv);

View File

@ -96,6 +96,14 @@ testing "grep -x -F (partial match 1)" "grep -x -F foo input ; echo \$?" \
testing "grep -x -F (partial match 2)" "grep -x -F foo input ; echo \$?" \ testing "grep -x -F (partial match 2)" "grep -x -F foo input ; echo \$?" \
"1\n" "bar foo\n" "" "1\n" "bar foo\n" ""
# -L "show filenames which do not match" has inverted exitcode (if it printed something, it's "success")
testing "grep -L exitcode 0" "grep -L qwe input; echo \$?" \
"input\n0\n" "asd\n" ""
testing "grep -L exitcode 0 #2" "grep -L qwe input -; echo \$?" \
"(standard input)\n0\n" "qwe\n" "asd\n"
testing "grep -L exitcode 1" "grep -L qwe input; echo \$?" \
"1\n" "qwe\n" ""
optional EGREP optional EGREP
testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
"b\ar\nfoo\nbaz" "b\ar\nfoo\nbaz"