From d9360676cd043ccb0cb8415c7284b7ce4b24e57a Mon Sep 17 00:00:00 2001 From: davidot Date: Tue, 13 Sep 2022 23:52:57 +0200 Subject: [PATCH] LibCore: Add standard_{output, input, error} functions to File::Stream --- Userland/Libraries/LibCore/Stream.cpp | 17 +++++++++++++++-- Userland/Libraries/LibCore/Stream.h | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index b73f9e1640..579a23045b 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -148,6 +148,19 @@ bool File::exists(StringView filename) return !Core::System::stat(filename).is_error(); } +ErrorOr> File::standard_input() +{ + return File::adopt_fd(STDIN_FILENO, OpenMode::Read, ShouldCloseFileDescriptor::No); +} +ErrorOr> File::standard_output() +{ + return File::adopt_fd(STDOUT_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No); +} +ErrorOr> File::standard_error() +{ + return File::adopt_fd(STDERR_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No); +} + ErrorOr> File::open_file_or_standard_stream(StringView filename, OpenMode mode) { if (!filename.is_empty() && filename != "-"sv) @@ -155,9 +168,9 @@ ErrorOr> File::open_file_or_standard_stream(StringView filen switch (mode) { case OpenMode::Read: - return File::adopt_fd(STDIN_FILENO, mode); + return standard_input(); case OpenMode::Write: - return File::adopt_fd(STDOUT_FILENO, mode); + return standard_output(); default: VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index ecb68615a0..d30de3788a 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -198,6 +198,9 @@ public: static ErrorOr> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes); static bool exists(StringView filename); + static ErrorOr> standard_input(); + static ErrorOr> standard_output(); + static ErrorOr> standard_error(); static ErrorOr> open_file_or_standard_stream(StringView filename, OpenMode mode); File(File&& other) { operator=(move(other)); }