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