1 | //===-- Linux implementation of tcsetattr ---------------------------------===// |
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/tcsetattr.h" |
10 | #include "kernel_termios.h" |
11 | |
12 | #include "src/__support/OSUtil/syscall.h" |
13 | #include "src/__support/common.h" |
14 | #include "src/__support/libc_errno.h" |
15 | #include "src/__support/macros/config.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, tcsetattr, |
24 | (int fd, int actions, const struct termios *t)) { |
25 | struct kernel_termios kt; |
26 | long cmd; |
27 | |
28 | switch (actions) { |
29 | case TCSANOW: |
30 | cmd = TCSETS; |
31 | break; |
32 | case TCSADRAIN: |
33 | cmd = TCSETSW; |
34 | break; |
35 | case TCSAFLUSH: |
36 | cmd = TCSETSF; |
37 | break; |
38 | default: |
39 | libc_errno = EINVAL; |
40 | return -1; |
41 | } |
42 | |
43 | kt.c_iflag = t->c_iflag; |
44 | kt.c_oflag = t->c_oflag; |
45 | kt.c_cflag = t->c_cflag; |
46 | kt.c_lflag = t->c_lflag; |
47 | size_t nccs = KERNEL_NCCS <= NCCS ? KERNEL_NCCS : NCCS; |
48 | for (size_t i = 0; i < nccs; ++i) |
49 | kt.c_cc[i] = t->c_cc[i]; |
50 | if (nccs < KERNEL_NCCS) { |
51 | for (size_t i = nccs; i < KERNEL_NCCS; ++i) |
52 | kt.c_cc[i] = 0; |
53 | } |
54 | |
55 | int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, cmd, &kt); |
56 | if (ret < 0) { |
57 | libc_errno = -ret; |
58 | return -1; |
59 | } |
60 | return 0; |
61 | } |
62 | |
63 | } // namespace LIBC_NAMESPACE_DECL |
64 | |