Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / speed.c
blob62674e132d1b6af57bc10e9a180b93f87967df3e
1 /* `struct termios' speed frobnication functions. Linux version.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stddef.h>
20 #include <errno.h>
21 #include <termios.h>
24 /* This is a gross hack around a kernel bug. If the cfsetispeed functions
25 is called with the SPEED argument set to zero this means use the same
26 speed as for output. But we don't have independent input and output
27 speeds and therefore cannot record this.
29 We use an unused bit in the `c_iflag' field to keep track of this
30 use of `cfsetispeed'. The value here must correspond to the one used
31 in `tcsetattr.c'. */
32 #define IBAUD0 020000000000
35 /* Return the output baud rate stored in *TERMIOS_P. */
36 speed_t
37 cfgetospeed (termios_p)
38 const struct termios *termios_p;
40 return termios_p->c_cflag & (CBAUD | CBAUDEX);
43 /* Return the input baud rate stored in *TERMIOS_P.
44 Although for Linux there is no difference between input and output
45 speed, the numerical 0 is a special case for the input baud rate. It
46 should set the input baud rate to the output baud rate. */
47 speed_t
48 cfgetispeed (termios_p)
49 const struct termios *termios_p;
51 return ((termios_p->c_iflag & IBAUD0)
52 ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
55 /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
56 int
57 cfsetospeed (termios_p, speed)
58 struct termios *termios_p;
59 speed_t speed;
61 if ((speed & ~CBAUD) != 0
62 && (speed < B57600 || speed > __MAX_BAUD))
64 __set_errno (EINVAL);
65 return -1;
68 #ifdef _HAVE_STRUCT_TERMIOS_C_OSPEED
69 termios_p->c_ospeed = speed;
70 #endif
71 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
72 termios_p->c_cflag |= speed;
74 return 0;
76 libc_hidden_def (cfsetospeed)
79 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
80 Although for Linux there is no difference between input and output
81 speed, the numerical 0 is a special case for the input baud rate. It
82 should set the input baud rate to the output baud rate. */
83 int
84 cfsetispeed (termios_p, speed)
85 struct termios *termios_p;
86 speed_t speed;
88 if ((speed & ~CBAUD) != 0
89 && (speed < B57600 || speed > __MAX_BAUD))
91 __set_errno (EINVAL);
92 return -1;
95 #ifdef _HAVE_STRUCT_TERMIOS_C_ISPEED
96 termios_p->c_ispeed = speed;
97 #endif
98 if (speed == 0)
99 termios_p->c_iflag |= IBAUD0;
100 else
102 termios_p->c_iflag &= ~IBAUD0;
103 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
104 termios_p->c_cflag |= speed;
107 return 0;
109 libc_hidden_def (cfsetispeed)