xref: /llvm-project/libc/src/unistd/linux/isatty.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
17639ba69SMichael Jones //===-- Linux implementation of isatty ------------------------------------===//
27639ba69SMichael Jones //
37639ba69SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47639ba69SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
57639ba69SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67639ba69SMichael Jones //
77639ba69SMichael Jones //===----------------------------------------------------------------------===//
87639ba69SMichael Jones 
97639ba69SMichael Jones #include "src/unistd/isatty.h"
107639ba69SMichael Jones 
117639ba69SMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
127639ba69SMichael Jones #include "src/__support/common.h"
137639ba69SMichael Jones 
14*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
15803437dbSMichael Jones #include "src/errno/libc_errno.h"
167639ba69SMichael Jones #include <sys/ioctl.h>   // For ioctl numbers.
177639ba69SMichael Jones #include <sys/syscall.h> // For syscall numbers.
187639ba69SMichael Jones 
19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
207639ba69SMichael Jones 
217639ba69SMichael Jones LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
227639ba69SMichael Jones   constexpr int INIT_VAL = 0x1234abcd;
237639ba69SMichael Jones   int line_d_val = INIT_VAL;
247639ba69SMichael Jones   // This gets the line dicipline of the terminal. When called on something that
257639ba69SMichael Jones   // isn't a terminal it doesn't change line_d_val and returns -1.
26f0a3954eSMichael Jones   int result =
27b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
287639ba69SMichael Jones   if (result == 0)
297639ba69SMichael Jones     return 1;
307639ba69SMichael Jones 
31204587a3SSiva Chandra Reddy   libc_errno = -result;
327639ba69SMichael Jones   return 0;
337639ba69SMichael Jones }
347639ba69SMichael Jones 
35*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
36