xref: /aosp_15_r20/external/llvm-libc/src/termios/linux/tcgetattr.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of tcgetattr ---------------------------------===//
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/termios/tcgetattr.h"
10 #include "kernel_termios.h"
11 
12 #include "src/__support/OSUtil/syscall.h"
13 #include "src/__support/common.h"
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 
17 #include <asm/ioctls.h> // Safe to include without the risk of name pollution.
18 #include <sys/syscall.h> // For syscall numbers
19 #include <termios.h>
20 
21 namespace LIBC_NAMESPACE_DECL {
22 
23 LLVM_LIBC_FUNCTION(int, tcgetattr, (int fd, struct termios *t)) {
24   LIBC_NAMESPACE::kernel_termios kt;
25   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCGETS, &kt);
26   if (ret < 0) {
27     libc_errno = -ret;
28     return -1;
29   }
30   t->c_iflag = kt.c_iflag;
31   t->c_oflag = kt.c_oflag;
32   t->c_cflag = kt.c_cflag;
33   t->c_lflag = kt.c_lflag;
34   t->c_ispeed = kt.c_cflag & CBAUD;
35   t->c_ospeed = kt.c_cflag & CBAUD;
36 
37   size_t nccs = KERNEL_NCCS <= NCCS ? KERNEL_NCCS : NCCS;
38   for (size_t i = 0; i < nccs; ++i)
39     t->c_cc[i] = kt.c_cc[i];
40   if (NCCS > nccs) {
41     for (size_t i = nccs; i < NCCS; ++i)
42       t->c_cc[i] = 0;
43   }
44   return 0;
45 }
46 
47 } // namespace LIBC_NAMESPACE_DECL
48