Function isatty [src]
Test whether a file descriptor refers to a terminal.
Prototype
pub fn isatty(handle: fd_t) bool
Parameters
handle: fd_t
Source
pub fn isatty(handle: fd_t) bool {
if (native_os == .windows) {
if (fs.File.isCygwinPty(.{ .handle = handle }))
return true;
var out: windows.DWORD = undefined;
return windows.kernel32.GetConsoleMode(handle, &out) != 0;
}
if (builtin.link_libc) {
return system.isatty(handle) != 0;
}
if (native_os == .wasi) {
var statbuf: wasi.fdstat_t = undefined;
const err = wasi.fd_fdstat_get(handle, &statbuf);
if (err != .SUCCESS)
return false;
// A tty is a character device that we can't seek or tell on.
if (statbuf.fs_filetype != .CHARACTER_DEVICE)
return false;
if (statbuf.fs_rights_base.FD_SEEK or statbuf.fs_rights_base.FD_TELL)
return false;
return true;
}
if (native_os == .linux) {
while (true) {
var wsz: winsize = undefined;
const fd: usize = @bitCast(@as(isize, handle));
const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @intFromPtr(&wsz));
switch (linux.E.init(rc)) {
.SUCCESS => return true,
.INTR => continue,
else => return false,
}
}
}
return system.isatty(handle) != 0;
}