From f69b419c05124fe6afc4edccef3d6e7f339e5f9b Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Mon, 17 Aug 2020 15:58:07 -0600 Subject: LibCore: Add File::{stdin, stdout, stderr}() This should make it easier to get a Core::File for standard streams. --- Libraries/LibCore/File.cpp | 30 ++++++++++++++++++++++++++++++ Libraries/LibCore/File.h | 4 ++++ 2 files changed, 34 insertions(+) (limited to 'Libraries') diff --git a/Libraries/LibCore/File.cpp b/Libraries/LibCore/File.cpp index 5b6e0abb7b..db9edd5cca 100644 --- a/Libraries/LibCore/File.cpp +++ b/Libraries/LibCore/File.cpp @@ -203,4 +203,34 @@ String File::read_link(const StringView& link_path) #endif +static RefPtr stdin_file; +static RefPtr stdout_file; +static RefPtr stderr_file; + +NonnullRefPtr File::stdin() +{ + if (!stdin_file) { + stdin_file = File::construct(); + stdin_file->open(STDIN_FILENO, IODevice::ReadOnly, ShouldCloseFileDescription::No); + } + return *stdin_file; +} + +NonnullRefPtr File::stdout() +{ + if (!stdout_file) { + stdout_file = File::construct(); + stdout_file->open(STDOUT_FILENO, IODevice::WriteOnly, ShouldCloseFileDescription::No); + } + return *stdout_file; +} + +NonnullRefPtr File::stderr() +{ + if (!stderr_file) { + stderr_file = File::construct(); + stderr_file->open(STDERR_FILENO, IODevice::WriteOnly, ShouldCloseFileDescription::No); + } + return *stderr_file; +} } diff --git a/Libraries/LibCore/File.h b/Libraries/LibCore/File.h index d098fb41fe..7e8b35a32c 100644 --- a/Libraries/LibCore/File.h +++ b/Libraries/LibCore/File.h @@ -57,6 +57,10 @@ public: }; bool open(int fd, IODevice::OpenMode, ShouldCloseFileDescription); + static NonnullRefPtr stdin(); + static NonnullRefPtr stdout(); + static NonnullRefPtr stderr(); + private: File(Object* parent = nullptr) : IODevice(parent) -- cgit v1.2.3