WASI isatty

This commit is contained in:
Benjamin Feng 2019-11-19 20:17:00 -06:00
parent 14e9c7d1f2
commit b88bb93af3
3 changed files with 21 additions and 2 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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;
}