xref: /aosp_15_r20/external/llvm-libc/src/unistd/linux/isatty.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of isatty ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/unistd/isatty.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 #include <sys/ioctl.h>   // For ioctl numbers.
17 #include <sys/syscall.h> // For syscall numbers.
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
22   constexpr int INIT_VAL = 0x1234abcd;
23   int line_d_val = INIT_VAL;
24   // This gets the line dicipline of the terminal. When called on something that
25   // isn't a terminal it doesn't change line_d_val and returns -1.
26   int result =
27       LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
28   if (result == 0)
29     return 1;
30 
31   libc_errno = -result;
32   return 0;
33 }
34 
35 } // namespace LIBC_NAMESPACE_DECL
36