aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorBenjamin Feng <contact@fengb.me>2019-11-19 20:17:00 -0600
committerBenjamin Feng <contact@fengb.me>2019-11-19 20:17:00 -0600
commitb88bb93af3cf22f5a994100e037dbb4091144f0c (patch)
tree971dca551cc1a553579115c719e21e067a673fcb /lib/std
parent14e9c7d1f2f9a1b583237626e6b834c8e86d6cf7 (diff)
downloadzig-b88bb93af3cf22f5a994100e037dbb4091144f0c.tar.gz
zig-b88bb93af3cf22f5a994100e037dbb4091144f0c.zip
WASI isatty
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/os.zig2
-rw-r--r--lib/std/os/bits/wasi.zig2
-rw-r--r--lib/std/os/wasi.zig19
3 files changed, 21 insertions, 2 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 68a3a6e9f6..36c1c8dbff 100644
--- a/lib/std/os.zig
+++ b/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;
diff --git a/lib/std/os/bits/wasi.zig b/lib/std/os/bits/wasi.zig
index 36944e5639..139418ded2 100644
--- a/lib/std/os/bits/wasi.zig
+++ b/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,
diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig
index 7f030510ed..e8f4d052ea 100644
--- a/lib/std/os/wasi.zig
+++ b/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;
+}