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