lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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, write to the Free |
| 16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 17 | 02111-1307 USA. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | #include <termios.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | |
| 26 | /* The difference here is that the termios structure used in the |
| 27 | kernel is not the same as we use in the libc. Therefore we must |
| 28 | translate it here. */ |
| 29 | #include "kernel_termios.h" |
| 30 | |
| 31 | |
| 32 | /* This is a gross hack around a kernel bug. If the cfsetispeed functions |
| 33 | is called with the SPEED argument set to zero this means use the same |
| 34 | speed as for output. But we don't have independent input and output |
| 35 | speeds and therefore cannot record this. |
| 36 | |
| 37 | We use an unused bit in the `c_iflag' field to keep track of this |
| 38 | use of `cfsetispeed'. The value here must correspond to the one used |
| 39 | in `speed.c'. */ |
| 40 | #if !defined _HAVE_C_ISPEED || !defined _HAVE_C_OSPEED |
| 41 | # define IBAUD0 020000000000 |
| 42 | #else |
| 43 | /* If we have separate values for input and output speed don't bother |
| 44 | with this. Define the value as zero so the compiler sees we don't |
| 45 | have to do the AND below. */ |
| 46 | # define IBAUD0 0 |
| 47 | #endif |
| 48 | |
| 49 | |
| 50 | /* Set the state of FD to *TERMIOS_P. */ |
| 51 | int tcsetattr (int fd, int optional_actions, const struct termios *termios_p) |
| 52 | { |
| 53 | struct __kernel_termios k_termios; |
| 54 | unsigned long int cmd; |
| 55 | int retval; |
| 56 | |
| 57 | switch (optional_actions) |
| 58 | { |
| 59 | case TCSANOW: |
| 60 | cmd = TCSETS; |
| 61 | break; |
| 62 | case TCSADRAIN: |
| 63 | cmd = TCSETSW; |
| 64 | break; |
| 65 | case TCSAFLUSH: |
| 66 | cmd = TCSETSF; |
| 67 | break; |
| 68 | default: |
| 69 | __set_errno (EINVAL); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0; |
| 74 | k_termios.c_oflag = termios_p->c_oflag; |
| 75 | k_termios.c_cflag = termios_p->c_cflag; |
| 76 | k_termios.c_lflag = termios_p->c_lflag; |
| 77 | k_termios.c_line = termios_p->c_line; |
| 78 | #ifdef _HAVE_C_ISPEED |
| 79 | k_termios.c_ispeed = termios_p->c_ispeed; |
| 80 | #endif |
| 81 | #ifdef _HAVE_C_OSPEED |
| 82 | k_termios.c_ospeed = termios_p->c_ospeed; |
| 83 | #endif |
| 84 | memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0], |
| 85 | __KERNEL_NCCS * sizeof (cc_t)); |
| 86 | |
| 87 | retval = ioctl (fd, cmd, &k_termios); |
| 88 | |
| 89 | if (retval == 0 && cmd == TCSETS) |
| 90 | { |
| 91 | /* The Linux kernel has a bug which silently ignore the invalid |
| 92 | c_cflag on pty. We have to check it here. */ |
| 93 | int save = errno; |
| 94 | retval = ioctl (fd, TCGETS, &k_termios); |
| 95 | if (retval) |
| 96 | { |
| 97 | /* We cannot verify if the setting is ok. We don't return |
| 98 | an error (?). */ |
| 99 | __set_errno (save); |
| 100 | retval = 0; |
| 101 | } |
| 102 | else if ((termios_p->c_cflag & (PARENB | CREAD)) |
| 103 | != (k_termios.c_cflag & (PARENB | CREAD)) |
| 104 | || ((termios_p->c_cflag & CSIZE) |
| 105 | && ((termios_p->c_cflag & CSIZE) |
| 106 | != (k_termios.c_cflag & CSIZE)))) |
| 107 | { |
| 108 | /* It looks like the Linux kernel silently changed the |
| 109 | PARENB/CREAD/CSIZE bits in c_cflag. Report it as an |
| 110 | error. */ |
| 111 | __set_errno (EINVAL); |
| 112 | retval = -1; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return retval; |
| 117 | } |
| 118 | libc_hidden_def(tcsetattr) |