OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libc / termios / tcsetattr.c
1 /* Copyright (C) 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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/>.  */
17
18 #include <errno.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23
24
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"
29
30
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.
35
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
47
48
49 /* Set the state of FD to *TERMIOS_P.  */
50 int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
51 {
52     struct __kernel_termios k_termios;
53     unsigned long int cmd;
54     int retval;
55
56     switch (optional_actions)
57     {
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;
70     }
71
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));
85
86     retval = ioctl (fd, cmd, &k_termios);
87
88     if (retval == 0 && cmd == TCSETS)
89     {
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)
95         {
96             /* We cannot verify if the setting is ok. We don't return
97                an error (?). */
98             __set_errno (save);
99             retval = 0;
100         }
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))))
106         {
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;
112         }
113     }
114
115     return retval;
116 }
117 libc_hidden_def(tcsetattr)