summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-11-27 13:02:33 -0800
committerAndreas Kling <kling@serenityos.org>2021-11-28 08:04:57 +0100
commitfcc00c9a276eca1213dcad7ea4d49bd329a29437 (patch)
treee406d81a12d8ab49aa87813625a6eb64c9a2ce77
parenteb896aa33eb067a9b343580dd5c7c5549434b894 (diff)
downloadserenity-fcc00c9a276eca1213dcad7ea4d49bd329a29437.zip
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.
-rw-r--r--Userland/Libraries/LibCore/System.cpp8
-rw-r--r--Userland/Libraries/LibCore/System.h4
-rw-r--r--Userland/Utilities/cat.cpp13
3 files changed, 14 insertions, 11 deletions
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp
index b6051d0851..4289b22ed5 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -157,17 +157,17 @@ ErrorOr<struct stat> stat(StringView path)
#endif
}
-ErrorOr<ssize_t> read(int fd, void* buffer, size_t buffer_size)
+ErrorOr<ssize_t> read(int fd, Bytes buffer)
{
- ssize_t rc = ::read(fd, buffer, buffer_size);
+ ssize_t rc = ::read(fd, buffer.data(), buffer.size());
if (rc < 0)
return Error::from_syscall("read"sv, -errno);
return rc;
}
-ErrorOr<ssize_t> write(int fd, void const* data, size_t data_size)
+ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer)
{
- ssize_t rc = ::write(fd, data, data_size);
+ ssize_t rc = ::write(fd, buffer.data(), buffer.size());
if (rc < 0)
return Error::from_syscall("write"sv, -errno);
return rc;
diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h
index d5e39d67ed..c90e2f0c9f 100644
--- a/Userland/Libraries/LibCore/System.h
+++ b/Userland/Libraries/LibCore/System.h
@@ -27,8 +27,8 @@ ErrorOr<int> open(StringView path, int options, ...);
ErrorOr<void> close(int fd);
ErrorOr<void> ftruncate(int fd, off_t length);
ErrorOr<struct stat> stat(StringView path);
-ErrorOr<ssize_t> read(int fd, void* buffer, size_t buffer_size);
-ErrorOr<ssize_t> write(int fd, void const* data, size_t data_size);
+ErrorOr<ssize_t> read(int fd, Bytes buffer);
+ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer);
ErrorOr<void> kill(pid_t, int signal);
ErrorOr<int> dup2(int source_fd, int destination_fd);
ErrorOr<String> ptsname(int fd);
diff --git a/Userland/Utilities/cat.cpp b/Userland/Utilities/cat.cpp
index 89f984b799..4c0d5be9e8 100644
--- a/Userland/Utilities/cat.cpp
+++ b/Userland/Utilities/cat.cpp
@@ -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));
}