• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/errno/libc_errno.h"
15 #include <sys/ioctl.h>   // For ioctl numbers.
16 #include <sys/syscall.h> // For syscall numbers.
17 
18 namespace LIBC_NAMESPACE {
19 
20 LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
21   constexpr int INIT_VAL = 0x1234abcd;
22   int line_d_val = INIT_VAL;
23   // This gets the line dicipline of the terminal. When called on something that
24   // isn't a terminal it doesn't change line_d_val and returns -1.
25   int result =
26       LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
27   if (result == 0)
28     return 1;
29 
30   libc_errno = -result;
31   return 0;
32 }
33 
34 } // namespace LIBC_NAMESPACE
35