arc: clone: Simplify CLONE_THREAD detection
[uclibc-ng.git] / libc / termios / tcsetattr.c
blob8b9d25361abfbca189f316aa2b0f3e5d27e009b5
1 /* Copyright (C) 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
25 /* The difference here is that the termios structure used in the
26 kernel is not the same as we use in the libc. Therefore we must
27 translate it here. */
28 #include "kernel_termios.h"
31 /* This is a gross hack around a kernel bug. If the cfsetispeed functions
32 is called with the SPEED argument set to zero this means use the same
33 speed as for output. But we don't have independent input and output
34 speeds and therefore cannot record this.
36 We use an unused bit in the `c_iflag' field to keep track of this
37 use of `cfsetispeed'. The value here must correspond to the one used
38 in `speed.c'. */
39 #if !defined _HAVE_C_ISPEED || !defined _HAVE_C_OSPEED
40 # define IBAUD0 020000000000
41 #else
42 /* If we have separate values for input and output speed don't bother
43 with this. Define the value as zero so the compiler sees we don't
44 have to do the AND below. */
45 # define IBAUD0 0
46 #endif
49 /* Set the state of FD to *TERMIOS_P. */
50 int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
52 struct __kernel_termios k_termios;
53 unsigned long int cmd;
54 int retval;
56 switch (optional_actions)
58 case TCSANOW:
59 cmd = TCSETS;
60 break;
61 case TCSADRAIN:
62 cmd = TCSETSW;
63 break;
64 case TCSAFLUSH:
65 cmd = TCSETSF;
66 break;
67 default:
68 __set_errno (EINVAL);
69 return -1;
72 k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
73 k_termios.c_oflag = termios_p->c_oflag;
74 k_termios.c_cflag = termios_p->c_cflag;
75 k_termios.c_lflag = termios_p->c_lflag;
76 k_termios.c_line = termios_p->c_line;
77 #ifdef _HAVE_C_ISPEED
78 k_termios.c_ispeed = termios_p->c_ispeed;
79 #endif
80 #ifdef _HAVE_C_OSPEED
81 k_termios.c_ospeed = termios_p->c_ospeed;
82 #endif
83 memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
84 __KERNEL_NCCS * sizeof (cc_t));
86 retval = ioctl (fd, cmd, &k_termios);
88 if (retval == 0 && cmd == TCSETS)
90 /* The Linux kernel has a bug which silently ignore the invalid
91 c_cflag on pty. We have to check it here. */
92 int save = errno;
93 retval = ioctl (fd, TCGETS, &k_termios);
94 if (retval)
96 /* We cannot verify if the setting is ok. We don't return
97 an error (?). */
98 __set_errno (save);
99 retval = 0;
101 else if ((termios_p->c_cflag & (PARENB | CREAD))
102 != (k_termios.c_cflag & (PARENB | CREAD))
103 || ((termios_p->c_cflag & CSIZE)
104 && ((termios_p->c_cflag & CSIZE)
105 != (k_termios.c_cflag & CSIZE))))
107 /* It looks like the Linux kernel silently changed the
108 PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
109 error. */
110 __set_errno (EINVAL);
111 retval = -1;
115 return retval;
117 libc_hidden_def(tcsetattr)