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

LibCore+cat: Switch Core::System::read/write to take a Span of bytes

In the spirit of the Core::System name space having "modern" facades
for classically C functions / Kernel interfaces, it seems appropriate
that we should take Span's of data instead of raw pointer + length
arguments.
This commit is contained in:
Brian Gianforcaro 2021-11-27 13:02:33 -08:00 committed by Andreas Kling
parent eb896aa33e
commit fcc00c9a27
3 changed files with 14 additions and 11 deletions

View file

@ -45,15 +45,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::pledge("stdio", nullptr));
Array<u8, 32768> buffer;
for (auto& fd : fds) {
for (;;) {
char buffer[32768];
auto nread = TRY(Core::System::read(fd, buffer, sizeof(buffer)));
auto buffer_span = buffer.span();
auto nread = TRY(Core::System::read(fd, buffer_span));
if (nread == 0)
break;
ssize_t already_written = 0;
while (already_written < nread)
already_written += TRY(Core::System::write(STDOUT_FILENO, buffer + already_written, nread - already_written));
buffer_span = buffer_span.trim(nread);
while (!buffer_span.is_empty()) {
auto already_written = TRY(Core::System::write(STDOUT_FILENO, buffer_span));
buffer_span = buffer_span.slice(already_written);
}
}
TRY(Core::System::close(fd));
}