Commit b88bb93af3

Benjamin Feng <contact@fengb.me>
2019-11-20 03:17:00
WASI isatty
1 parent 14e9c7d
Changed files (3)
lib
lib/std/os/bits/wasi.zig
@@ -138,7 +138,7 @@ pub const FDFLAG_NONBLOCK: fdflags_t = 0x0004;
 pub const FDFLAG_RSYNC: fdflags_t = 0x0008;
 pub const FDFLAG_SYNC: fdflags_t = 0x0010;
 
-const fdstat_t = extern struct {
+pub const fdstat_t = extern struct {
     fs_filetype: filetype_t,
     fs_flags: fdflags_t,
     fs_rights_base: rights_t,
lib/std/os/wasi.zig
@@ -108,3 +108,22 @@ pub fn clock_gettime(clock_id: i32, tp: *timespec) errno_t {
     };
     return 0;
 }
+
+pub fn isatty(fd: fd_t) bool {
+    var statbuf: fdstat_t = undefined;
+    const err = fd_fdstat_get(fd, &statbuf);
+    if (err != 0) {
+        // errno = err;
+        return false;
+    }
+
+    // A tty is a character device that we can't seek or tell on.
+    if (statbuf.fs_filetype != FILETYPE_CHARACTER_DEVICE or
+        (statbuf.fs_rights_base & (RIGHT_FD_SEEK | RIGHT_FD_TELL)) != 0)
+    {
+        // errno = ENOTTY;
+        return false;
+    }
+
+    return true;
+}
lib/std/os.zig
@@ -1522,7 +1522,7 @@ pub fn isatty(handle: fd_t) bool {
         return system.isatty(handle) != 0;
     }
     if (builtin.os == .wasi) {
-        @compileError("TODO implement std.os.isatty for WASI");
+        return system.isatty(handle);
     }
     if (builtin.os == .linux) {
         var wsz: linux.winsize = undefined;