Update.
[glibc.git] / sysdeps / unix / sysv / linux / speed.c
blob1244eb4c01f8c07f0e38f47a328a9ebd5c68c9cd
1 /* `struct termios' speed frobnication functions. Linux version.
2 Copyright (C) 1991, 92, 93, 95, 96, 97, 98 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stddef.h>
21 #include <errno.h>
22 #include <termios.h>
25 /* This is a gross hack around a kernel bug. If the cfsetispeed functions
26 is called with the SPEED argument set to zero this means use the same
27 speed as for output. But we don't have independent input and output
28 speeds and therefore cannot record this.
30 We use an unused bit in the `c_iflag' field to keep track of this
31 use of `cfsetispeed'. The value here must correspond to the one used
32 in `tcsetattr.c'. */
33 #define IBAUD0 020000000000
36 /* Return the output baud rate stored in *TERMIOS_P. */
37 speed_t
38 cfgetospeed (termios_p)
39 const struct termios *termios_p;
41 return termios_p->c_cflag & (CBAUD | CBAUDEX);
44 /* Return the input baud rate stored in *TERMIOS_P.
45 Although for Linux there is no difference between input and output
46 speed, the numerical 0 is a special case for the input baud rate. It
47 should set the input baud rate to the output baud rate. */
48 speed_t
49 cfgetispeed (termios_p)
50 const struct termios *termios_p;
52 return ((termios_p->c_iflag & IBAUD0)
53 ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
56 /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
57 int
58 cfsetospeed (termios_p, speed)
59 struct termios *termios_p;
60 speed_t speed;
62 if ((speed & ~CBAUD) != 0
63 && (speed < B57600 || speed > B460800))
65 __set_errno (EINVAL);
66 return -1;
69 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
70 termios_p->c_cflag |= speed;
72 return 0;
75 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
76 Although for Linux there is no difference between input and output
77 speed, the numerical 0 is a special case for the input baud rate. It
78 should set the input baud rate to the output baud rate. */
79 int
80 cfsetispeed (termios_p, speed)
81 struct termios *termios_p;
82 speed_t speed;
84 if ((speed & ~CBAUD) != 0
85 && (speed < B57600 || speed > B460800))
87 __set_errno (EINVAL);
88 return -1;
91 if (speed == 0)
92 termios_p->c_iflag |= IBAUD0;
93 else
95 termios_p->c_iflag &= ~IBAUD0;
96 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
97 termios_p->c_cflag |= speed;
100 return 0;