From 8b48b6959ca37a8daedc4cff33f82d759f35f732 Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Thu, 26 Jul 2018 14:32:10 +0200
Subject: [PATCH] fixup! Implement isatty

---
 src/platform/src/linux/mod.rs | 8 ++++++++
 src/platform/src/redox/mod.rs | 9 +++++++++
 src/platform/src/types.rs     | 9 +++++++++
 src/sys_ioctl/src/lib.rs      | 1 -
 src/unistd/src/lib.rs         | 5 +----
 5 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs
index 4b61ba75..3b1683bc 100644
--- a/src/platform/src/linux/mod.rs
+++ b/src/platform/src/linux/mod.rs
@@ -3,6 +3,8 @@ use core::{mem, ptr};
 use errno;
 use types::*;
 
+const TIOCGWINSZ: u32 = 0x5413;
+
 const AT_FDCWD: c_int = -100;
 const AT_EMPTY_PATH: c_int = 0x1000;
 const AT_REMOVEDIR: c_int = 0x200;
@@ -215,9 +217,15 @@ pub fn getuid() -> uid_t {
 }
 
 pub fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int {
+    // TODO: Somehow support varargs to syscall??
     e(unsafe { syscall!(IOCTL, fd, request, out) }) as c_int
 }
 
+pub fn isatty(fd: c_int) -> c_int {
+    let mut winsize = winsize::default();
+    (ioctl(fd, TIOCGWINSZ as c_ulong, &mut winsize as *mut _ as *mut c_void) == 0) as c_int
+}
+
 pub fn kill(pid: pid_t, sig: c_int) -> c_int {
     e(unsafe { syscall!(KILL, pid, sig) }) as c_int
 }
diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs
index f778d766..0ee98d55 100644
--- a/src/platform/src/redox/mod.rs
+++ b/src/platform/src/redox/mod.rs
@@ -470,6 +470,15 @@ pub fn getuid() -> uid_t {
     e(syscall::getuid()) as pid_t
 }
 
+pub fn isatty(fd: c_int) -> c_int {
+    syscall::dup(fd as usize, b"termios")
+        .map(|fd| {
+            let _ = syscall::close(fd);
+            1
+        })
+        .unwrap_or(0)
+}
+
 pub fn kill(pid: pid_t, sig: c_int) -> c_int {
     e(syscall::kill(pid, sig as usize)) as c_int
 }
diff --git a/src/platform/src/types.rs b/src/platform/src/types.rs
index 6d4902ed..5124a52a 100644
--- a/src/platform/src/types.rs
+++ b/src/platform/src/types.rs
@@ -188,3 +188,12 @@ pub struct dirent {
     pub d_type: c_uchar,
     pub d_name: [c_char; 256]
 }
+
+#[repr(C)]
+#[derive(Default)]
+pub struct winsize {
+    ws_row: c_ushort,
+    ws_col: c_ushort,
+    ws_xpixel: c_ushort,
+    ws_ypixel: c_ushort
+}
diff --git a/src/sys_ioctl/src/lib.rs b/src/sys_ioctl/src/lib.rs
index de1f7e0d..ac99a318 100644
--- a/src/sys_ioctl/src/lib.rs
+++ b/src/sys_ioctl/src/lib.rs
@@ -9,7 +9,6 @@ pub mod inner {
     use self::platform::types::*;
 
     #[repr(C)]
-    #[derive(Default)]
     pub struct winsize {
         ws_row: c_ushort,
         ws_col: c_ushort,
diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs
index a6a1ab29..49c5a6b5 100644
--- a/src/unistd/src/lib.rs
+++ b/src/unistd/src/lib.rs
@@ -6,12 +6,10 @@ extern crate errno;
 extern crate platform;
 extern crate stdio;
 extern crate string;
-extern crate sys_ioctl;
 
 use core::{ptr, slice};
 
 use platform::types::*;
-use sys_ioctl::{ioctl, winsize};
 
 pub use brk::*;
 pub use getopt::*;
@@ -264,8 +262,7 @@ pub extern "C" fn getwd(path_name: *mut c_char) -> *mut c_char {
 
 #[no_mangle]
 pub extern "C" fn isatty(fd: c_int) -> c_int {
-    let mut winsize = winsize::default();
-    (ioctl(fd, sys_ioctl::TIOCGWINSZ as c_ulong, &mut winsize as *mut _ as *mut c_void) == 0) as c_int
+    platform::isatty(fd)
 }
 
 // #[no_mangle]
-- 
GitLab