summaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorbruvzg <7645683+bruvzg@users.noreply.github.com>2024-04-26 10:34:35 +0300
committerbruvzg <7645683+bruvzg@users.noreply.github.com>2024-11-06 13:33:38 +0200
commit76164c2aa9e94a0002ed266ac6b6ba5193801bb3 (patch)
treeb1be7cb93e5a9cea819b3fd4a0e2a118168f16f0 /drivers
parent87318a2fb7fffeb72adca934e31915be077c3d1f (diff)
downloadredot-engine-76164c2aa9e94a0002ed266ac6b6ba5193801bb3.tar.gz
[OS] Add functions to determine standard I/O device type.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/unix/os_unix.cpp85
-rw-r--r--drivers/unix/os_unix.h6
2 files changed, 87 insertions, 4 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp
index 8a9b130068..fead94fd75 100644
--- a/drivers/unix/os_unix.cpp
+++ b/drivers/unix/os_unix.cpp
@@ -77,6 +77,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
+#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
@@ -182,9 +183,87 @@ Vector<String> OS_Unix::get_video_adapter_driver_info() const {
return Vector<String>();
}
-String OS_Unix::get_stdin_string() {
- char buff[1024];
- return String::utf8(fgets(buff, 1024, stdin));
+String OS_Unix::get_stdin_string(int64_t p_buffer_size) {
+ Vector<uint8_t> data;
+ data.resize(p_buffer_size);
+ if (fgets((char *)data.ptrw(), data.size(), stdin)) {
+ return String::utf8((char *)data.ptr());
+ }
+ return String();
+}
+
+PackedByteArray OS_Unix::get_stdin_buffer(int64_t p_buffer_size) {
+ Vector<uint8_t> data;
+ data.resize(p_buffer_size);
+ size_t sz = fread((void *)data.ptrw(), 1, data.size(), stdin);
+ if (sz > 0) {
+ data.resize(sz);
+ return data;
+ }
+ return PackedByteArray();
+}
+
+OS_Unix::StdHandleType OS_Unix::get_stdin_type() const {
+ int h = fileno(stdin);
+ if (h == -1) {
+ return STD_HANDLE_INVALID;
+ }
+
+ if (isatty(h)) {
+ return STD_HANDLE_CONSOLE;
+ }
+ struct stat statbuf;
+ if (fstat(h, &statbuf) < 0) {
+ return STD_HANDLE_UNKNOWN;
+ }
+ if (S_ISFIFO(statbuf.st_mode)) {
+ return STD_HANDLE_PIPE;
+ } else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) {
+ return STD_HANDLE_FILE;
+ }
+ return STD_HANDLE_UNKNOWN;
+}
+
+OS_Unix::StdHandleType OS_Unix::get_stdout_type() const {
+ int h = fileno(stdout);
+ if (h == -1) {
+ return STD_HANDLE_INVALID;
+ }
+
+ if (isatty(h)) {
+ return STD_HANDLE_CONSOLE;
+ }
+ struct stat statbuf;
+ if (fstat(h, &statbuf) < 0) {
+ return STD_HANDLE_UNKNOWN;
+ }
+ if (S_ISFIFO(statbuf.st_mode)) {
+ return STD_HANDLE_PIPE;
+ } else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) {
+ return STD_HANDLE_FILE;
+ }
+ return STD_HANDLE_UNKNOWN;
+}
+
+OS_Unix::StdHandleType OS_Unix::get_stderr_type() const {
+ int h = fileno(stderr);
+ if (h == -1) {
+ return STD_HANDLE_INVALID;
+ }
+
+ if (isatty(h)) {
+ return STD_HANDLE_CONSOLE;
+ }
+ struct stat statbuf;
+ if (fstat(h, &statbuf) < 0) {
+ return STD_HANDLE_UNKNOWN;
+ }
+ if (S_ISFIFO(statbuf.st_mode)) {
+ return STD_HANDLE_PIPE;
+ } else if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) {
+ return STD_HANDLE_FILE;
+ }
+ return STD_HANDLE_UNKNOWN;
}
Error OS_Unix::get_entropy(uint8_t *r_buffer, int p_bytes) {
diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h
index 3add5df055..2c7920c142 100644
--- a/drivers/unix/os_unix.h
+++ b/drivers/unix/os_unix.h
@@ -58,7 +58,11 @@ public:
virtual Vector<String> get_video_adapter_driver_info() const override;
- virtual String get_stdin_string() override;
+ virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
+ virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
+ virtual StdHandleType get_stdin_type() const override;
+ virtual StdHandleType get_stdout_type() const override;
+ virtual StdHandleType get_stderr_type() const override;
virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;