blob: 963ea40c006794ec35dec8c42ca05eafd8c87b65 [file] [edit]
//===-- Linux implementation of isatty ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/unistd/isatty.h"
#include "hdr/sys_ioctl_macros.h" // For ioctl numbers.
#include "src/__support/OSUtil/linux/syscall_wrappers/ioctl.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
constexpr int INIT_VAL = 0x1234abcd;
int line_d_val = INIT_VAL;
// This gets the line dicipline of the terminal. When called on something that
// isn't a terminal it doesn't change line_d_val and returns -1.
auto result = linux_syscalls::ioctl(fd, TIOCGETD, &line_d_val);
if (result.has_value())
return 1;
libc_errno = result.error();
return 0;
}
} // namespace LIBC_NAMESPACE_DECL