authorgravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-11-19 20:17:00-06:00
committergravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-11-19 20:17:00-06:00
logb88bb93af3cf22f5a994100e037dbb4091144f0c
tree971dca551cc1a553579115c719e21e067a673fcb
parent14e9c7d1f2f9a1b583237626e6b834c8e86d6cf7

WASI isatty


3 files changed, 21 insertions(+), 2 deletions(-)

lib/std/os.zig+1-1
......@@ -1522,7 +1522,7 @@ pub fn isatty(handle: fd_t) bool {
15221522 return system.isatty(handle) != 0;
15231523 }
15241524 if (builtin.os == .wasi) {
1525 @compileError("TODO implement std.os.isatty for WASI");
1525 return system.isatty(handle);
15261526 }
15271527 if (builtin.os == .linux) {
15281528 var wsz: linux.winsize = undefined;
lib/std/os/bits/wasi.zig+1-1
......@@ -138,7 +138,7 @@ pub const FDFLAG_NONBLOCK: fdflags_t = 0x0004;
138138pub const FDFLAG_RSYNC: fdflags_t = 0x0008;
139139pub const FDFLAG_SYNC: fdflags_t = 0x0010;
140140
141const fdstat_t = extern struct {
141pub const fdstat_t = extern struct {
142142 fs_filetype: filetype_t,
143143 fs_flags: fdflags_t,
144144 fs_rights_base: rights_t,
lib/std/os/wasi.zig+19
......@@ -108,3 +108,22 @@ pub fn clock_gettime(clock_id: i32, tp: *timespec) errno_t {
108108 };
109109 return 0;
110110}
111
112pub fn isatty(fd: fd_t) bool {
113 var statbuf: fdstat_t = undefined;
114 const err = fd_fdstat_get(fd, &statbuf);
115 if (err != 0) {
116 // errno = err;
117 return false;
118 }
119
120 // A tty is a character device that we can't seek or tell on.
121 if (statbuf.fs_filetype != FILETYPE_CHARACTER_DEVICE or
122 (statbuf.fs_rights_base & (RIGHT_FD_SEEK | RIGHT_FD_TELL)) != 0)
123 {
124 // errno = ENOTTY;
125 return false;
126 }
127
128 return true;
129}