summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorPeter Elliott <pelliott@ualberta.ca>2020-08-17 15:58:07 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-21 12:26:30 +0200
commitf69b419c05124fe6afc4edccef3d6e7f339e5f9b (patch)
tree744b4a00ceb5afb3c9987df176d9efaa04c7881e /Libraries
parent6c2d0dea91d054b627714d223d7ffd78683704c9 (diff)
downloadserenity-f69b419c05124fe6afc4edccef3d6e7f339e5f9b.zip
LibCore: Add File::{stdin, stdout, stderr}()
This should make it easier to get a Core::File for standard streams.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibCore/File.cpp30
-rw-r--r--Libraries/LibCore/File.h4
2 files changed, 34 insertions, 0 deletions
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<File> stdin_file;
+static RefPtr<File> stdout_file;
+static RefPtr<File> stderr_file;
+
+NonnullRefPtr<File> File::stdin()
+{
+ if (!stdin_file) {
+ stdin_file = File::construct();
+ stdin_file->open(STDIN_FILENO, IODevice::ReadOnly, ShouldCloseFileDescription::No);
+ }
+ return *stdin_file;
+}
+
+NonnullRefPtr<File> File::stdout()
+{
+ if (!stdout_file) {
+ stdout_file = File::construct();
+ stdout_file->open(STDOUT_FILENO, IODevice::WriteOnly, ShouldCloseFileDescription::No);
+ }
+ return *stdout_file;
+}
+
+NonnullRefPtr<File> 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<File> stdin();
+ static NonnullRefPtr<File> stdout();
+ static NonnullRefPtr<File> stderr();
+
private:
File(Object* parent = nullptr)
: IODevice(parent)