From db4a654e9f3b0df8ac8c1ac510574ea36d5d954e Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Thu, 14 Sep 2023 17:53:58 +0200 Subject: [PATCH] grep: Print filenames when counting lines and path size is less than 2 `grep --count --recursive foo` doesn't specify any file, but we should show the paths anyway. --- Userland/Utilities/grep.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Userland/Utilities/grep.cpp b/Userland/Utilities/grep.cpp index 52d840ccdf..3814d2879a 100644 --- a/Userland/Utilities/grep.cpp +++ b/Userland/Utilities/grep.cpp @@ -168,7 +168,6 @@ ErrorOr serenity_main(Main::Arguments args) patterns.append(files.take_first()); auto user_has_specified_files = !files.is_empty(); - auto user_specified_multiple_files = files.size() >= 2; PosixOptions options {}; if (case_insensitive) @@ -228,7 +227,7 @@ ErrorOr serenity_main(Main::Arguments args) auto exit_status = ExitStatus::NoLinesMatched; auto handle_file = [&matches, binary_mode, count_lines, quiet_mode, - user_specified_multiple_files, &matched_line_count, &exit_status](StringView filename, bool print_filename) -> ErrorOr { + &matched_line_count, &exit_status](StringView filename, bool print_filename) -> ErrorOr { auto file = TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read)); auto buffered_file = TRY(Core::InputBufferedFile::create(move(file))); if (filename == '-') @@ -250,7 +249,7 @@ ErrorOr serenity_main(Main::Arguments args) } if (count_lines && !quiet_mode) { - if (user_specified_multiple_files) + if (print_filename) outln("{}:{}", filename, matched_line_count); else outln("{}", matched_line_count);