Updated to fedora-glibc-20071212T1051
[glibc.git] / sysdeps / unix / sysv / linux / tcsetattr.c
blob21e6ab2ede7ec9d2c23d5f0e1d83876ca3e9bc54
1 /* Copyright (C) 1993,1996,1997,1998,2002,2003 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <string.h>
21 #include <termios.h>
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <sysdep.h>
26 /* The difference here is that the termios structure used in the
27 kernel is not the same as we use in the libc. Therefore we must
28 translate it here. */
29 #include <kernel_termios.h>
32 /* This is a gross hack around a kernel bug. If the cfsetispeed functions
33 is called with the SPEED argument set to zero this means use the same
34 speed as for output. But we don't have independent input and output
35 speeds and therefore cannot record this.
37 We use an unused bit in the `c_iflag' field to keep track of this
38 use of `cfsetispeed'. The value here must correspond to the one used
39 in `speed.c'. */
40 #define IBAUD0 020000000000
43 /* Set the state of FD to *TERMIOS_P. */
44 int
45 tcsetattr (fd, optional_actions, termios_p)
46 int fd;
47 int optional_actions;
48 const struct termios *termios_p;
50 struct __kernel_termios k_termios;
51 unsigned long int cmd;
52 int retval;
54 switch (optional_actions)
56 case TCSANOW:
57 cmd = TCSETS;
58 break;
59 case TCSADRAIN:
60 cmd = TCSETSW;
61 break;
62 case TCSAFLUSH:
63 cmd = TCSETSF;
64 break;
65 default:
66 __set_errno (EINVAL);
67 return -1;
70 k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
71 k_termios.c_oflag = termios_p->c_oflag;
72 k_termios.c_cflag = termios_p->c_cflag;
73 k_termios.c_lflag = termios_p->c_lflag;
74 k_termios.c_line = termios_p->c_line;
75 #if defined _HAVE_C_ISPEED && defined _HAVE_STRUCT_TERMIOS_C_ISPEED
76 k_termios.c_ispeed = termios_p->c_ispeed;
77 #endif
78 #if defined _HAVE_C_OSPEED && defined _HAVE_STRUCT_TERMIOS_C_OSPEED
79 k_termios.c_ospeed = termios_p->c_ospeed;
80 #endif
81 memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
82 __KERNEL_NCCS * sizeof (cc_t));
84 retval = INLINE_SYSCALL (ioctl, 3, fd, cmd, &k_termios);
86 if (retval == 0 && cmd == TCSETS)
88 /* The Linux kernel has a bug which silently ignore the invalid
89 c_cflag on pty. We have to check it here. */
90 int save = errno;
91 retval = INLINE_SYSCALL (ioctl, 3, fd, TCGETS, &k_termios);
92 if (retval)
94 /* We cannot verify if the setting is ok. We don't return
95 an error (?). */
96 __set_errno (save);
97 retval = 0;
99 else if ((termios_p->c_cflag & (PARENB | CREAD))
100 != (k_termios.c_cflag & (PARENB | CREAD))
101 || ((termios_p->c_cflag & CSIZE)
102 && ((termios_p->c_cflag & CSIZE)
103 != (k_termios.c_cflag & CSIZE))))
105 /* It looks like the Linux kernel silently changed the
106 PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
107 error. */
108 __set_errno (EINVAL);
109 retval = -1;
113 return retval;
115 libc_hidden_def (tcsetattr)