Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / termios / linux / tcsetattr.cpp
blobba500a00210d9980bef07af30998159940769e2b
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 //===----------------------------------------------------------------------===//
9 #include "src/termios/tcsetattr.h"
10 #include "kernel_termios.h"
12 #include "src/__support/OSUtil/syscall.h"
13 #include "src/__support/common.h"
14 #include "src/errno/libc_errno.h"
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>
20 namespace LIBC_NAMESPACE {
22 LLVM_LIBC_FUNCTION(int, tcsetattr,
23 (int fd, int actions, const struct termios *t)) {
24 struct kernel_termios kt;
25 long cmd;
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;
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;
54 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, cmd, &kt);
55 if (ret < 0) {
56 libc_errno = -ret;
57 return -1;
59 return 0;
62 } // namespace LIBC_NAMESPACE