1  /* Copyright (C) 1993-2023 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, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <string.h>
      20  #include <termios.h>
      21  #include <sys/ioctl.h>
      22  #include <sys/types.h>
      23  #include <sysdep.h>
      24  
      25  /* The difference here is that the termios structure used in the
      26     kernel is not the same as we use in the libc.  Therefore we must
      27     translate it here.  */
      28  #include <kernel_termios.h>
      29  
      30  
      31  /* This is a gross hack around a kernel bug.  If the cfsetispeed functions
      32     is called with the SPEED argument set to zero this means use the same
      33     speed as for output.  But we don't have independent input and output
      34     speeds and therefore cannot record this.
      35  
      36     We use an unused bit in the `c_iflag' field to keep track of this
      37     use of `cfsetispeed'.  The value here must correspond to the one used
      38     in `speed.c'.  */
      39  #define IBAUD0	020000000000
      40  
      41  
      42  /* Set the state of FD to *TERMIOS_P.  */
      43  int
      44  __tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
      45  {
      46    struct __kernel_termios k_termios;
      47    unsigned long int cmd;
      48  
      49    switch (optional_actions)
      50      {
      51      case TCSANOW:
      52        cmd = TCSETS;
      53        break;
      54      case TCSADRAIN:
      55        cmd = TCSETSW;
      56        break;
      57      case TCSAFLUSH:
      58        cmd = TCSETSF;
      59        break;
      60      default:
      61        return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
      62      }
      63  
      64    k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
      65    k_termios.c_oflag = termios_p->c_oflag;
      66    k_termios.c_cflag = termios_p->c_cflag;
      67    k_termios.c_lflag = termios_p->c_lflag;
      68    k_termios.c_line = termios_p->c_line;
      69  #if _HAVE_C_ISPEED && _HAVE_STRUCT_TERMIOS_C_ISPEED
      70    k_termios.c_ispeed = termios_p->c_ispeed;
      71  #endif
      72  #if _HAVE_C_OSPEED && _HAVE_STRUCT_TERMIOS_C_OSPEED
      73    k_termios.c_ospeed = termios_p->c_ospeed;
      74  #endif
      75    memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
      76  	  __KERNEL_NCCS * sizeof (cc_t));
      77  
      78    return INLINE_SYSCALL (ioctl, 3, fd, cmd, &k_termios);
      79  }
      80  weak_alias (__tcsetattr, tcsetattr)
      81  libc_hidden_def (tcsetattr)