elf: Fix _dl_debug_vdprintf to work before self-relocation
[glibc.git] / sysdeps / unix / sysv / linux / speed.c
blob178bbb50405a0075271e9d20c6297f11caf947ff
1 /* `struct termios' speed frobnication functions. Linux version.
2 Copyright (C) 1991-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <stddef.h>
20 #include <errno.h>
21 #include <termios.h>
22 #include <sysdep.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 (const struct termios *termios_p)
39 return termios_p->c_cflag & (CBAUD | CBAUDEX);
42 /* Return the input baud rate stored in *TERMIOS_P.
43 Although for Linux there is no difference between input and output
44 speed, the numerical 0 is a special case for the input baud rate. It
45 should set the input baud rate to the output baud rate. */
46 speed_t
47 cfgetispeed (const struct termios *termios_p)
49 return ((termios_p->c_iflag & IBAUD0)
50 ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
53 /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
54 int
55 cfsetospeed (struct termios *termios_p, speed_t speed)
57 if ((speed & ~CBAUD) != 0
58 && (speed < B57600 || speed > __MAX_BAUD))
59 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
61 #if _HAVE_STRUCT_TERMIOS_C_OSPEED
62 termios_p->c_ospeed = speed;
63 #endif
64 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
65 termios_p->c_cflag |= speed;
67 return 0;
69 libc_hidden_def (cfsetospeed)
72 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
73 Although for Linux there is no difference between input and output
74 speed, the numerical 0 is a special case for the input baud rate. It
75 should set the input baud rate to the output baud rate. */
76 int
77 cfsetispeed (struct termios *termios_p, speed_t speed)
79 if ((speed & ~CBAUD) != 0
80 && (speed < B57600 || speed > __MAX_BAUD))
81 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
83 #if _HAVE_STRUCT_TERMIOS_C_ISPEED
84 termios_p->c_ispeed = speed;
85 #endif
86 if (speed == 0)
87 termios_p->c_iflag |= IBAUD0;
88 else
90 termios_p->c_iflag &= ~IBAUD0;
91 termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
92 termios_p->c_cflag |= speed;
95 return 0;
97 libc_hidden_def (cfsetispeed)