Update.
[glibc.git] / sysdeps / unix / sysv / linux / tcsetattr.c
blobb0f29946e3f21a96ec9e28dce6ccc544c77f7217
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, 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>
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
51 tcsetattr (fd, optional_actions, termios_p)
52 int fd;
53 int optional_actions;
54 const struct termios *termios_p;
56 struct __kernel_termios k_termios;
57 unsigned long int cmd;
58 int retval;
60 switch (optional_actions)
62 case TCSANOW:
63 cmd = TCSETS;
64 break;
65 case TCSADRAIN:
66 cmd = TCSETSW;
67 break;
68 case TCSAFLUSH:
69 cmd = TCSETSF;
70 break;
71 default:
72 __set_errno (EINVAL);
73 return -1;
76 k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
77 k_termios.c_oflag = termios_p->c_oflag;
78 k_termios.c_cflag = termios_p->c_cflag;
79 k_termios.c_lflag = termios_p->c_lflag;
80 k_termios.c_line = termios_p->c_line;
81 #ifdef _HAVE_C_ISPEED
82 k_termios.c_ispeed = termios_p->c_ispeed;
83 #endif
84 #ifdef _HAVE_C_OSPEED
85 k_termios.c_ospeed = termios_p->c_ospeed;
86 #endif
87 memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
88 __KERNEL_NCCS * sizeof (cc_t));
90 retval = __ioctl (fd, cmd, &k_termios);
92 if (retval == 0 && cmd == TCSETS)
94 /* The Linux kernel has a bug which silently ignore the invalid
95 c_cflag on pty. We have to check it here. */
96 int save = errno;
97 retval = __ioctl (fd, TCGETS, &k_termios);
98 if (retval)
100 /* We cannot verify if the setting is ok. We don't return
101 an error (?). */
102 __set_errno (save);
103 retval = 0;
105 else if ((termios_p->c_cflag & (PARENB | CREAD))
106 != (k_termios.c_cflag & (PARENB | CREAD))
107 || ((termios_p->c_cflag & CSIZE)
108 && ((termios_p->c_cflag & CSIZE)
109 != (k_termios.c_cflag & CSIZE))))
111 /* It looks like the Linux kernel silently changed the
112 PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
113 error. */
114 __set_errno (EINVAL);
115 retval = -1;
119 return retval;