1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:54:58 +00:00

grep: Adapt maximum line size depending on file size

Otherwise, we would never stop trying to fetch new lines if the next
newline is found beyond a range that the (default sized) buffer can
hold.
This commit is contained in:
Tim Schumacher 2022-03-14 17:59:03 +01:00 committed by Andreas Kling
parent ab794a199b
commit c1004b095e

View file

@ -195,8 +195,17 @@ ErrorOr<int> serenity_main(Main::Arguments args)
return false;
}
auto file_size_or_error = Core::File::size(filename);
if (file_size_or_error.is_error()) {
if (!suppress_errors)
warnln("Failed to retrieve size of {}: {}", filename, strerror(file_size_or_error.error().code()));
return false;
}
auto file_size = file_size_or_error.release_value();
for (size_t line_number = 1; file->can_read_line(); ++line_number) {
auto line = file->read_line();
auto line = file->read_line(file_size);
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)