2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / speed.c
blobba394cb05a73c0f4dfd09042ab20c4d5972c8589
1 /* `struct termios' speed frobnication functions. Linux version.
2 Copyright (C) 1991,1992,1993,1995,1996,1997,1998,2000,2002,2003
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stddef.h>
22 #include <errno.h>
23 #include <termios.h>
26 /* This is a gross hack around a kernel bug. If the cfsetispeed functions
27 is called with the SPEED argument set to zero this means use the same
28 speed as for output. But we don't have independent input and output
29 speeds and therefore cannot record this.
31 We use an unused bit in the `c_iflag' field to keep track of this
32 use of `cfsetispeed'. The value here must correspond to the one used
33 in `tcsetattr.c'. */
34 #define IBAUD0 020000000000
37 /* Return the output baud rate stored in *TERMIOS_P. */
38 speed_t
39 cfgetospeed (termios_p)
40 const struct termios *termios_p;
42 return termios_p->c_cflag & (CBAUD | CBAUDEX);
45 /* Return the input baud rate stored in *TERMIOS_P.
46 Although for Linux there is no difference between input and output
47 speed, the numerical 0 is a special case for the input baud rate. It
48 should set the input baud rate to the output baud rate. */
49 speed_t
50 cfgetispeed (termios_p)
51 const struct termios *termios_p;
53 return ((termios_p->c_iflag & IBAUD0)
54 ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
57 /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
58 int
59 cfsetospeed (termios_p, speed)
60 struct termios *termios_p;
61 speed_t speed;
63 if ((speed & ~CBAUD) != 0
64 && (speed < B57600 || speed > __MAX_BAUD))
66 __set_errno (EINVAL);
67 return -1;
70 #ifdef _HAVE_STRUCT_TERMIOS_C_OSPEED
71 termios_p->c_ospeed = speed;
72 #endif
73 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
74 termios_p->c_cflag |= speed;
76 return 0;
78 libc_hidden_def (cfsetospeed)
81 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
82 Although for Linux there is no difference between input and output
83 speed, the numerical 0 is a special case for the input baud rate. It
84 should set the input baud rate to the output baud rate. */
85 int
86 cfsetispeed (termios_p, speed)
87 struct termios *termios_p;
88 speed_t speed;
90 if ((speed & ~CBAUD) != 0
91 && (speed < B57600 || speed > __MAX_BAUD))
93 __set_errno (EINVAL);
94 return -1;
97 #ifdef _HAVE_STRUCT_TERMIOS_C_ISPEED
98 termios_p->c_ispeed = speed;
99 #endif
100 if (speed == 0)
101 termios_p->c_iflag |= IBAUD0;
102 else
104 termios_p->c_iflag &= ~IBAUD0;
105 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
106 termios_p->c_cflag |= speed;
109 return 0;
111 libc_hidden_def (cfsetispeed)