From 6f9e6e63b6bf1b9c79bcad4da1ac9c5886db836f Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Thu, 11 Feb 2021 21:55:58 +0330 Subject: [PATCH] grep: Exit with 1 if nothing matches --- Userland/Utilities/grep.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Utilities/grep.cpp b/Userland/Utilities/grep.cpp index fb68753327..97d26d49e9 100644 --- a/Userland/Utilities/grep.cpp +++ b/Userland/Utilities/grep.cpp @@ -188,6 +188,7 @@ int main(int argc, char** argv) } }; + bool did_match_something = false; if (!files.size() && !recursive) { char* line = nullptr; size_t line_len = 0; @@ -201,7 +202,9 @@ int main(int argc, char** argv) if (is_binary && binary_mode == BinaryFileMode::Skip) return 1; - if (matches(line_view, "stdin", false, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary) + auto matched = matches(line_view, "stdin", false, is_binary); + did_match_something = did_match_something || matched; + if (matched && is_binary && binary_mode == BinaryFileMode::Binary) return 0; } } else { @@ -217,5 +220,5 @@ int main(int argc, char** argv) } } - return 0; + return did_match_something ? 0 : 1; }